refactor: afterStateUpdated bubbling

This commit is contained in:
Sandro
2025-06-04 00:00:04 +04:00
parent eec8eec03c
commit e7b3ac4c8d
3 changed files with 7 additions and 32 deletions
@@ -89,7 +89,7 @@ trait HasState
return $this;
}
public function callAfterStateUpdated(): static
public function callAfterStateUpdated(bool $shouldBubbleToParents = true, bool $isBubbling = false): static
{
foreach ($this->afterStateUpdated as $callback) {
$runId = spl_object_id($callback) . md5(json_encode($this->getState()));
@@ -103,6 +103,10 @@ trait HasState
store($this)->push('executedAfterStateUpdatedCallbacks', value: $runId, iKey: $runId);
}
if ($shouldBubbleToParents && ($isBubbling || $this->isLive())) {
$this->getContainer()->getParentComponent()?->callAfterStateUpdated(isBubbling: true);
}
return $this;
}
@@ -162,21 +162,6 @@ class Repeater extends Field implements Contracts\CanConcealComponents, Contract
});
}
protected function callAfterStateUpdatedOnParents(Component $component, bool $isLive = false): void
{
if (! $isLive && ! $component->isLive()) {
return;
}
$parent = $component->getContainer()->getParentComponent();
if ($parent) {
$parent->callAfterStateUpdated();
$this->callAfterStateUpdatedOnParents($parent, true);
}
}
public function getAddAction(): Action
{
$action = Action::make($this->getAddActionName())
@@ -200,8 +185,6 @@ class Repeater extends Field implements Contracts\CanConcealComponents, Contract
$component->collapsed(false, shouldMakeComponentCollapsible: false);
$component->callAfterStateUpdated();
$this->callAfterStateUpdatedOnParents($component);
})
->button()
->size(ActionSize::Small)
@@ -277,8 +260,6 @@ class Repeater extends Field implements Contracts\CanConcealComponents, Contract
$component->collapsed(false, shouldMakeComponentCollapsible: false);
$component->callAfterStateUpdated();
$this->callAfterStateUpdatedOnParents($component);
})
->button()
->size(ActionSize::Small)
@@ -339,8 +320,6 @@ class Repeater extends Field implements Contracts\CanConcealComponents, Contract
$component->collapsed(false, shouldMakeComponentCollapsible: false);
$component->callAfterStateUpdated();
$this->callAfterStateUpdatedOnParents($component);
})
->iconButton()
->size(ActionSize::Small)
@@ -380,8 +359,6 @@ class Repeater extends Field implements Contracts\CanConcealComponents, Contract
$component->state($items);
$component->callAfterStateUpdated();
$this->callAfterStateUpdatedOnParents($component);
})
->iconButton()
->size(ActionSize::Small)
@@ -420,8 +397,6 @@ class Repeater extends Field implements Contracts\CanConcealComponents, Contract
$component->state($items);
$component->callAfterStateUpdated();
$this->callAfterStateUpdatedOnParents($component);
})
->iconButton()
->size(ActionSize::Small)
@@ -460,8 +435,6 @@ class Repeater extends Field implements Contracts\CanConcealComponents, Contract
$component->state($items);
$component->callAfterStateUpdated();
$this->callAfterStateUpdatedOnParents($component);
})
->iconButton()
->size(ActionSize::Small)
@@ -503,8 +476,6 @@ class Repeater extends Field implements Contracts\CanConcealComponents, Contract
$component->state($items);
$component->callAfterStateUpdated();
$this->callAfterStateUpdatedOnParents($component);
})
->livewireClickHandlerEnabled(false)
->iconButton()
+2 -2
View File
@@ -27,13 +27,13 @@ trait HasState
{
foreach ($this->getComponents(withHidden: true) as $component) {
if ($component->getStatePath() === $path) {
$component->callAfterStateUpdated();
$component->callAfterStateUpdated(shouldBubbleToParents: false);
return true;
}
if (str($path)->startsWith("{$component->getStatePath()}.")) {
$component->callAfterStateUpdated();
$component->callAfterStateUpdated(shouldBubbleToParents: false);
}
foreach ($component->getChildComponentContainers() as $container) {