Skip to content

Commit

Permalink
[Fix] selecting a filter from select filter empties all filter select…
Browse files Browse the repository at this point in the history
… inputs (#1460)

* Reorder resolveFilters - Add computedDatasource

* Reorder resolveFilters - Add computedDatasource

* Make laravel stable
  • Loading branch information
luanfreitasdev authored Mar 13, 2024
1 parent 4fe7267 commit bfb6b9c
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 20 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
exclude:
- php: 8.1
laravel: 11.*
include:
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*
dependency-version: [ prefer-stable ]

name: PHP:${{ matrix.php }} / L:${{ matrix.laravel }}
Expand Down Expand Up @@ -54,7 +59,7 @@ jobs:

- name: Install Composer dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer install
- name: Install openspout/openspout
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "PowerGrid generates Advanced Datatables using Laravel Livewire.",
"homepage": "https://github.com/power-components/livewire-powergrid",
"license": "MIT",
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true,
"authors": [
{
Expand Down
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
7 changes: 2 additions & 5 deletions src/DataSource/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,8 @@ public function filterContains(): Builder
$search = strtolower($search);

$this->query = $this->query->where(function (EloquentBuilder|QueryBuilder $query) use ($search) {
if ($query instanceof QueryBuilder) {
$modelTable = $query->from;
} else {
$modelTable = $query->getModel()->getTable();
}
/** @var string $modelTable */
$modelTable = $query instanceof QueryBuilder ? $query->from : $query->getModel()->getTable();

$columnList = $this->getColumnList($modelTable);

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);
}
}
4 changes: 3 additions & 1 deletion src/ProcessDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ private static function processBatch(BaseCollection $collection, PowerGridCompon
protected function setCurrentTable(EloquentBuilder|array|BaseCollection|MorphToMany|Collection|QueryBuilder|null $datasource): void
{
if ($datasource instanceof QueryBuilder) {
$this->component->currentTable = $datasource->from;
/** @var string $from */
$from = $datasource->from;
$this->component->currentTable = $from;

return;
}
Expand Down
38 changes: 36 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,20 @@ 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)
->assertSee('Pastel de Nata')
->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 bfb6b9c

Please sign in to comment.