diff --git a/docs/index.md b/docs/index.md index 9cedc7a..60dfcd8 100644 --- a/docs/index.md +++ b/docs/index.md @@ -174,6 +174,15 @@ Default value: ] ``` +### afterUpdatingBlocks + +After sorting/editing/adding/deleting a block this closure will be called, you can pass the usual Filament component variables to it. + +```php +ArchitectInput::make('body') + ->afterUpdatingBlocks(fn (Component $livewire) => $livewire->dispatch('refreshPreview')), +``` + ### enableDuplicateButton Enables or disables (disabled by default) the "duplicate" action for the Architect, allowing the user to duplicate rows easily. diff --git a/src/Filament/Fields/ArchitectInput.php b/src/Filament/Fields/ArchitectInput.php index 77ebad7..ed041b6 100644 --- a/src/Filament/Fields/ArchitectInput.php +++ b/src/Filament/Fields/ArchitectInput.php @@ -39,6 +39,8 @@ class ArchitectInput extends Field public Closure|bool $hasTemplates = true; public Closure|bool $hasPreview = true; + public null|Closure $afterUpdatingBlocksCall = null; + protected function setUp(): void { parent::setUp(); @@ -107,6 +109,20 @@ function (self $component, string $statePath, array $data): void { ]); } + public function state(mixed $state): static + { + $return = parent::state($state); + + $this->callAfterUpdatingBlocks(); + + return $return; + } + + public function callAfterUpdatingBlocks(): void + { + $this->evaluate($this->afterUpdatingBlocksCall); + } + public function getArchitectPreviewAction(): Action { return Action::make('architectPreview') @@ -410,6 +426,13 @@ public function getHasPreview(): bool return $this->evaluate($this->hasPreview); } + public function afterUpdatingBlocks(null|Closure $afterUpdatingBlocksCall): static + { + $this->afterUpdatingBlocksCall = $afterUpdatingBlocksCall; + + return $this; + } + private function newBlock(array $data) { return [