Skip to content

Commit

Permalink
Merge pull request #15290 from Abdulmajeed-Jamaan/Improve-CanDisableO…
Browse files Browse the repository at this point in the history
…ptions

Allow multiple disable option conditions
  • Loading branch information
danharrin authored Jan 9, 2025
2 parents 5ab442b + 4370a7c commit cf5c208
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/forms/src/Components/Concerns/CanDisableOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
namespace Filament\Forms\Components\Concerns;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

trait CanDisableOptions
{
protected bool | Closure | null $isOptionDisabled = null;
/**
* @var array<bool | Closure>
*/
protected array $isOptionDisabled = [];

public function disableOptionWhen(bool | Closure $callback): static
public function disableOptionWhen(bool | Closure | null $callback, bool $merge = false): static
{
$this->isOptionDisabled = $callback;
if ($merge) {
$this->isOptionDisabled[] = $callback;
} else {
$this->isOptionDisabled = Arr::wrap($callback);
}

return $this;
}
Expand All @@ -38,18 +46,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 collect($this->isOptionDisabled)
->contains(fn (bool | Closure $isOptionDisabled): bool => (bool) $this->evaluate($isOptionDisabled, [
'label' => $label,
'value' => $value,
]));
}

public function hasDynamicDisabledOptions(): bool
{
return $this->isOptionDisabled instanceof Closure;
return collect($this->isOptionDisabled)
->contains(fn (bool | Closure $isOptionDisabled): bool => $isOptionDisabled instanceof Closure);
}
}

0 comments on commit cf5c208

Please sign in to comment.