Skip to content

Commit 1d83e0e

Browse files
authored
Migrate LivewireComponentColumn methods and add some missing basic tests (rappasoft#2168)
* Migrate methods and add some missing tests * Fix styling * Add Attribute Checks * Fix styling --------- Co-authored-by: lrljoe <[email protected]>
1 parent 2ecd092 commit 1d83e0e

File tree

4 files changed

+159
-44
lines changed

4 files changed

+159
-44
lines changed

src/Views/Columns/LivewireComponentColumn.php

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

19-
protected string $livewireComponent;
20-
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-
}
19+
protected ?string $livewireComponent;
5820
}

src/Views/Traits/Configuration/LivewireComponentColumnConfiguration.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

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

5+
use Illuminate\Support\Str;
6+
57
trait LivewireComponentColumnConfiguration
68
{
7-
use ComponentColumnConfiguration;
8-
9-
protected string $componentView;
9+
public function component(string $livewireComponent): self
10+
{
11+
$this->livewireComponent = (Str::startsWith($livewireComponent, 'livewire:')) ? substr($livewireComponent, 9) : $livewireComponent;
1012

11-
protected mixed $slotCallback;
13+
return $this;
14+
}
1215
}

src/Views/Traits/Helpers/LivewireComponentColumnHelpers.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,62 @@
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+
throw new DataTableConfigurationException('You must define a Livewire Component for this column');
33+
}
34+
35+
if ($this->isLabel()) {
36+
throw new DataTableConfigurationException('You can not use a label column with a Livewire Component column');
37+
}
38+
39+
$attributes = [];
40+
$value = $this->getValue($row);
41+
42+
if ($this->hasAttributesCallback()) {
43+
$attributes = call_user_func($this->getAttributesCallback(), $value, $row, $this);
44+
45+
if (! is_array($attributes)) {
46+
throw new DataTableConfigurationException('The return type of callback must be an array');
47+
}
48+
}
49+
50+
$implodedAttributes = collect($attributes)->map(function ($value, $key) {
51+
return ':'.$key.'="$'.$key.'"';
52+
})->implode(' ');
53+
54+
return new HtmlString(Blade::render(
55+
'<livewire:dynamic-component :component="$component" '.$implodedAttributes.' :wire:key="'.$row->{$row->getKeyName()}.'" />',
56+
[
57+
'component' => $this->getLivewireComponent(),
58+
...$attributes,
59+
],
60+
));
61+
62+
}
863
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\Column;
9+
use Rappasoft\LaravelLivewireTables\Views\Columns\LivewireComponentColumn;
10+
11+
final class LivewireComponentColumnTest extends TestCase
12+
{
13+
public function test_can_set_the_column_title(): void
14+
{
15+
$column = LivewireComponentColumn::make('Name', 'name');
16+
17+
$this->assertSame('Name', $column->getTitle());
18+
}
19+
20+
public function test_can_not_be_a_label_without_component(): void
21+
{
22+
$this->expectException(DataTableConfigurationException::class);
23+
24+
$column = LivewireComponentColumn::make('Total Users')->label(fn () => 'My Label')->getContents(Pet::find(1));
25+
26+
}
27+
28+
public function test_can_not_be_a_label_with_component(): void
29+
{
30+
$this->expectException(DataTableConfigurationException::class);
31+
32+
$column = LivewireComponentColumn::make('Total Users')->component('test-component')->label(fn () => 'My Label')->getContents(Pet::find(1));
33+
34+
}
35+
36+
public function test_can_add_livewire_component(): void
37+
{
38+
$column = LivewireComponentColumn::make('Name', 'name');
39+
40+
$this->assertFalse($column->hasLivewireComponent());
41+
$column->component('test-component');
42+
$this->assertTrue($column->hasLivewireComponent());
43+
}
44+
45+
public function test_can_get_livewire_component(): void
46+
{
47+
$column = LivewireComponentColumn::make('Name', 'name');
48+
49+
$this->assertFalse($column->hasLivewireComponent());
50+
$this->assertNull($column->getLivewireComponent());
51+
52+
$column->component('test-component');
53+
54+
$this->assertTrue($column->hasLivewireComponent());
55+
$this->assertSame('test-component', $column->getLivewireComponent());
56+
}
57+
58+
public function test_can_not_avoid_defining_livewire_component(): void
59+
{
60+
$this->expectException(DataTableConfigurationException::class);
61+
62+
$contents = LivewireComponentColumn::make('Name')->getContents(Pet::find(1));
63+
64+
}
65+
66+
public function test_attributes_should_return_array(): void
67+
{
68+
$this->expectException(DataTableConfigurationException::class);
69+
70+
$column = LivewireComponentColumn::make('Name')->component('test-component')->attributes(fn ($value, $row, Column $column) => 'test');
71+
72+
$column->getContents(Pet::find(1));
73+
}
74+
75+
public function test_can_check_attribute_callback_presence(): void
76+
{
77+
$column = LivewireComponentColumn::make('Name', 'name')->component('test-component');
78+
$this->assertFalse($column->hasAttributesCallback());
79+
}
80+
81+
public function test_can_set_attribute_callback(): void
82+
{
83+
$column = LivewireComponentColumn::make('Name', 'name')->component('test-component');
84+
$this->assertFalse($column->hasAttributesCallback());
85+
86+
$column->attributes(function ($row) {
87+
return [
88+
'class' => '!rounded-lg self-center',
89+
'default' => true,
90+
];
91+
});
92+
93+
$this->assertTrue($column->hasAttributesCallback());
94+
}
95+
}

0 commit comments

Comments
 (0)