From b84522c49aadec73b861c53666758cd1fc1e3f4e Mon Sep 17 00:00:00 2001 From: Abdulmajeed-Jamaan <41128358+Abdulmajeed-Jamaan@users.noreply.github.com> Date: Wed, 8 Jan 2025 20:55:23 +0300 Subject: [PATCH 1/3] Allow multiple disable option conditions --- .../Components/Concerns/CanDisableOptions.php | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/forms/src/Components/Concerns/CanDisableOptions.php b/packages/forms/src/Components/Concerns/CanDisableOptions.php index 1f15f47f3ff..460d781fa12 100644 --- a/packages/forms/src/Components/Concerns/CanDisableOptions.php +++ b/packages/forms/src/Components/Concerns/CanDisableOptions.php @@ -7,11 +7,18 @@ trait CanDisableOptions { - protected bool | Closure | null $isOptionDisabled = null; + /** + * @var array + */ + protected array $isOptionDisabled = []; - public function disableOptionWhen(bool | Closure $callback): static + public function disableOptionWhen(bool | Closure $callback, bool $merge = false): static { - $this->isOptionDisabled = $callback; + if ($merge) { + $this->isOptionDisabled[] = $callback; + } else { + $this->isOptionDisabled = [$callback]; + } return $this; } @@ -38,18 +45,16 @@ public function getEnabledOptions(): array */ public function isOptionDisabled($value, string $label): bool { - if ($this->isOptionDisabled === null) { - return false; - } - - return (bool) $this->evaluate($this->isOptionDisabled, [ - 'label' => $label, - 'value' => $value, - ]); + return (bool) collect($this->isOptionDisabled) + ->first(fn ($isOptionDisabled) => $this->evaluate($isOptionDisabled, [ + 'label' => $label, + 'value' => $value, + ])); } public function hasDynamicDisabledOptions(): bool { - return $this->isOptionDisabled instanceof Closure; + return (bool) collect($this->isOptionDisabled) + ->first(fn ($isOptionDisabled) => $isOptionDisabled instanceof Closure); } } From 49187459029c84f46f02f653b6ea33f078ff6c4e Mon Sep 17 00:00:00 2001 From: Abdulmajeed-Jamaan <41128358+Abdulmajeed-Jamaan@users.noreply.github.com> Date: Wed, 8 Jan 2025 21:50:43 +0300 Subject: [PATCH 2/3] enhance readability --- .../forms/src/Components/Concerns/CanDisableOptions.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/forms/src/Components/Concerns/CanDisableOptions.php b/packages/forms/src/Components/Concerns/CanDisableOptions.php index 460d781fa12..339ef7efff1 100644 --- a/packages/forms/src/Components/Concerns/CanDisableOptions.php +++ b/packages/forms/src/Components/Concerns/CanDisableOptions.php @@ -45,8 +45,8 @@ public function getEnabledOptions(): array */ public function isOptionDisabled($value, string $label): bool { - return (bool) collect($this->isOptionDisabled) - ->first(fn ($isOptionDisabled) => $this->evaluate($isOptionDisabled, [ + return collect($this->isOptionDisabled) + ->contains(fn ($isOptionDisabled) => $this->evaluate($isOptionDisabled, [ 'label' => $label, 'value' => $value, ])); @@ -54,7 +54,7 @@ public function isOptionDisabled($value, string $label): bool public function hasDynamicDisabledOptions(): bool { - return (bool) collect($this->isOptionDisabled) - ->first(fn ($isOptionDisabled) => $isOptionDisabled instanceof Closure); + return collect($this->isOptionDisabled) + ->contains(fn ($isOptionDisabled) => $isOptionDisabled instanceof Closure); } } From 4370a7ca8aaee6329b6414ce61ce31eb995850a6 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Thu, 9 Jan 2025 10:45:25 +0000 Subject: [PATCH 3/3] Update CanDisableOptions.php --- .../src/Components/Concerns/CanDisableOptions.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/forms/src/Components/Concerns/CanDisableOptions.php b/packages/forms/src/Components/Concerns/CanDisableOptions.php index 339ef7efff1..a6b0863adf0 100644 --- a/packages/forms/src/Components/Concerns/CanDisableOptions.php +++ b/packages/forms/src/Components/Concerns/CanDisableOptions.php @@ -3,21 +3,22 @@ namespace Filament\Forms\Components\Concerns; use Closure; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; trait CanDisableOptions { /** - * @var array + * @var array */ protected array $isOptionDisabled = []; - public function disableOptionWhen(bool | Closure $callback, bool $merge = false): static + public function disableOptionWhen(bool | Closure | null $callback, bool $merge = false): static { if ($merge) { $this->isOptionDisabled[] = $callback; } else { - $this->isOptionDisabled = [$callback]; + $this->isOptionDisabled = Arr::wrap($callback); } return $this; @@ -46,7 +47,7 @@ public function getEnabledOptions(): array public function isOptionDisabled($value, string $label): bool { return collect($this->isOptionDisabled) - ->contains(fn ($isOptionDisabled) => $this->evaluate($isOptionDisabled, [ + ->contains(fn (bool | Closure $isOptionDisabled): bool => (bool) $this->evaluate($isOptionDisabled, [ 'label' => $label, 'value' => $value, ])); @@ -55,6 +56,6 @@ public function isOptionDisabled($value, string $label): bool public function hasDynamicDisabledOptions(): bool { return collect($this->isOptionDisabled) - ->contains(fn ($isOptionDisabled) => $isOptionDisabled instanceof Closure); + ->contains(fn (bool | Closure $isOptionDisabled): bool => $isOptionDisabled instanceof Closure); } }