From 8cd045426c2dabf0105c024b070c8f07947f7f29 Mon Sep 17 00:00:00 2001 From: ghsgabriel Date: Mon, 22 Sep 2025 05:44:00 -0300 Subject: [PATCH] [4.x] feat: ModalTableSelect badge() and badgeColor() (#17687) * feat: ModalTableSelect badge() and badgeColor() * clean up * Update modal-table-select.blade.php --------- Signed-off-by: ghsgabriel Co-authored-by: Dan Harrin --- packages/forms/docs/03-select.md | 29 ++++++++++ .../components/modal-table-select.blade.php | 53 +++++++++++-------- .../forms/src/Components/ModalTableSelect.php | 28 ++++++++++ 3 files changed, 88 insertions(+), 22 deletions(-) diff --git a/packages/forms/docs/03-select.md b/packages/forms/docs/03-select.md index 2ce631154d..2fa1c5d995 100644 --- a/packages/forms/docs/03-select.md +++ b/packages/forms/docs/03-select.md @@ -926,6 +926,35 @@ ModalTableSelect::make('category_id') ->getOptionLabelFromRecordUsing(fn (Category $record): string => "{$record->name} ({$record->slug})") ``` +By default, `multiple()` options are listed in a "badge" design, and singular options are listed in plain text. The `badge()` method can be used to define whether the option label should appear inside a badge: + +```php +use Filament\Forms\Components\ModalTableSelect; + +ModalTableSelect::make('category_id') + ->relationship('category', 'name') + ->tableConfiguration(CategoriesTable::class) + ->badge() + +ModalTableSelect::make('categories') + ->relationship('categories', 'name') + ->multiple() + ->tableConfiguration(CategoriesTable::class) + ->badge(false) +``` + +The `badgeColor()` method can be used to set the badge [color](../styling/colors): + +```php +use Filament\Forms\Components\ModalTableSelect; + +ModalTableSelect::make('categories') + ->relationship('categories', 'name') + ->multiple() + ->tableConfiguration(CategoriesTable::class) + ->badgeColor('success') +``` + ### Passing additional arguments to the table in a modal select You can pass arguments from your form to the table configuration class using the `tableArguments()` method. For example, this can be used to modify the table's query based on previously filled form fields: diff --git a/packages/forms/resources/views/components/modal-table-select.blade.php b/packages/forms/resources/views/components/modal-table-select.blade.php index cd2c138904..5ab0f6881e 100644 --- a/packages/forms/resources/views/components/modal-table-select.blade.php +++ b/packages/forms/resources/views/components/modal-table-select.blade.php @@ -6,6 +6,8 @@ $id = $getId(); $isDisabled = $isDisabled(); $isMultiple = $isMultiple(); + $hasBadges = $hasBadges(); + $badgeColor = $getBadgeColor(); @endphp @@ -23,38 +25,45 @@ ]) }} > - @if ($isMultiple) - @if (filled($optionLabels = $getOptionLabels())) + @if (((! $isMultiple) && filled($optionLabel = $getOptionLabel())) || + ($isMultiple && filled($optionLabels = $getOptionLabels()))) + @if ($isMultiple && $hasBadges)
@foreach ($optionLabels as $optionLabel) - + @if ($hasBadges) + + {{ $optionLabel }} + + @else {{ $optionLabel }} - + @endif @endforeach
- @elseif (filled($placeholder = $getPlaceholder())) -
- {{ $placeholder }} -
- @endif - - @if (! $isDisabled) + @else
- {{ $getAction('select') }} -
- @endif - @else - @if (filled($optionLabel = $getOptionLabel())) - {{ $optionLabel }} - @elseif (filled($placeholder = $getPlaceholder())) -
- {{ $placeholder }} + @if ($hasBadges) + + {{ $optionLabel }} + + @elseif ($isMultiple) + @foreach ($optionLabels as $optionLabel) + {{ $optionLabel . ($loop->last ? '' : ', ') }} + @endforeach + @else + {{ $optionLabel }} + @endif
@endif + @elseif (filled($placeholder = $getPlaceholder())) +
+ {{ $placeholder }} +
+ @endif - @if (! $isDisabled) + @if (! $isDisabled) +
{{ $getAction('select') }} - @endif +
@endif
diff --git a/packages/forms/src/Components/ModalTableSelect.php b/packages/forms/src/Components/ModalTableSelect.php index ef77420952..0dd733c571 100644 --- a/packages/forms/src/Components/ModalTableSelect.php +++ b/packages/forms/src/Components/ModalTableSelect.php @@ -59,6 +59,10 @@ class ModalTableSelect extends Field protected ?Closure $modifySelectActionUsing = null; + protected bool | Closure | null $hasBadges = null; + + protected string | Closure | null $badgeColor = null; + /** * @var array | Closure */ @@ -669,6 +673,30 @@ class ModalTableSelect extends Field return $this->evaluate($this->tableArguments) ?? []; } + public function badge(bool | Closure | null $condition = true): static + { + $this->hasBadges = $condition; + + return $this; + } + + public function hasBadges(): bool + { + return $this->evaluate($this->hasBadges) ?? $this->isMultiple(); + } + + public function badgeColor(string | Closure | null $color): static + { + $this->badgeColor = $color; + + return $this; + } + + public function getBadgeColor(): ?string + { + return $this->evaluate($this->badgeColor); + } + /** * @return array */