From 451ba8e406d7da67dc743a8771e00f191f467963 Mon Sep 17 00:00:00 2001 From: tiagorodrigues-creativetech Date: Mon, 23 Oct 2023 08:59:05 +0100 Subject: [PATCH 1/2] Fixes MorphToSelect afterStateUpdated, reactive and lazy. Adds support for lazy and reactive dynamic implementations --- packages/forms/src/Components/MorphToSelect.php | 11 +++++++++-- .../forms/src/Concerns/HasStateBindingModifiers.php | 13 +++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/forms/src/Components/MorphToSelect.php b/packages/forms/src/Components/MorphToSelect.php index 35985f1e12f..024c68d32ed 100644 --- a/packages/forms/src/Components/MorphToSelect.php +++ b/packages/forms/src/Components/MorphToSelect.php @@ -57,7 +57,10 @@ public function getChildComponents(): array )) ->required($isRequired) ->reactive() - ->afterStateUpdated(fn (Closure $set) => $set($keyColumn, null)), + ->afterStateUpdated(function (Closure $set) use ($keyColumn) { + $set($keyColumn, null); + $this->callAfterStateUpdated(); + }), Select::make($keyColumn) ->label($selectedType?->getLabel()) ->disableLabel() @@ -74,7 +77,11 @@ public function getChildComponents(): array ->loadingMessage($this->getLoadingMessage()) ->allowHtml($this->isHtmlAllowed()) ->optionsLimit($this->getOptionsLimit()) - ->preload($this->isPreloaded()), + ->preload($this->isPreloaded()) + ->reactive($this->isReactive()) + ->afterStateUpdated(function () { + $this->callAfterStateUpdated(); + }), ]; } diff --git a/packages/forms/src/Concerns/HasStateBindingModifiers.php b/packages/forms/src/Concerns/HasStateBindingModifiers.php index 29b01ca3afd..5d8f00e01aa 100644 --- a/packages/forms/src/Concerns/HasStateBindingModifiers.php +++ b/packages/forms/src/Concerns/HasStateBindingModifiers.php @@ -2,6 +2,7 @@ namespace Filament\Forms\Concerns; +use Closure; use Filament\Forms\Components\Component; use Illuminate\Support\Str; @@ -11,15 +12,23 @@ trait HasStateBindingModifiers protected string | int | null $debounce = null; - public function reactive(): static + public function reactive(bool | Closure $condition = true): static { + if ($condition === false) { + return $this; + } + $this->stateBindingModifiers([]); return $this; } - public function lazy(): static + public function lazy(bool | Closure $condition = true): static { + if ($condition === false) { + return $this; + } + $this->stateBindingModifiers(['lazy']); return $this; From 1cd382a097253028baf679dd3ab4ab15ea19fe22 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Fri, 27 Oct 2023 12:37:24 +0100 Subject: [PATCH 2/2] Update HasStateBindingModifiers.php --- .../src/Concerns/HasStateBindingModifiers.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/forms/src/Concerns/HasStateBindingModifiers.php b/packages/forms/src/Concerns/HasStateBindingModifiers.php index 5d8f00e01aa..700223c5666 100644 --- a/packages/forms/src/Concerns/HasStateBindingModifiers.php +++ b/packages/forms/src/Concerns/HasStateBindingModifiers.php @@ -2,7 +2,6 @@ namespace Filament\Forms\Concerns; -use Closure; use Filament\Forms\Components\Component; use Illuminate\Support\Str; @@ -12,25 +11,21 @@ trait HasStateBindingModifiers protected string | int | null $debounce = null; - public function reactive(bool | Closure $condition = true): static + public function reactive(bool $condition = true): static { - if ($condition === false) { - return $this; + if ($condition) { + $this->stateBindingModifiers([]); } - $this->stateBindingModifiers([]); - return $this; } - public function lazy(bool | Closure $condition = true): static + public function lazy(bool $condition = true): static { - if ($condition === false) { - return $this; + if ($condition) { + $this->stateBindingModifiers(['lazy']); } - $this->stateBindingModifiers(['lazy']); - return $this; }