Merge pull request #16295 from alanost/test/dehydration-hidden-fields

Hidden fields incorrectly dehydrated in Section with state path
This commit is contained in:
Dan Harrin
2025-05-20 11:34:13 +01:00
committed by GitHub
3 changed files with 32 additions and 14 deletions
@@ -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
-8
View File
@@ -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;
}
+26
View File
@@ -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')