diff --git a/packages/forms/src/Components/Concerns/HasState.php b/packages/forms/src/Components/Concerns/HasState.php index 04358a6b59..218549eba2 100644 --- a/packages/forms/src/Components/Concerns/HasState.php +++ b/packages/forms/src/Components/Concerns/HasState.php @@ -189,7 +189,7 @@ trait HasState // we need to dehydrate the child component containers while // informing them that they are not dehydrated, so that their // child components get removed from the state. - foreach ($this->getChildComponentContainers() as $container) { + foreach ($this->getChildComponentContainers(withHidden: true) as $container) { $container->dehydrateState($state, isDehydrated: false); } diff --git a/tests/src/Forms/StateTest.php b/tests/src/Forms/StateTest.php index 35b1003085..484cc4ea64 100644 --- a/tests/src/Forms/StateTest.php +++ b/tests/src/Forms/StateTest.php @@ -599,6 +599,28 @@ test('hidden components are not excluded from state dehydration if there is anot ->dehydrateState()->data->not()->toBe([]); }); +test('components in a hidden parent component with a grandparent component with a state path are excluded from state dehydration', function () { + $container = ComponentContainer::make(Livewire::make()) + ->statePath('data') + ->components([ + (new Component) + ->statePath('data') + ->schema([ + (new Component) + ->schema([ + (new Component) + ->statePath(Str::random()) + ->default(Str::random()), + ]) + ->hidden(), + ]), + ]) + ->fill(); + + expect($container) + ->dehydrateState()->toBe(['data' => ['data' => []]]); +}); + test('disabled components are excluded from state dehydration', function () { $container = ComponentContainer::make(Livewire::make()) ->statePath('data')