Skip to content

Adjustments for ComponentColumn #2173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Views/Traits/Columns/HasComponentView.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace Rappasoft\LaravelLivewireTables\Views\Traits\Columns;

use Illuminate\Support\Facades\View;

trait HasComponentView
{
protected string $componentView;

public function component(string $component): self
{
$this->componentView = 'components.'.$component;
if (View::exists('components.'.$component)) {
$this->componentView = 'components.'.$component;
} elseif (View::exists($component)) {
$this->componentView = $component;
}

return $this;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
use Rappasoft\LaravelLivewireTables\Tests\Models\Veterinary;
use Rappasoft\LaravelLivewireTables\Tests\TestServiceProvider;

class TestCase extends Orchestra
{
Expand Down Expand Up @@ -139,6 +140,7 @@ protected function setupUnpaginatedTable()
protected function getPackageProviders($app): array
{
return [
TestServiceProvider::class,
LivewireServiceProvider::class,
LaravelLivewireTablesServiceProvider::class,
BladeIconsServiceProvider::class,
Expand Down
17 changes: 17 additions & 0 deletions tests/TestServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Rappasoft\LaravelLivewireTables\Tests\Http\Components\TestComponent;

class TestServiceProvider extends ServiceProvider
{
public function boot(): void
{
Blade::component('test-component', TestComponent::class);
$this->loadViewsFrom(__DIR__.'/views', 'livewire-tables-test');

}
}
60 changes: 60 additions & 0 deletions tests/Unit/Views/Columns/ComponentColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

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

use Illuminate\Support\Facades\Blade;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Tests\Http\Components\TestComponent;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ComponentColumn;

final class ComponentColumnTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
}

public function test_can_set_the_column_title(): void
{
$column = ComponentColumn::make('Name', 'name');
Expand All @@ -34,4 +41,57 @@ public function test_can_not_be_both_collapsible_on_mobile_and_on_tablet(): void
$column->getContents($row);

}

public function test_can_set_custom_slot(): void
{
$column = ComponentColumn::make('Age 2', 'age')
->attributes(fn ($value, $row, Column $column) => [
'age' => $row->age,
])
->slot(fn ($value, $row, Column $column) => [
($row->age < 2) => 'test1',
($row->age > 2) => 'test2',
]);
$this->assertTrue($column->hasSlotCallback());
}

public function test_can_get_custom_slot(): void
{

$column = ComponentColumn::make('Age 2', 'age')
->attributes(fn ($value, $row, Column $column) => [
'age' => $row->age,
])
->slot(fn ($value, $row, Column $column) => (($row->age < 10) ? 'youngslot' : 'oldslot'))
->component('livewire-tables-test::test');

$pet1 = Pet::where('age', '>', 11)->first();
$pet1_contents = $column->getContents($pet1);
$this->assertSame('oldslot', $pet1_contents->getData()['slot']->__toString());

$pet2 = Pet::where('age', '<', 5)->first();
$pet2_contents = $column->getContents($pet2);
$this->assertSame('youngslot', $pet2_contents->getData()['slot']->__toString());

}

public function test_can_get_attributes(): void
{

$column = ComponentColumn::make('Age 2', 'age')
->attributes(fn ($value, $row, Column $column) => [
'age' => $row->age,
])
->slot(fn ($value, $row, Column $column) => (($row->age < 10) ? 'youngslot' : 'oldslot'))
->component('livewire-tables-test::test');

$pet1 = Pet::where('age', '>', 11)->first();
$pet1_contents = $column->getContents($pet1);
$this->assertSame(22, $pet1_contents->getData()['attributes']['age']);

$pet2 = Pet::where('age', '<', 5)->first();
$pet2_contents = $column->getContents($pet2);
$this->assertSame(2, $pet2_contents->getData()['attributes']['age']);

}
}
2 changes: 1 addition & 1 deletion tests/Unit/Views/Columns/ViewComponentColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function test_can_have_component_view(): void
]);

$this->assertFalse($column->hasComponentView());
$column->component('test-component');
$column->component('livewire-tables-test::test');
$this->assertTrue($column->hasComponentView());
}

Expand Down
33 changes: 0 additions & 33 deletions tests/Unit/Views/ComponentColumnTest.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/views/test.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
test
</div>
Loading