diff --git a/packages/tables/docs/04-filters/02-select.md b/packages/tables/docs/04-filters/02-select.md index ccfc01ed1fb..e84cc1dabad 100644 --- a/packages/tables/docs/04-filters/02-select.md +++ b/packages/tables/docs/04-filters/02-select.md @@ -100,3 +100,20 @@ SelectFilter::make('author') ->relationship('author', 'name') ->searchable() ``` + +## Disable placeholder selection + +You can prevent the placeholder (null option) from being selected using the `selectablePlaceholder()` method: + +```php +use Filament\Tables\Filters\SelectFilter; + +SelectFilter::make('status') + ->options([ + 'draft' => 'Draft', + 'reviewing' => 'Reviewing', + 'published' => 'Published', + ]) + ->default('draft') + ->selectablePlaceholder(false) +``` \ No newline at end of file diff --git a/packages/tables/src/Filters/SelectFilter.php b/packages/tables/src/Filters/SelectFilter.php index 642d70a98e9..0e3edf536da 100644 --- a/packages/tables/src/Filters/SelectFilter.php +++ b/packages/tables/src/Filters/SelectFilter.php @@ -23,6 +23,8 @@ class SelectFilter extends BaseFilter protected bool | Closure $isSearchable = false; + protected bool | Closure $canSelectPlaceholder = true; + protected int | Closure $optionsLimit = 50; protected bool | Closure | null $isSearchForcedCaseInsensitive = null; @@ -211,6 +213,13 @@ public function searchable(bool | Closure $condition = true): static return $this; } + public function selectablePlaceholder(bool | Closure $condition = true): static + { + $this->canSelectPlaceholder = $condition; + + return $this; + } + public function getAttribute(): string { return $this->evaluate($this->attribute) ?? $this->getName(); @@ -243,6 +252,7 @@ public function getFormField(): Select ->multiple($this->isMultiple()) ->placeholder($this->getPlaceholder()) ->searchable($this->isSearchable()) + ->selectablePlaceholder($this->canSelectPlaceholder()) ->preload($this->isPreloaded()) ->native($this->isNative()) ->optionsLimit($this->getOptionsLimit()); @@ -292,6 +302,11 @@ public function isSearchable(): bool return (bool) $this->evaluate($this->isSearchable); } + public function canSelectPlaceholder(): bool + { + return (bool) $this->evaluate($this->canSelectPlaceholder); + } + public function optionsLimit(int | Closure $limit): static { $this->optionsLimit = $limit;