From afec044323bf0c2dd7bba82bc3fa8fe86eb0060e Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Wed, 25 Oct 2023 11:20:28 +0200 Subject: [PATCH 1/8] Extract table filter indicators getter --- .../tables/resources/views/index.blade.php | 21 +----------- packages/tables/src/Table.php | 1 + .../Table/Concerns/HasFilterIndicators.php | 32 +++++++++++++++++++ 3 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 packages/tables/src/Table/Concerns/HasFilterIndicators.php diff --git a/packages/tables/resources/views/index.blade.php b/packages/tables/resources/views/index.blade.php index ec39ed9f4f..f5dfb1dcfb 100644 --- a/packages/tables/resources/views/index.blade.php +++ b/packages/tables/resources/views/index.blade.php @@ -14,26 +14,7 @@ $content = $getContent(); $contentGrid = $getContentGrid(); $contentFooter = $getContentFooter(); - $filterIndicators = [ - ...($hasSearch() ? ['resetTableSearch' => $getSearchIndicator()] : []), - ...collect($getColumnSearchIndicators()) - ->mapWithKeys(fn (string $indicator, string $column): array => [ - "resetTableColumnSearch('{$column}')" => $indicator, - ]) - ->all(), - ...array_reduce( - $getFilters(), - fn (array $carry, \Filament\Tables\Filters\BaseFilter $filter): array => [ - ...$carry, - ...collect($filter->getIndicators()) - ->mapWithKeys(fn (string $label, int | string $field) => [ - "removeTableFilter('{$filter->getName()}'" . (is_string($field) ? ' , \'' . $field . '\'' : null) . ')' => $label, - ]) - ->all(), - ], - [], - ), - ]; + $filterIndicators = $getFilterIndicators(); $hasColumnsLayout = $hasColumnsLayout(); $hasSummary = $hasSummary(); $header = $getHeader(); diff --git a/packages/tables/src/Table.php b/packages/tables/src/Table.php index 46dbf203ab..92fea794a7 100644 --- a/packages/tables/src/Table.php +++ b/packages/tables/src/Table.php @@ -23,6 +23,7 @@ class Table extends ViewComponent use Table\Concerns\HasColumns; use Table\Concerns\HasContent; use Table\Concerns\HasEmptyState; + use Table\Concerns\HasFilterIndicators; use Table\Concerns\HasFilters; use Table\Concerns\HasHeader; use Table\Concerns\HasHeaderActions; diff --git a/packages/tables/src/Table/Concerns/HasFilterIndicators.php b/packages/tables/src/Table/Concerns/HasFilterIndicators.php new file mode 100644 index 0000000000..e06f525b80 --- /dev/null +++ b/packages/tables/src/Table/Concerns/HasFilterIndicators.php @@ -0,0 +1,32 @@ +hasSearch() ? ['resetTableSearch' => $this->getSearchIndicator()] : []), + ...collect($this->getColumnSearchIndicators()) + ->mapWithKeys(fn (string $indicator, string $column): array => [ + "resetTableColumnSearch('{$column}')" => $indicator, + ]) + ->all(), + ...array_reduce( + $this->getFilters(), + fn (array $carry, BaseFilter $filter): array => [ + ...$carry, + ...collect($filter->getIndicators()) + ->mapWithKeys(fn (string $label, int | string $field) => [ + "removeTableFilter('{$filter->getName()}'" . (is_string($field) ? ' , \'' . $field . '\'' : null) . ')' => $label, + ]) + ->all(), + ], + [], + ), + ]; + } +} From 362a236fb452b01ccc64e7393dad1b85c3778ed9 Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Wed, 25 Oct 2023 14:20:40 +0200 Subject: [PATCH 2/8] wip --- .../components/filters/indicators.blade.php | 22 +++--- .../tables/src/Concerns/CanSearchRecords.php | 11 ++- packages/tables/src/Contracts/HasTable.php | 3 +- .../src/Filters/Concerns/HasIndicators.php | 47 +++++++++-- packages/tables/src/Filters/Filter.php | 10 ++- packages/tables/src/Filters/Indicator.php | 78 +++++++++++++++++++ packages/tables/src/Filters/SelectFilter.php | 18 ++++- packages/tables/src/Filters/TernaryFilter.php | 14 +++- packages/tables/src/Filters/TrashedFilter.php | 6 +- .../src/Table/Concerns/CanSearchRecords.php | 5 +- .../Table/Concerns/HasFilterIndicators.php | 17 ++-- 11 files changed, 185 insertions(+), 46 deletions(-) create mode 100644 packages/tables/src/Filters/Indicator.php diff --git a/packages/tables/resources/views/components/filters/indicators.blade.php b/packages/tables/resources/views/components/filters/indicators.blade.php index 21d994ea33..4792b3b5b5 100644 --- a/packages/tables/resources/views/components/filters/indicators.blade.php +++ b/packages/tables/resources/views/components/filters/indicators.blade.php @@ -13,17 +13,19 @@
- @foreach ($indicators as $wireClickHandler => $label) - - {{ $label }} + @foreach ($indicators as $indicator) + + {{ $indicator->getLabel() }} - + @if ($indicator->isRemovable()) + + @endif @endforeach
diff --git a/packages/tables/src/Concerns/CanSearchRecords.php b/packages/tables/src/Concerns/CanSearchRecords.php index d17e68bb97..2a41dc74f2 100644 --- a/packages/tables/src/Concerns/CanSearchRecords.php +++ b/packages/tables/src/Concerns/CanSearchRecords.php @@ -2,6 +2,7 @@ namespace Filament\Tables\Concerns; +use Filament\Tables\Filters\Indicator; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Arr; use RecursiveArrayIterator; @@ -171,13 +172,14 @@ trait CanSearchRecords $this->updatedTableColumnSearches(); } - public function getTableSearchIndicator(): string + public function getTableSearchIndicator(): Indicator { - return __('filament-tables::table.fields.search.indicator') . ': ' . $this->getTableSearch(); + return Indicator::make(__('filament-tables::table.fields.search.indicator') . ': ' . $this->getTableSearch()) + ->removeLivewireClickHandler('resetTableSearch'); } /** - * @return array + * @return array */ public function getTableColumnSearchIndicators(): array { @@ -200,7 +202,8 @@ trait CanSearchRecords continue; } - $indicators[$columnName] = "{$column->getLabel()}: {$search}"; + $indicators[] = Indicator::make("{$column->getLabel()}: {$search}") + ->removeLivewireClickHandler("resetTableColumnSearch('{$columnName}')"); } return $indicators; diff --git a/packages/tables/src/Contracts/HasTable.php b/packages/tables/src/Contracts/HasTable.php index 5c23dff9af..3ec4e71146 100644 --- a/packages/tables/src/Contracts/HasTable.php +++ b/packages/tables/src/Contracts/HasTable.php @@ -6,6 +6,7 @@ use Filament\Forms\Form; use Filament\Support\Contracts\TranslatableContentDriver; use Filament\Tables\Actions\Action; use Filament\Tables\Actions\BulkAction; +use Filament\Tables\Filters\Indicator; use Filament\Tables\Grouping\Group; use Filament\Tables\Table; use Illuminate\Contracts\Pagination\Paginator; @@ -93,7 +94,7 @@ interface HasTable public function resetTableColumnSearch(string $column): void; - public function getTableSearchIndicator(): string; + public function getTableSearchIndicator(): Indicator; /** * @return array diff --git a/packages/tables/src/Filters/Concerns/HasIndicators.php b/packages/tables/src/Filters/Concerns/HasIndicators.php index 20c4bddd5b..0bdca799b9 100644 --- a/packages/tables/src/Filters/Concerns/HasIndicators.php +++ b/packages/tables/src/Filters/Concerns/HasIndicators.php @@ -3,36 +3,53 @@ namespace Filament\Tables\Filters\Concerns; use Closure; +use Filament\Tables\Filters\Indicator; use Illuminate\Support\Arr; trait HasIndicators { protected string | Closure | null $indicateUsing = null; - protected string | Closure | null $indicator = null; + protected Indicator | string | Closure | null $indicator = null; - public function indicator(string | Closure | null $indicator): static + /** + * @var array | Closure + */ + protected array | Closure $indicators = []; + + public function indicator(Indicator | string | Closure | null $indicator): static { $this->indicator = $indicator; return $this; } - public function indicateUsing(?Closure $callback): static + /** + * @param array | Closure $indicators + */ + public function indicators(array | Closure $indicators): static { - $this->indicateUsing = $callback; + $this->indicators = $indicators; return $this; } /** - * @return array + * @deprecated Use `indicators()` instead. + */ + public function indicateUsing(?Closure $callback): static + { + return $this->indicators($callback); + } + + /** + * @return array | array */ public function getIndicators(): array { $state = $this->getState(); - $indicators = $this->evaluate($this->indicateUsing, [ + $indicators = $this->evaluate($this->indicators, [ 'data' => $state, 'state' => $state, ]); @@ -41,10 +58,24 @@ trait HasIndicators return []; } - return Arr::wrap($indicators); + $indicators = Arr::wrap($indicators); + + foreach ($indicators as $field => $indicator) { + if (! $indicator instanceof Indicator) { + $indicator = Indicator::make($indicator); + } + + if (is_string($field)) { + $indicator = $indicator->removeField($field); + } + + $indicators[$field] = $indicator; + } + + return $indicators; } - public function getIndicator(): string + public function getIndicator(): Indicator | string { $state = $this->getState(); diff --git a/packages/tables/src/Filters/Filter.php b/packages/tables/src/Filters/Filter.php index 72e64debb0..08e0c814cb 100644 --- a/packages/tables/src/Filters/Filter.php +++ b/packages/tables/src/Filters/Filter.php @@ -14,12 +14,18 @@ class Filter extends BaseFilter { parent::setUp(); - $this->indicateUsing(function (array $state): array { + $this->indicators(function (array $state): array { if (! ($state['isActive'] ?? false)) { return []; } - return [$this->getIndicator()]; + $indicator = $this->getIndicator(); + + if (! $indicator instanceof Indicator) { + $indicator = Indicator::make($indicator); + } + + return [$indicator]; }); } diff --git a/packages/tables/src/Filters/Indicator.php b/packages/tables/src/Filters/Indicator.php new file mode 100644 index 0000000000..02a209d116 --- /dev/null +++ b/packages/tables/src/Filters/Indicator.php @@ -0,0 +1,78 @@ +label($label); + } + + public static function make(string | Closure | null $label = null): static + { + return new static($label); + } + + public function label(string $label): static + { + $this->label = $label; + + return $this; + } + + public function getLabel(): string + { + return $this->label; + } + + public function removable(bool | Closure $condition = true): static + { + $this->isRemovable = $condition; + + return $this; + } + + public function isRemovable(): bool + { + return (bool) $this->evaluate($this->isRemovable); + } + + public function removeField(string | Closure | null $name): static + { + $this->removeField = $name; + + return $this; + } + + public function getRemoveField(): ?string + { + return $this->evaluate($this->removeField); + } + + public function removeLivewireClickHandler(string | Closure | null $handler): static + { + $this->removeLivewireClickHandler = $handler; + + return $this; + } + + public function getRemoveLivewireClickHandler(): ?string + { + return $this->evaluate($this->removeLivewireClickHandler); + } +} diff --git a/packages/tables/src/Filters/SelectFilter.php b/packages/tables/src/Filters/SelectFilter.php index 76af930a66..0b1f1b8494 100644 --- a/packages/tables/src/Filters/SelectFilter.php +++ b/packages/tables/src/Filters/SelectFilter.php @@ -39,7 +39,7 @@ class SelectFilter extends BaseFilter __('filament-tables::table.filters.select.placeholder'), ); - $this->indicateUsing(function (SelectFilter $filter, array $state): array { + $this->indicators(function (SelectFilter $filter, array $state): array { if ($filter->isMultiple()) { if (blank($state['values'] ?? null)) { return []; @@ -66,7 +66,13 @@ class SelectFilter extends BaseFilter $labels = collect($labels)->join(', ', ' & '); - return ["{$filter->getIndicator()}: {$labels}"]; + $indicator = $filter->getIndicator(); + + if (! $indicator instanceof Indicator) { + $indicator = Indicator::make("{$indicator}: {$labels}"); + } + + return [$indicator]; } if (blank($state['value'] ?? null)) { @@ -86,7 +92,13 @@ class SelectFilter extends BaseFilter return []; } - return ["{$filter->getIndicator()}: {$label}"]; + $indicator = $filter->getIndicator(); + + if (! $indicator instanceof Indicator) { + $indicator = Indicator::make("{$indicator}: {$label}"); + } + + return [$indicator]; }); $this->resetState(['value' => null]); diff --git a/packages/tables/src/Filters/TernaryFilter.php b/packages/tables/src/Filters/TernaryFilter.php index 9858a0a526..29f3e1ac87 100644 --- a/packages/tables/src/Filters/TernaryFilter.php +++ b/packages/tables/src/Filters/TernaryFilter.php @@ -22,16 +22,22 @@ class TernaryFilter extends SelectFilter $this->boolean(); - $this->indicateUsing(function (array $state): array { + $this->indicators(function (TernaryFilter $filter, array $state): array { if (blank($state['value'] ?? null)) { return []; } $stateLabel = $state['value'] ? - $this->getTrueLabel() : - $this->getFalseLabel(); + $filter->getTrueLabel() : + $filter->getFalseLabel(); - return ["{$this->getIndicator()}: {$stateLabel}"]; + $indicator = $filter->getIndicator(); + + if (! $indicator instanceof Indicator) { + $indicator = Indicator::make("{$indicator}: {$stateLabel}"); + } + + return [$indicator]; }); } diff --git a/packages/tables/src/Filters/TrashedFilter.php b/packages/tables/src/Filters/TrashedFilter.php index 17b7235b2c..81339e0912 100644 --- a/packages/tables/src/Filters/TrashedFilter.php +++ b/packages/tables/src/Filters/TrashedFilter.php @@ -34,16 +34,16 @@ class TrashedFilter extends TernaryFilter SoftDeletingScope::class, ])); - $this->indicateUsing(function (array $state): array { + $this->indicators(function (array $state): array { if ($state['value'] ?? null) { - return [$this->getTrueLabel()]; + return [Indicator::make($this->getTrueLabel())]; } if (blank($state['value'] ?? null)) { return []; } - return [$this->getFalseLabel()]; + return [Indicator::make($this->getFalseLabel())]; }); } } diff --git a/packages/tables/src/Table/Concerns/CanSearchRecords.php b/packages/tables/src/Table/Concerns/CanSearchRecords.php index 8634fbbe01..2b9b39d6dd 100644 --- a/packages/tables/src/Table/Concerns/CanSearchRecords.php +++ b/packages/tables/src/Table/Concerns/CanSearchRecords.php @@ -3,6 +3,7 @@ namespace Filament\Tables\Table\Concerns; use Closure; +use Filament\Tables\Filters\Indicator; trait CanSearchRecords { @@ -79,13 +80,13 @@ trait CanSearchRecords return $this->getLivewire()->hasTableSearch(); } - public function getSearchIndicator(): string + public function getSearchIndicator(): Indicator { return $this->getLivewire()->getTableSearchIndicator(); } /** - * @return array + * @return array | array */ public function getColumnSearchIndicators(): array { diff --git a/packages/tables/src/Table/Concerns/HasFilterIndicators.php b/packages/tables/src/Table/Concerns/HasFilterIndicators.php index e06f525b80..1937d92c8f 100644 --- a/packages/tables/src/Table/Concerns/HasFilterIndicators.php +++ b/packages/tables/src/Table/Concerns/HasFilterIndicators.php @@ -3,26 +3,25 @@ namespace Filament\Tables\Table\Concerns; use Filament\Tables\Filters\BaseFilter; +use Filament\Tables\Filters\Indicator; trait HasFilterIndicators { public function getFilterIndicators(): array { return [ - ...($this->hasSearch() ? ['resetTableSearch' => $this->getSearchIndicator()] : []), - ...collect($this->getColumnSearchIndicators()) - ->mapWithKeys(fn (string $indicator, string $column): array => [ - "resetTableColumnSearch('{$column}')" => $indicator, - ]) - ->all(), + ...($this->hasSearch() ? [$this->getSearchIndicator()] : []), + ...$this->getColumnSearchIndicators(), ...array_reduce( $this->getFilters(), fn (array $carry, BaseFilter $filter): array => [ ...$carry, ...collect($filter->getIndicators()) - ->mapWithKeys(fn (string $label, int | string $field) => [ - "removeTableFilter('{$filter->getName()}'" . (is_string($field) ? ' , \'' . $field . '\'' : null) . ')' => $label, - ]) + ->map(function (Indicator $indicator) use ($filter): Indicator { + $removeField = $indicator->getRemoveField(); + + return $indicator->removeLivewireClickHandler("removeTableFilter('{$filter->getName()}'" . (filled($removeField) ? ', \'' . $removeField . '\'' : null) . ')'); + }) ->all(), ], [], From ed64dd4af4613921257fd62e81c4425507270e49 Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Wed, 25 Oct 2023 14:26:04 +0200 Subject: [PATCH 3/8] docs --- docs-assets/app/app/Livewire/TablesDemo.php | 9 ++++++--- packages/tables/docs/04-filters.md | 15 +++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/docs-assets/app/app/Livewire/TablesDemo.php b/docs-assets/app/app/Livewire/TablesDemo.php index 6ff3bfe7d5..3d64e4306c 100644 --- a/docs-assets/app/app/Livewire/TablesDemo.php +++ b/docs-assets/app/app/Livewire/TablesDemo.php @@ -41,6 +41,7 @@ use Filament\Tables\Contracts\HasTable; use Filament\Tables\Enums\ActionsPosition; use Filament\Tables\Enums\FiltersLayout; use Filament\Tables\Filters\Filter; +use Filament\Tables\Filters\Indicator; use Filament\Tables\Filters\SelectFilter; use Filament\Tables\Grouping\Group; use Filament\Tables\Table; @@ -708,9 +709,11 @@ class TablesDemo extends Component implements HasForms, HasTable return $this->filtersTable($table) ->filters([ Filter::make('dummy') - ->indicateUsing(fn () => [ - 'one' => 'Posted by administrator', - 'two' => 'Less than 1 year old', + ->indicators(fn () => [ + Indicator::make('Posted by administrator') + ->removeField('one'), + Indicator::make('Less than 1 year old') + ->removeField('two'), ]), ]); } diff --git a/packages/tables/docs/04-filters.md b/packages/tables/docs/04-filters.md index 38b5b3330b..f3eedff91c 100644 --- a/packages/tables/docs/04-filters.md +++ b/packages/tables/docs/04-filters.md @@ -314,11 +314,11 @@ Filter::make('is_admin') ->indicator('Administrators') ``` -If you are using a [custom filter form](#custom-filter-forms), you should use [`indicateUsing()`](#custom-active-indicators) to display an active indicator. +If you are using a [custom filter form](#custom-filter-forms), you should use [`indicators()`](#custom-active-indicators) to display an active indicator. ### Custom active indicators -Not all indicators are simple, so you may need to use `indicateUsing()` to customize which indicators should be shown at any time. +Not all indicators are simple, so you may need to use `indicators()` to customize which indicators should be shown at any time. For example, if you have a custom date filter, you may create a custom indicator that formats the selected date: @@ -329,7 +329,7 @@ use Filament\Tables\Filters\Filter; Filter::make('created_at') ->form([DatePicker::make('date')]) // ... - ->indicateUsing(function (array $data): ?string { + ->indicators(function (array $data): ?string { if (! $data['date']) { return null; } @@ -345,6 +345,7 @@ You may even render multiple indicators at once, by returning an array. If you h ```php use Filament\Forms\Components\DatePicker; use Filament\Tables\Filters\Filter; +use Filament\Tables\Filters\Indicator; Filter::make('created_at') ->form([ @@ -352,15 +353,17 @@ Filter::make('created_at') DatePicker::make('until'), ]) // ... - ->indicateUsing(function (array $data): array { + ->indicators(function (array $data): array { $indicators = []; if ($data['from'] ?? null) { - $indicators['from'] = 'Created from ' . Carbon::parse($data['from'])->toFormattedDateString(); + $indicators[] = Indicator::make('Created from ' . Carbon::parse($data['from'])->toFormattedDateString()) + ->removeField('from'); } if ($data['until'] ?? null) { - $indicators['until'] = 'Created until ' . Carbon::parse($data['until'])->toFormattedDateString(); + $indicators[] = Indicator::make('Created until ' . Carbon::parse($data['until'])->toFormattedDateString()) + ->removeField('until'); } return $indicators; From 57a8168e94ff1240bce8cddd7d1b852896544d00 Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Wed, 25 Oct 2023 19:32:11 +0200 Subject: [PATCH 4/8] wip --- packages/tables/src/Filters/Indicator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/tables/src/Filters/Indicator.php b/packages/tables/src/Filters/Indicator.php index 02a209d116..12102245ab 100644 --- a/packages/tables/src/Filters/Indicator.php +++ b/packages/tables/src/Filters/Indicator.php @@ -25,7 +25,7 @@ class Indicator extends Component public static function make(string | Closure | null $label = null): static { - return new static($label); + return app(static::class, ['label' => $label]); } public function label(string $label): static From 611fc4385a4ba8b570765d7553c17b57785039b4 Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Wed, 25 Oct 2023 19:34:43 +0200 Subject: [PATCH 5/8] wip --- packages/tables/src/Contracts/HasTable.php | 2 +- packages/tables/src/Table/Concerns/HasFilterIndicators.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/tables/src/Contracts/HasTable.php b/packages/tables/src/Contracts/HasTable.php index 3ec4e71146..4cbcf61baf 100644 --- a/packages/tables/src/Contracts/HasTable.php +++ b/packages/tables/src/Contracts/HasTable.php @@ -97,7 +97,7 @@ interface HasTable public function getTableSearchIndicator(): Indicator; /** - * @return array + * @return array */ public function getTableColumnSearchIndicators(): array; diff --git a/packages/tables/src/Table/Concerns/HasFilterIndicators.php b/packages/tables/src/Table/Concerns/HasFilterIndicators.php index 1937d92c8f..af7c6e6bbb 100644 --- a/packages/tables/src/Table/Concerns/HasFilterIndicators.php +++ b/packages/tables/src/Table/Concerns/HasFilterIndicators.php @@ -7,6 +7,9 @@ use Filament\Tables\Filters\Indicator; trait HasFilterIndicators { + /** + * @return array + */ public function getFilterIndicators(): array { return [ From 00e398fc9885f74c98c1365cf8c93fdc08ee182c Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Thu, 26 Oct 2023 13:04:40 +0100 Subject: [PATCH 6/8] Update HasIndicators.php --- packages/tables/src/Filters/Concerns/HasIndicators.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/tables/src/Filters/Concerns/HasIndicators.php b/packages/tables/src/Filters/Concerns/HasIndicators.php index 0bdca799b9..a62f38419f 100644 --- a/packages/tables/src/Filters/Concerns/HasIndicators.php +++ b/packages/tables/src/Filters/Concerns/HasIndicators.php @@ -34,9 +34,6 @@ trait HasIndicators return $this; } - /** - * @deprecated Use `indicators()` instead. - */ public function indicateUsing(?Closure $callback): static { return $this->indicators($callback); From 901a99f1ad5f80f4402c8d8cc71ff52e947fe1a8 Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Sun, 29 Oct 2023 14:34:21 +0100 Subject: [PATCH 7/8] wip --- docs-assets/app/app/Livewire/TablesDemo.php | 2 +- packages/tables/docs/04-filters.md | 8 ++++---- .../src/Filters/Concerns/HasIndicators.php | 17 ++--------------- packages/tables/src/Filters/Filter.php | 2 +- packages/tables/src/Filters/SelectFilter.php | 2 +- packages/tables/src/Filters/TernaryFilter.php | 2 +- packages/tables/src/Filters/TrashedFilter.php | 2 +- 7 files changed, 11 insertions(+), 24 deletions(-) diff --git a/docs-assets/app/app/Livewire/TablesDemo.php b/docs-assets/app/app/Livewire/TablesDemo.php index 3d64e4306c..1ede8b1c16 100644 --- a/docs-assets/app/app/Livewire/TablesDemo.php +++ b/docs-assets/app/app/Livewire/TablesDemo.php @@ -709,7 +709,7 @@ class TablesDemo extends Component implements HasForms, HasTable return $this->filtersTable($table) ->filters([ Filter::make('dummy') - ->indicators(fn () => [ + ->indicateUsing(fn () => [ Indicator::make('Posted by administrator') ->removeField('one'), Indicator::make('Less than 1 year old') diff --git a/packages/tables/docs/04-filters.md b/packages/tables/docs/04-filters.md index f3eedff91c..1f27ed5844 100644 --- a/packages/tables/docs/04-filters.md +++ b/packages/tables/docs/04-filters.md @@ -314,11 +314,11 @@ Filter::make('is_admin') ->indicator('Administrators') ``` -If you are using a [custom filter form](#custom-filter-forms), you should use [`indicators()`](#custom-active-indicators) to display an active indicator. +If you are using a [custom filter form](#custom-filter-forms), you should use [`indicateUsing()`](#custom-active-indicators) to display an active indicator. ### Custom active indicators -Not all indicators are simple, so you may need to use `indicators()` to customize which indicators should be shown at any time. +Not all indicators are simple, so you may need to use `indicateUsing()` to customize which indicators should be shown at any time. For example, if you have a custom date filter, you may create a custom indicator that formats the selected date: @@ -329,7 +329,7 @@ use Filament\Tables\Filters\Filter; Filter::make('created_at') ->form([DatePicker::make('date')]) // ... - ->indicators(function (array $data): ?string { + ->indicateUsing(function (array $data): ?string { if (! $data['date']) { return null; } @@ -353,7 +353,7 @@ Filter::make('created_at') DatePicker::make('until'), ]) // ... - ->indicators(function (array $data): array { + ->indicateUsing(function (array $data): array { $indicators = []; if ($data['from'] ?? null) { diff --git a/packages/tables/src/Filters/Concerns/HasIndicators.php b/packages/tables/src/Filters/Concerns/HasIndicators.php index a62f38419f..59aa8358c2 100644 --- a/packages/tables/src/Filters/Concerns/HasIndicators.php +++ b/packages/tables/src/Filters/Concerns/HasIndicators.php @@ -12,11 +12,6 @@ trait HasIndicators protected Indicator | string | Closure | null $indicator = null; - /** - * @var array | Closure - */ - protected array | Closure $indicators = []; - public function indicator(Indicator | string | Closure | null $indicator): static { $this->indicator = $indicator; @@ -24,23 +19,15 @@ trait HasIndicators return $this; } - /** - * @param array | Closure $indicators - */ - public function indicators(array | Closure $indicators): static + public function indicateUsing(?Closure $indicators): static { $this->indicators = $indicators; return $this; } - public function indicateUsing(?Closure $callback): static - { - return $this->indicators($callback); - } - /** - * @return array | array + * @return array */ public function getIndicators(): array { diff --git a/packages/tables/src/Filters/Filter.php b/packages/tables/src/Filters/Filter.php index 08e0c814cb..4412a28b31 100644 --- a/packages/tables/src/Filters/Filter.php +++ b/packages/tables/src/Filters/Filter.php @@ -14,7 +14,7 @@ class Filter extends BaseFilter { parent::setUp(); - $this->indicators(function (array $state): array { + $this->indicateUsing(function (array $state): array { if (! ($state['isActive'] ?? false)) { return []; } diff --git a/packages/tables/src/Filters/SelectFilter.php b/packages/tables/src/Filters/SelectFilter.php index 0b1f1b8494..eb1d6cdccb 100644 --- a/packages/tables/src/Filters/SelectFilter.php +++ b/packages/tables/src/Filters/SelectFilter.php @@ -39,7 +39,7 @@ class SelectFilter extends BaseFilter __('filament-tables::table.filters.select.placeholder'), ); - $this->indicators(function (SelectFilter $filter, array $state): array { + $this->indicateUsing(function (SelectFilter $filter, array $state): array { if ($filter->isMultiple()) { if (blank($state['values'] ?? null)) { return []; diff --git a/packages/tables/src/Filters/TernaryFilter.php b/packages/tables/src/Filters/TernaryFilter.php index 29f3e1ac87..a1fc9daac4 100644 --- a/packages/tables/src/Filters/TernaryFilter.php +++ b/packages/tables/src/Filters/TernaryFilter.php @@ -22,7 +22,7 @@ class TernaryFilter extends SelectFilter $this->boolean(); - $this->indicators(function (TernaryFilter $filter, array $state): array { + $this->indicateUsing(function (TernaryFilter $filter, array $state): array { if (blank($state['value'] ?? null)) { return []; } diff --git a/packages/tables/src/Filters/TrashedFilter.php b/packages/tables/src/Filters/TrashedFilter.php index 81339e0912..5aa99f3fb7 100644 --- a/packages/tables/src/Filters/TrashedFilter.php +++ b/packages/tables/src/Filters/TrashedFilter.php @@ -34,7 +34,7 @@ class TrashedFilter extends TernaryFilter SoftDeletingScope::class, ])); - $this->indicators(function (array $state): array { + $this->indicateUsing(function (array $state): array { if ($state['value'] ?? null) { return [Indicator::make($this->getTrueLabel())]; } From 7fac31eddc0c5f95447a71c5243b0689af12551a Mon Sep 17 00:00:00 2001 From: Zep Fietje Date: Sun, 29 Oct 2023 14:37:17 +0100 Subject: [PATCH 8/8] wip --- packages/tables/src/Filters/Concerns/HasIndicators.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/tables/src/Filters/Concerns/HasIndicators.php b/packages/tables/src/Filters/Concerns/HasIndicators.php index 59aa8358c2..19d3b885c4 100644 --- a/packages/tables/src/Filters/Concerns/HasIndicators.php +++ b/packages/tables/src/Filters/Concerns/HasIndicators.php @@ -19,9 +19,9 @@ trait HasIndicators return $this; } - public function indicateUsing(?Closure $indicators): static + public function indicateUsing(?Closure $callback): static { - $this->indicators = $indicators; + $this->indicateUsing = $callback; return $this; } @@ -33,7 +33,7 @@ trait HasIndicators { $state = $this->getState(); - $indicators = $this->evaluate($this->indicators, [ + $indicators = $this->evaluate($this->indicateUsing, [ 'data' => $state, 'state' => $state, ]);