diff --git a/packages/forms/src/Components/Concerns/HasState.php b/packages/forms/src/Components/Concerns/HasState.php index f33f1d57d6..37d8bba409 100644 --- a/packages/forms/src/Components/Concerns/HasState.php +++ b/packages/forms/src/Components/Concerns/HasState.php @@ -186,16 +186,12 @@ trait HasState return; } - if ($this->getStatePath(isAbsolute: false)) { + if ($this->hasStatePath()) { foreach ($this->getStateToDehydrate() as $key => $value) { Arr::set($state, $key, $value); } } - if ($this->isHiddenAndNotDehydrated()) { - return; - } - foreach ($this->getChildComponentContainers(withHidden: true) as $container) { $container->dehydrateState($state, $isDehydrated); } @@ -438,7 +434,11 @@ trait HasState public function isDehydrated(): bool { - return (bool) $this->evaluate($this->isDehydrated); + if (! $this->evaluate($this->isDehydrated)) { + return false; + } + + return ! $this->isHiddenAndNotDehydrated(); } public function isDehydratedWhenHidden(): bool diff --git a/packages/forms/src/Concerns/HasState.php b/packages/forms/src/Concerns/HasState.php index d6c65009f9..acf82a1255 100644 --- a/packages/forms/src/Concerns/HasState.php +++ b/packages/forms/src/Concerns/HasState.php @@ -72,10 +72,6 @@ trait HasState public function dehydrateState(array &$state = [], bool $isDehydrated = true): array { foreach ($this->getComponents(withHidden: true) as $component) { - if ($component->isHiddenAndNotDehydrated()) { - continue; - } - $component->dehydrateState($state, $isDehydrated); } @@ -89,10 +85,6 @@ trait HasState public function mutateDehydratedState(array &$state = []): array { foreach ($this->getComponents(withHidden: true) as $component) { - if ($component->isHiddenAndNotDehydrated()) { - continue; - } - if (! $component->isDehydrated()) { continue; } diff --git a/tests/src/Forms/StateTest.php b/tests/src/Forms/StateTest.php index f7acaa37a8..64a28815d3 100644 --- a/tests/src/Forms/StateTest.php +++ b/tests/src/Forms/StateTest.php @@ -3,6 +3,9 @@ use Filament\Forms\ComponentContainer; use Filament\Forms\Components\Component; use Filament\Forms\Components\Placeholder; +use Filament\Forms\Components\Radio; +use Filament\Forms\Components\Section; +use Filament\Forms\Components\TextInput; use Filament\Forms\Get; use Filament\Tests\Forms\Fixtures\Livewire; use Filament\Tests\TestCase; @@ -508,6 +511,29 @@ test('hidden components are excluded from state dehydration except if they are m ->dehydrateState()->not()->toBe([]); }); +test('hidden components are excluded from state dehydration even if their parent component has a state path', function () { + $container = ComponentContainer::make(Livewire::make()) + ->statePath('data') + ->components([ + (new Component) + ->statePath('nested') + ->schema([ + (new Component) + ->statePath(Str::random()) + ->default(Str::random()) + ->hidden(), + ]), + ]) + ->fill(); + + expect($container) + ->dehydrateState()->toBe([ + 'data' => [ + 'nested' => [], + ], + ]); +}); + test('disabled components are excluded from state dehydration', function () { $container = ComponentContainer::make(Livewire::make()) ->statePath('data')