Skip to content

Commit fe00cff

Browse files
authored
Adjustments for ComponentColumn (#2173)
* Adjustments for ComponentColumn * Fix styling
1 parent 4761dfe commit fe00cff

7 files changed

+90
-35
lines changed

src/Views/Traits/Columns/HasComponentView.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

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

5+
use Illuminate\Support\Facades\View;
6+
57
trait HasComponentView
68
{
79
protected string $componentView;
810

911
public function component(string $component): self
1012
{
11-
$this->componentView = 'components.'.$component;
13+
if (View::exists('components.'.$component)) {
14+
$this->componentView = 'components.'.$component;
15+
} elseif (View::exists($component)) {
16+
$this->componentView = $component;
17+
}
1218

1319
return $this;
1420
}

tests/TestCase.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
1717
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
1818
use Rappasoft\LaravelLivewireTables\Tests\Models\Veterinary;
19+
use Rappasoft\LaravelLivewireTables\Tests\TestServiceProvider;
1920

2021
class TestCase extends Orchestra
2122
{
@@ -139,6 +140,7 @@ protected function setupUnpaginatedTable()
139140
protected function getPackageProviders($app): array
140141
{
141142
return [
143+
TestServiceProvider::class,
142144
LivewireServiceProvider::class,
143145
LaravelLivewireTablesServiceProvider::class,
144146
BladeIconsServiceProvider::class,

tests/TestServiceProvider.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests;
4+
5+
use Illuminate\Support\Facades\Blade;
6+
use Illuminate\Support\ServiceProvider;
7+
use Rappasoft\LaravelLivewireTables\Tests\Http\Components\TestComponent;
8+
9+
class TestServiceProvider extends ServiceProvider
10+
{
11+
public function boot(): void
12+
{
13+
Blade::component('test-component', TestComponent::class);
14+
$this->loadViewsFrom(__DIR__.'/views', 'livewire-tables-test');
15+
16+
}
17+
}

tests/Unit/Views/Columns/ComponentColumnTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Tests\Unit\Views\Columns;
44

5+
use Illuminate\Support\Facades\Blade;
56
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
7+
use Rappasoft\LaravelLivewireTables\Tests\Http\Components\TestComponent;
68
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
79
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
810
use Rappasoft\LaravelLivewireTables\Views\Column;
911
use Rappasoft\LaravelLivewireTables\Views\Columns\ComponentColumn;
1012

1113
final class ComponentColumnTest extends TestCase
1214
{
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
}
19+
1320
public function test_can_set_the_column_title(): void
1421
{
1522
$column = ComponentColumn::make('Name', 'name');
@@ -34,4 +41,57 @@ public function test_can_not_be_both_collapsible_on_mobile_and_on_tablet(): void
3441
$column->getContents($row);
3542

3643
}
44+
45+
public function test_can_set_custom_slot(): void
46+
{
47+
$column = ComponentColumn::make('Age 2', 'age')
48+
->attributes(fn ($value, $row, Column $column) => [
49+
'age' => $row->age,
50+
])
51+
->slot(fn ($value, $row, Column $column) => [
52+
($row->age < 2) => 'test1',
53+
($row->age > 2) => 'test2',
54+
]);
55+
$this->assertTrue($column->hasSlotCallback());
56+
}
57+
58+
public function test_can_get_custom_slot(): void
59+
{
60+
61+
$column = ComponentColumn::make('Age 2', 'age')
62+
->attributes(fn ($value, $row, Column $column) => [
63+
'age' => $row->age,
64+
])
65+
->slot(fn ($value, $row, Column $column) => (($row->age < 10) ? 'youngslot' : 'oldslot'))
66+
->component('livewire-tables-test::test');
67+
68+
$pet1 = Pet::where('age', '>', 11)->first();
69+
$pet1_contents = $column->getContents($pet1);
70+
$this->assertSame('oldslot', $pet1_contents->getData()['slot']->__toString());
71+
72+
$pet2 = Pet::where('age', '<', 5)->first();
73+
$pet2_contents = $column->getContents($pet2);
74+
$this->assertSame('youngslot', $pet2_contents->getData()['slot']->__toString());
75+
76+
}
77+
78+
public function test_can_get_attributes(): void
79+
{
80+
81+
$column = ComponentColumn::make('Age 2', 'age')
82+
->attributes(fn ($value, $row, Column $column) => [
83+
'age' => $row->age,
84+
])
85+
->slot(fn ($value, $row, Column $column) => (($row->age < 10) ? 'youngslot' : 'oldslot'))
86+
->component('livewire-tables-test::test');
87+
88+
$pet1 = Pet::where('age', '>', 11)->first();
89+
$pet1_contents = $column->getContents($pet1);
90+
$this->assertSame(22, $pet1_contents->getData()['attributes']['age']);
91+
92+
$pet2 = Pet::where('age', '<', 5)->first();
93+
$pet2_contents = $column->getContents($pet2);
94+
$this->assertSame(2, $pet2_contents->getData()['attributes']['age']);
95+
96+
}
3797
}

tests/Unit/Views/Columns/ViewComponentColumnTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function test_can_have_component_view(): void
3333
]);
3434

3535
$this->assertFalse($column->hasComponentView());
36-
$column->component('test-component');
36+
$column->component('livewire-tables-test::test');
3737
$this->assertTrue($column->hasComponentView());
3838
}
3939

tests/Unit/Views/ComponentColumnTest.php

-33
This file was deleted.

tests/views/test.blade.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
test
3+
</div>

0 commit comments

Comments
 (0)