Skip to content

Commit

Permalink
Reorder resolveFilters - Add computedDatasource
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev committed Mar 13, 2024
1 parent 4fe7267 commit 9031aa7
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class="{{ $filterClasses }}"
{{ $defaultAttributes['selectAttributes'] }}
>
<option value="">{{ trans('livewire-powergrid::datatable.select.all') }}</option>
@foreach (data_get($filter, 'dataSource') as $key => $item)

@php
$computedDatasource = data_get($filter, 'computedDatasource');
$dataSource = filled($computedDatasource)
? $this->{$computedDatasource}
: data_get($filter, 'dataSource');
@endphp

@foreach ($dataSource as $key => $item)
<option
wire:key="select-{{ $tableName }}-{{ $key }}"
value="{{ $item[data_get($filter, 'optionValue')] }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,15 @@ class="{{ $filterClasses }}"
{{ $defaultAttributes['selectAttributes'] }}
>
<option value="">{{ trans('livewire-powergrid::datatable.select.all') }}</option>
@foreach (data_get($filter, 'dataSource') as $key => $item)

@php
$computedDatasource = data_get($filter, 'computedDatasource');
$dataSource = filled($computedDatasource)
? $this->{$computedDatasource}
: data_get($filter, 'dataSource');
@endphp

@foreach ($dataSource as $key => $item)
<option
wire:key="select-{{ $tableName }}-{{ $key }}"
value="{{ $item[data_get($filter, 'optionValue')] }}"
Expand Down
17 changes: 13 additions & 4 deletions resources/views/components/inputs/select.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@
$collection = collect();
if (filled(data_get($filter, 'dataSource'))) {
$collection = collect(data_get($filter, 'dataSource'))->transform(function (array|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $entry) use ($filter) {
if (is_array($entry)) {
$entry = collect($entry);
}
$collection = collect(data_get($filter, 'dataSource'))
->transform(function (array|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $entry) use ($filter) {
if (is_array($entry)) {
$entry = collect($entry);
}
return $entry->only([data_get($filter, 'optionValue'), data_get($filter, 'optionLabel')]);
});
} elseif (filled(data_get($filter, 'computedDatasource'))) {
$collection = collect(data_get($filter, 'computedDatasource'))
->transform(function (array|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $entry) use ($filter) {
if (is_array($entry)) {
$entry = collect($entry);
}
return $entry->only([data_get($filter, 'optionValue'), data_get($filter, 'optionLabel')]);
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/Components/Filters/FilterSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class FilterSelect extends FilterBase

public array $depends = [];

public string $computedDatasource = '';

public function depends(array $fields): FilterSelect
{
$this->depends = $fields;
Expand All @@ -31,6 +33,13 @@ public function dataSource(Collection|array|\Closure $collection): FilterSelect
return $this;
}

public function computedDatasource(string $computedDatasource): FilterSelect
{
$this->computedDatasource = $computedDatasource;

return $this;
}

public function optionValue(string $value): FilterSelect
{
$this->optionValue = $value;
Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,10 @@ private function resolveFilters(): void

data_set($column, 'filters', (array) $filter);

if (isset($this->filters[data_get($filter, 'field')])
if (isset($this->filters[data_get($filter, 'key')])
&& in_array(data_get($filter, 'field'), array_keys($this->filters[data_get($filter, 'key')]))
&& array_values($this->filters[data_get($filter, 'key')])) {
&& array_values($this->filters[data_get($filter, 'key')])
&& !in_array(data_get($filter, 'field'), array_column($this->enabledFilters, 'field'))) {
$this->enabledFilters[] = [
'field' => data_get($filter, 'field'),
'label' => data_get($column, 'title'),
Expand Down
4 changes: 2 additions & 2 deletions src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ public function mount(): void
$this->resolveTotalRow();

$this->restoreState();

$this->resolveFilters();
}

public function fetchDatasource(): void
Expand Down Expand Up @@ -318,6 +316,8 @@ public function render(): Application|Factory|View
/** @phpstan-ignore-next-line */
$this->totalCurrentPage = method_exists($data, 'items') ? count($data->items()) : $data->count();

$this->resolveFilters();

return $this->renderView($data);
}
}
37 changes: 35 additions & 2 deletions tests/Feature/Filters/FilterSelectTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\{Builder, Collection};
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Models\Category;

Expand Down Expand Up @@ -74,8 +74,28 @@ public function filters(): array
}
};

$computedDatasource = new class () extends DishTableBase {
public int $dishId;

#[\Livewire\Attributes\Computed]
public function getAllCategories(): Collection
{
return Category::all();
}

public function filters(): array
{
return [
Filter::select('category_name', 'category_id')
->computedDatasource('getAllCategories')
->optionValue('id')
->optionLabel('name'),
];
}
};

it('property filter using custom builder', function (string $component, object $params) {
$component = livewire($component)
livewire($component)
->call($params->theme)
->set('filters', filterSelect('category_id', 1))
->assertSee('Pastel de Nata')
Expand All @@ -87,6 +107,19 @@ public function filters(): array
'bootstrap -> id' => [$customBuilder::class, (object) ['theme' => 'bootstrap', 'field' => 'id']],
]);

it('property filter using computed datasource', function (string $component, object $params) {
livewire($component)
->call($params->theme)
->set('filters', filterSelect('category_id', 1))
->assertSee('Almôndegas ao Sugo')
->assertDontSee('Pastel de Nata');
;
})->group('filters', 'filterSelect')
->with([
'tailwind -> id' => [$computedDatasource::class, (object) ['theme' => 'tailwind', 'field' => 'id']],
'bootstrap -> id' => [$computedDatasource::class, (object) ['theme' => 'bootstrap', 'field' => 'id']],
]);

it('property filter using custom collection', function (string $component) {
livewire($component)
->set('filters', filterSelect('id', 2))
Expand Down

0 comments on commit 9031aa7

Please sign in to comment.