From c8afab6eabd15d7316a529213b7efcea1255fe2e Mon Sep 17 00:00:00 2001 From: IgorPierre Date: Mon, 21 Jul 2025 16:21:26 -0300 Subject: [PATCH 1/2] fix: support enums as Select options with disableOptionsWhenSelectedInSiblingRepeaterItems --- ...ionsWhenSelectedInSiblingRepeaterItems.php | 8 ++- tests/src/Forms/Components/RepeaterTest.php | 50 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/packages/forms/src/Components/Concerns/CanDisableOptionsWhenSelectedInSiblingRepeaterItems.php b/packages/forms/src/Components/Concerns/CanDisableOptionsWhenSelectedInSiblingRepeaterItems.php index 79bc5b08ff..cd85e1ab7a 100644 --- a/packages/forms/src/Components/Concerns/CanDisableOptionsWhenSelectedInSiblingRepeaterItems.php +++ b/packages/forms/src/Components/Concerns/CanDisableOptionsWhenSelectedInSiblingRepeaterItems.php @@ -27,7 +27,13 @@ trait CanDisableOptionsWhenSelectedInSiblingRepeaterItems ->after('.'), ) ->flatten() - ->diff(Arr::wrap($state)) + ->map(function ($siblingItemState) { + if ($siblingItemState instanceof \UnitEnum) { + return property_exists($siblingItemState, 'value') ? $siblingItemState->value : $siblingItemState->name; + } + return $siblingItemState; + }) + ->diff(Arr::wrap($state instanceof \UnitEnum ? (property_exists($state, 'value') ? $state->value : $state->name) : $state)) ->filter(fn (mixed $siblingItemState): bool => filled($siblingItemState)) ->contains($value); }); diff --git a/tests/src/Forms/Components/RepeaterTest.php b/tests/src/Forms/Components/RepeaterTest.php index 224317ff71..44e950b56f 100644 --- a/tests/src/Forms/Components/RepeaterTest.php +++ b/tests/src/Forms/Components/RepeaterTest.php @@ -8,6 +8,7 @@ use Filament\Tests\TestCase; use Illuminate\Contracts\View\View; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Filament\Forms\Components\Select; use function Filament\Tests\livewire; @@ -140,6 +141,26 @@ it('can remove items from a repeater', function () { $undoRepeaterFake(); }); +it('can use enum as select options 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'; +} From 470917c4e4fdd5a31bc1a00c576aa589c5ad00ff Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Tue, 22 Jul 2025 10:16:06 +0100 Subject: [PATCH 2/2] clean up --- ...isableOptionsWhenSelectedInSiblingRepeaterItems.php | 10 ++++++---- tests/src/Forms/Components/RepeaterTest.php | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/forms/src/Components/Concerns/CanDisableOptionsWhenSelectedInSiblingRepeaterItems.php b/packages/forms/src/Components/Concerns/CanDisableOptionsWhenSelectedInSiblingRepeaterItems.php index cd85e1ab7a..ad8aa007de 100644 --- a/packages/forms/src/Components/Concerns/CanDisableOptionsWhenSelectedInSiblingRepeaterItems.php +++ b/packages/forms/src/Components/Concerns/CanDisableOptionsWhenSelectedInSiblingRepeaterItems.php @@ -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,13 +28,14 @@ trait CanDisableOptionsWhenSelectedInSiblingRepeaterItems ->after('.'), ) ->flatten() - ->map(function ($siblingItemState) { - if ($siblingItemState instanceof \UnitEnum) { - return property_exists($siblingItemState, 'value') ? $siblingItemState->value : $siblingItemState->name; + ->map(function (mixed $siblingItemState): mixed { + if ($siblingItemState instanceof BackedEnum) { + return $siblingItemState->value; } + return $siblingItemState; }) - ->diff(Arr::wrap($state instanceof \UnitEnum ? (property_exists($state, 'value') ? $state->value : $state->name) : $state)) + ->diff(Arr::wrap(($state instanceof BackedEnum) ? $state->value : $state)) ->filter(fn (mixed $siblingItemState): bool => filled($siblingItemState)) ->contains($value); }); diff --git a/tests/src/Forms/Components/RepeaterTest.php b/tests/src/Forms/Components/RepeaterTest.php index 44e950b56f..ab12e4a889 100644 --- a/tests/src/Forms/Components/RepeaterTest.php +++ b/tests/src/Forms/Components/RepeaterTest.php @@ -1,6 +1,7 @@