Skip to content

Commit 5c4b2a9

Browse files
committed
Migrate methods and add some missing tests
1 parent 2ecd092 commit 5c4b2a9

File tree

4 files changed

+134
-42
lines changed

4 files changed

+134
-42
lines changed

src/Views/Columns/LivewireComponentColumn.php

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,6 @@ class LivewireComponentColumn extends Column
1616
use LivewireComponentColumnConfiguration,
1717
LivewireComponentColumnHelpers;
1818

19-
protected string $livewireComponent;
19+
protected ?string $livewireComponent;
2020

21-
public function component(string $livewireComponent): self
22-
{
23-
$this->livewireComponent = (Str::startsWith($livewireComponent, 'livewire:')) ? substr($livewireComponent, 9) : $livewireComponent;
24-
25-
return $this;
26-
}
27-
28-
public function getContents(Model $row): null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
29-
{
30-
if ($this->isLabel()) {
31-
throw new DataTableConfigurationException('You can not use a label column with a component column');
32-
}
33-
34-
$attributes = [];
35-
$value = $this->getValue($row);
36-
37-
if ($this->hasAttributesCallback()) {
38-
$attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this);
39-
40-
if (! is_array($attributes)) {
41-
throw new DataTableConfigurationException('The return type of callback must be an array');
42-
}
43-
}
44-
45-
$implodedAttributes = collect($attributes)->map(function ($value, $key) {
46-
return ':'.$key.'="$'.$key.'"';
47-
})->implode(' ');
48-
49-
return new HtmlString(Blade::render(
50-
'<livewire:dynamic-component :component="$component" '.$implodedAttributes.' :wire:key="'.$row->{$row->getKeyName()}.'" />',
51-
[
52-
'component' => $this->livewireComponent,
53-
...$attributes,
54-
],
55-
));
56-
57-
}
5821
}

src/Views/Traits/Configuration/LivewireComponentColumnConfiguration.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Configuration;
44

5+
use Illuminate\Support\Str;
6+
57
trait LivewireComponentColumnConfiguration
68
{
7-
use ComponentColumnConfiguration;
89

9-
protected string $componentView;
10+
public function component(string $livewireComponent): self
11+
{
12+
$this->livewireComponent = (Str::startsWith($livewireComponent, 'livewire:')) ? substr($livewireComponent, 9) : $livewireComponent;
13+
14+
return $this;
15+
}
1016

11-
protected mixed $slotCallback;
1217
}

src/Views/Traits/Helpers/LivewireComponentColumnHelpers.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,64 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views\Traits\Helpers;
44

5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Blade;
7+
use Illuminate\Support\HtmlString;
8+
use Illuminate\Support\Str;
9+
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
10+
511
trait LivewireComponentColumnHelpers
612
{
7-
use ComponentColumnHelpers;
13+
/**
14+
* Retrieves the defined Component View
15+
*/
16+
public function getLivewireComponent(): ?string
17+
{
18+
return $this->livewireComponent ?? null;
19+
}
20+
21+
/**
22+
* Determines whether a Component View has been set
23+
*/
24+
public function hasLivewireComponent(): bool
25+
{
26+
return isset($this->livewireComponent);
27+
}
28+
29+
public function getContents(Model $row): null|string|HtmlString|DataTableConfigurationException|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
30+
{
31+
if(!$this->hasLivewireComponent())
32+
{
33+
throw new DataTableConfigurationException('You must define a Livewire Component for this column');
34+
}
35+
36+
if ($this->isLabel()) {
37+
throw new DataTableConfigurationException('You can not use a label column with a Livewire Component column');
38+
}
39+
40+
$attributes = [];
41+
$value = $this->getValue($row);
42+
43+
if ($this->hasAttributesCallback()) {
44+
$attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this);
45+
46+
if (! is_array($attributes)) {
47+
throw new DataTableConfigurationException('The return type of callback must be an array');
48+
}
49+
}
50+
51+
$implodedAttributes = collect($attributes)->map(function ($value, $key) {
52+
return ':'.$key.'="$'.$key.'"';
53+
})->implode(' ');
54+
55+
return new HtmlString(Blade::render(
56+
'<livewire:dynamic-component :component="$component" '.$implodedAttributes.' :wire:key="'.$row->{$row->getKeyName()}.'" />',
57+
[
58+
'component' => $this->getLivewireComponent(),
59+
...$attributes,
60+
],
61+
));
62+
63+
}
64+
865
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests\Unit\Views\Columns;
4+
5+
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
6+
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
7+
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
8+
use Rappasoft\LaravelLivewireTables\Views\Columns\LivewireComponentColumn ;
9+
10+
final class LivewireComponentColumnTest extends TestCase
11+
{
12+
public function test_can_set_the_column_title(): void
13+
{
14+
$column = LivewireComponentColumn::make('Name', 'name');
15+
16+
$this->assertSame('Name', $column->getTitle());
17+
}
18+
19+
public function test_can_not_be_a_label(): void
20+
{
21+
$this->expectException(DataTableConfigurationException::class);
22+
23+
$column = LivewireComponentColumn::make('Total Users')->label(fn () => 'My Label')->getContents(Pet::find(1));
24+
25+
}
26+
27+
public function test_can_add_livewire_component(): void
28+
{
29+
$column = LivewireComponentColumn::make('Name', 'name');
30+
31+
$this->assertFalse($column->hasLivewireComponent());
32+
$column->component('test-component');
33+
$this->assertTrue($column->hasLivewireComponent());
34+
}
35+
36+
public function test_can_get_livewire_component(): void
37+
{
38+
$column = LivewireComponentColumn::make('Name', 'name');
39+
40+
$this->assertFalse($column->hasLivewireComponent());
41+
$this->assertNull($column->getLivewireComponent());
42+
43+
$column->component('test-component');
44+
45+
$this->assertTrue($column->hasLivewireComponent());
46+
$this->assertSame('test-component',$column->getLivewireComponent());
47+
}
48+
49+
public function test_can_not_avoid_defining_livewire_component(): void
50+
{
51+
$this->expectException(DataTableConfigurationException::class);
52+
53+
$contents = LivewireComponentColumn::make('Total Users')->getContents(Pet::find(1));
54+
55+
}
56+
57+
public function test_attributes_should_return_array(): void
58+
{
59+
$this->expectException(DataTableConfigurationException::class);
60+
61+
$column = LivewireComponentColumn::make('Total Users')->attributes(fn ($value, $row, Column $column) => 'test');
62+
63+
$column->getContents(Pet::find(1));
64+
}
65+
66+
67+
}

0 commit comments

Comments
 (0)