mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge pull request #17055 from IgorPierre/fix/enum-select-disable-options-repeater
fix: support enums as Select options with disableOptionsWhenSelectedI…
This commit is contained in:
+9
-1
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Filament\Forms\Components\Concerns;
|
||||
|
||||
use BackedEnum;
|
||||
use Filament\Forms\Components\Component;
|
||||
use Filament\Forms\Components\Contracts\CanDisableOptions;
|
||||
use Illuminate\Support\Arr;
|
||||
@@ -27,7 +28,14 @@ trait CanDisableOptionsWhenSelectedInSiblingRepeaterItems
|
||||
->after('.'),
|
||||
)
|
||||
->flatten()
|
||||
->diff(Arr::wrap($state))
|
||||
->map(function (mixed $siblingItemState): mixed {
|
||||
if ($siblingItemState instanceof BackedEnum) {
|
||||
return $siblingItemState->value;
|
||||
}
|
||||
|
||||
return $siblingItemState;
|
||||
})
|
||||
->diff(Arr::wrap(($state instanceof BackedEnum) ? $state->value : $state))
|
||||
->filter(fn (mixed $siblingItemState): bool => filled($siblingItemState))
|
||||
->contains($value);
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Filament\Forms\Components\Repeater;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Tests\Forms\Fixtures\Livewire;
|
||||
@@ -140,6 +141,26 @@ it('can remove items from a repeater', function () {
|
||||
$undoRepeaterFake();
|
||||
});
|
||||
|
||||
it('can use select options from an enum with `disableOptionsWhenSelectedInSiblingRepeaterItems()`', function () {
|
||||
$undoRepeaterFake = Repeater::fake();
|
||||
|
||||
livewire(TestComponentWithEnumSelectRepeater::class)
|
||||
->fillForm([
|
||||
'alternatives' => [
|
||||
['letter' => TestLetterEnum::A],
|
||||
['letter' => TestLetterEnum::B],
|
||||
],
|
||||
])
|
||||
->assertFormSet([
|
||||
'alternatives' => [
|
||||
['letter' => TestLetterEnum::A],
|
||||
['letter' => TestLetterEnum::B],
|
||||
],
|
||||
]);
|
||||
|
||||
$undoRepeaterFake();
|
||||
});
|
||||
|
||||
class TestComponentWithRepeater extends Livewire
|
||||
{
|
||||
public function form(Form $form): Form
|
||||
@@ -178,3 +199,32 @@ class TestComponentWithRepeater extends Livewire
|
||||
return view('forms.fixtures.form');
|
||||
}
|
||||
}
|
||||
|
||||
class TestComponentWithEnumSelectRepeater extends Livewire
|
||||
{
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Repeater::make('alternatives')
|
||||
->schema([
|
||||
Select::make('letter')
|
||||
->options(TestLetterEnum::class)
|
||||
->disableOptionsWhenSelectedInSiblingRepeaterItems(),
|
||||
]),
|
||||
])
|
||||
->statePath('data');
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('forms.fixtures.form');
|
||||
}
|
||||
}
|
||||
|
||||
enum TestLetterEnum: string
|
||||
{
|
||||
case A = 'A';
|
||||
case B = 'B';
|
||||
case C = 'C';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user