From ae7044f682d2cc803a6601f9b024050207dd32b9 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Tue, 2 Nov 2021 11:43:13 +0100 Subject: [PATCH 1/5] adds filter name as filter pills default fallback value --- resources/views/bootstrap-4/includes/filter-pills.blade.php | 2 +- resources/views/bootstrap-5/includes/filter-pills.blade.php | 2 +- resources/views/tailwind/includes/filter-pills.blade.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/bootstrap-4/includes/filter-pills.blade.php b/resources/views/bootstrap-4/includes/filter-pills.blade.php index 4e748b4a4..020ad9775 100644 --- a/resources/views/bootstrap-4/includes/filter-pills.blade.php +++ b/resources/views/bootstrap-4/includes/filter-pills.blade.php @@ -9,7 +9,7 @@ wire:key="filter-pill-{{ $key }}" class="badge badge-pill badge-info d-inline-flex align-items-center" > - {{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}: + {{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, isset($customFilters[$key]) && property_exists($customFilters[$key], 'name') ? $customFilters[$key]->name : ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}: @if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options')) @if(is_array($value)) @foreach($value as $selectedValue) diff --git a/resources/views/bootstrap-5/includes/filter-pills.blade.php b/resources/views/bootstrap-5/includes/filter-pills.blade.php index 6c8c30ea3..dc4cf08d7 100644 --- a/resources/views/bootstrap-5/includes/filter-pills.blade.php +++ b/resources/views/bootstrap-5/includes/filter-pills.blade.php @@ -9,7 +9,7 @@ wire:key="filter-pill-{{ $key }}" class="badge rounded-pill bg-info d-inline-flex align-items-center" > - {{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}: + {{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, isset($customFilters[$key]) && property_exists($customFilters[$key], 'name') ? $customFilters[$key]->name : ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}: @if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options')) @if(is_array($value)) @foreach($value as $selectedValue) diff --git a/resources/views/tailwind/includes/filter-pills.blade.php b/resources/views/tailwind/includes/filter-pills.blade.php index 2491d9437..3919030b2 100644 --- a/resources/views/tailwind/includes/filter-pills.blade.php +++ b/resources/views/tailwind/includes/filter-pills.blade.php @@ -9,7 +9,7 @@ wire:key="filter-pill-{{ $key }}" class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium leading-4 bg-indigo-100 text-indigo-800 capitalize dark:bg-indigo-200 dark:text-indigo-900" > - {{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}: + {{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, isset($customFilters[$key]) && property_exists($customFilters[$key], 'name') ? $customFilters[$key]->name : ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}: @if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options')) @if(is_array($value)) @foreach($value as $selectedValue) From ac0cbf5bba4b32a58fdde19f42167356d73798b5 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Tue, 2 Nov 2021 12:25:32 +0100 Subject: [PATCH 2/5] add tests for filters --- tests/DataTableComponentTest.php | 40 +++++++++++++++++++++++++++++++ tests/Http/Livewire/PetsTable.php | 27 +++++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/tests/DataTableComponentTest.php b/tests/DataTableComponentTest.php index 470d6875d..8726fc42a 100644 --- a/tests/DataTableComponentTest.php +++ b/tests/DataTableComponentTest.php @@ -3,7 +3,11 @@ namespace Rappasoft\LaravelLivewireTables\Tests; use Illuminate\Contracts\Pagination\LengthAwarePaginator; +use Illuminate\Encryption\Encrypter; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Hash; +use Illuminate\View\View; +use Livewire\Livewire; use Rappasoft\LaravelLivewireTables\DataTableComponent; use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsAltQueryTable; use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable; @@ -115,6 +119,42 @@ public function search_filter_alt_query_relation() $this->assertEquals(2, $this->tableAltQuery->rows->total()); } + /** @test */ + public function custom_filter() + { + $this->table->filters['species.name'] = 1; + + $this->assertTrue($this->table->getRowsProperty()->where('name', 'Cartman')->isNotEmpty()); + $this->assertTrue($this->table->getRowsProperty()->where('name', 'Tux')->isNotEmpty()); + $this->assertFalse($this->table->getRowsProperty()->where('May', 'Tux')->isNotEmpty()); + $this->assertFalse($this->table->getRowsProperty()->where('Ben', 'Tux')->isNotEmpty()); + $this->assertFalse($this->table->getRowsProperty()->where('Chico', 'Tux')->isNotEmpty()); + } + + /** @test */ + public function custom_filters_pills_label_use_column_name_when_possible() + { + config()->set('app.key', Encrypter::generateKey(config('app.cipher'))); + + Livewire::test(PetsTable::class) + ->set('filters', [ + 'species.name' => 1, + ]) + ->assertSeeTextInOrder(['Applied Filters:', 'Species:', 'Cat', 'Filters']); + } + + /** @test */ + public function custom_filters_pills_label_use_filter_name_when_is_not_bound_to_a_column() + { + config()->set('app.key', Encrypter::generateKey(config('app.cipher'))); + + Livewire::test(PetsTable::class) + ->set('filters', [ + 'breed_id' => 1 + ]) + ->assertSeeTextInOrder(['Applied Filters:', 'Filter Breed:', 'American Shorthair', 'Filters']); + } + /** @test */ public function bulk_actions_defined_through_function() { diff --git a/tests/Http/Livewire/PetsTable.php b/tests/Http/Livewire/PetsTable.php index 707290505..4dcdbd47a 100644 --- a/tests/Http/Livewire/PetsTable.php +++ b/tests/Http/Livewire/PetsTable.php @@ -6,9 +6,30 @@ use Rappasoft\LaravelLivewireTables\DataTableComponent; use Rappasoft\LaravelLivewireTables\Tests\Models\Pet; use Rappasoft\LaravelLivewireTables\Views\Column; +use Rappasoft\LaravelLivewireTables\Views\Filter; class PetsTable extends DataTableComponent { + public function filters(): array + { + return [ + 'species.name' => Filter::make('Filter Species')->select([ + '' => 'All', + 1 => 'Cat', + 2 => 'Dog', + 3 => 'Horse', + 4 => 'Bird', + ]), + 'breed_id' => Filter::make('Filter Breed')->select([ + '' => 'All', + 1 => 'American Shorthair', + 2 => 'Maine Coon', + 3 => 'Persian', + 4 => 'Norwegian Forest', + ]), + ]; + } + public function bulkActions(): array { return ['count' => 'Count selected']; @@ -17,11 +38,13 @@ public function bulkActions(): array /** * @return Builder */ - public function query() : Builder + public function query(): Builder { return Pet::query() ->with('species') - ->with('breed'); + ->with('breed') + ->when($this->getFilter('species.name'), fn (Builder $query, $specimen_id) => $query->where('species_id', $specimen_id)) + ->when($this->getFilter('breed_id'), fn (Builder $query, $breed_id) => $query->where('breed_id', $breed_id)); } public function columns(): array From f10990d484b205712d0e209bc14e73b77fada3ce Mon Sep 17 00:00:00 2001 From: fabio-ivona Date: Tue, 2 Nov 2021 11:25:57 +0000 Subject: [PATCH 3/5] Fix styling --- tests/DataTableComponentTest.php | 4 +--- tests/Http/Livewire/PetsTable.php | 18 +++++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/tests/DataTableComponentTest.php b/tests/DataTableComponentTest.php index 8726fc42a..4dfca67b3 100644 --- a/tests/DataTableComponentTest.php +++ b/tests/DataTableComponentTest.php @@ -5,8 +5,6 @@ use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Encryption\Encrypter; use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Hash; -use Illuminate\View\View; use Livewire\Livewire; use Rappasoft\LaravelLivewireTables\DataTableComponent; use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsAltQueryTable; @@ -150,7 +148,7 @@ public function custom_filters_pills_label_use_filter_name_when_is_not_bound_to_ Livewire::test(PetsTable::class) ->set('filters', [ - 'breed_id' => 1 + 'breed_id' => 1, ]) ->assertSeeTextInOrder(['Applied Filters:', 'Filter Breed:', 'American Shorthair', 'Filters']); } diff --git a/tests/Http/Livewire/PetsTable.php b/tests/Http/Livewire/PetsTable.php index 4dcdbd47a..564494475 100644 --- a/tests/Http/Livewire/PetsTable.php +++ b/tests/Http/Livewire/PetsTable.php @@ -15,17 +15,17 @@ public function filters(): array return [ 'species.name' => Filter::make('Filter Species')->select([ '' => 'All', - 1 => 'Cat', - 2 => 'Dog', - 3 => 'Horse', - 4 => 'Bird', + 1 => 'Cat', + 2 => 'Dog', + 3 => 'Horse', + 4 => 'Bird', ]), - 'breed_id' => Filter::make('Filter Breed')->select([ + 'breed_id' => Filter::make('Filter Breed')->select([ '' => 'All', - 1 => 'American Shorthair', - 2 => 'Maine Coon', - 3 => 'Persian', - 4 => 'Norwegian Forest', + 1 => 'American Shorthair', + 2 => 'Maine Coon', + 3 => 'Persian', + 4 => 'Norwegian Forest', ]), ]; } From 9ea0f1ffb6444673fb22c438d081c2bf877f0a51 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Tue, 2 Nov 2021 12:47:35 +0100 Subject: [PATCH 4/5] fix tests prefer-lowest error --- resources/views/bootstrap-4/components/table/row.blade.php | 2 +- resources/views/bootstrap-5/components/table/row.blade.php | 2 +- resources/views/tailwind/components/table/row.blade.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/views/bootstrap-4/components/table/row.blade.php b/resources/views/bootstrap-4/components/table/row.blade.php index 6a0f27458..9fa710eee 100644 --- a/resources/views/bootstrap-4/components/table/row.blade.php +++ b/resources/views/bootstrap-4/components/table/row.blade.php @@ -1,6 +1,6 @@ @props(['url' => null, 'target' => '_self', 'reordering' => false, 'customAttributes' => []]) -@if (!$reordering && $attributes->has('wire:sortable.item')) +@if (!$reordering && (method_exists($attributes, 'has') ? $attributes->has('wire:sortable.item') : array_key_exists('wire:sortable.item', $attributes->getAttributes()))) @php $attributes = $attributes->filter(fn ($value, $key) => $key !== 'wire:sortable.item'); @endphp diff --git a/resources/views/bootstrap-5/components/table/row.blade.php b/resources/views/bootstrap-5/components/table/row.blade.php index 6a0f27458..9fa710eee 100644 --- a/resources/views/bootstrap-5/components/table/row.blade.php +++ b/resources/views/bootstrap-5/components/table/row.blade.php @@ -1,6 +1,6 @@ @props(['url' => null, 'target' => '_self', 'reordering' => false, 'customAttributes' => []]) -@if (!$reordering && $attributes->has('wire:sortable.item')) +@if (!$reordering && (method_exists($attributes, 'has') ? $attributes->has('wire:sortable.item') : array_key_exists('wire:sortable.item', $attributes->getAttributes()))) @php $attributes = $attributes->filter(fn ($value, $key) => $key !== 'wire:sortable.item'); @endphp diff --git a/resources/views/tailwind/components/table/row.blade.php b/resources/views/tailwind/components/table/row.blade.php index c6cf88265..1c40c5531 100644 --- a/resources/views/tailwind/components/table/row.blade.php +++ b/resources/views/tailwind/components/table/row.blade.php @@ -1,6 +1,6 @@ @props(['url' => null, 'target' => '_self', 'reordering' => false, 'customAttributes' => []]) -@if (!$reordering && $attributes->has('wire:sortable.item')) +@if (!$reordering && (method_exists($attributes, 'has') ? $attributes->has('wire:sortable.item') : array_key_exists('wire:sortable.item', $attributes->getAttributes()))) @php $attributes = $attributes->filter(fn ($value, $key) => $key !== 'wire:sortable.item'); @endphp From 36986f2eaf7b5d63e0e3f0a89999ebf144c607ac Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Tue, 2 Nov 2021 12:53:43 +0100 Subject: [PATCH 5/5] fix tests prefer-lowest error --- resources/views/tailwind/components/table/row.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/tailwind/components/table/row.blade.php b/resources/views/tailwind/components/table/row.blade.php index 1c40c5531..f22f7e99d 100644 --- a/resources/views/tailwind/components/table/row.blade.php +++ b/resources/views/tailwind/components/table/row.blade.php @@ -7,7 +7,7 @@ @endif merge($customAttributes)->class(['cursor-pointer' => $url]) }} + {{ $attributes->merge($customAttributes)->merge(['class' => $url ? 'cursor-pointer' : '']) }} @if ($url) onclick="window.open('{{ $url }}', '{{ $target }}')"