diff --git a/src/Actions/CallPropertyHook.php b/src/Actions/CallPropertyHook.php index 5fdbb1a..e0f4d65 100644 --- a/src/Actions/CallPropertyHook.php +++ b/src/Actions/CallPropertyHook.php @@ -3,6 +3,7 @@ namespace Livewire\Volt\Actions; use Closure; +use Illuminate\Support\Arr; use Livewire\Volt\CompileContext; use Livewire\Volt\Component; use Livewire\Volt\Contracts\Action; @@ -21,6 +22,20 @@ public function __construct(protected string $hookName, protected string $proper */ public function execute(CompileContext $context, Component $component, array $arguments): mixed { + if (! isset($context->{$this->hookName}[$this->propertyName]) && str_contains($this->propertyName, '.')) { + $hookedProperties = array_keys($context->{$this->hookName}); + + $matchingProperty = Arr::first($hookedProperties, fn ($key) => str($this->propertyName)->is("$key*")); + + if ($matchingProperty) { + $additionalArgument = str($this->propertyName)->after($matchingProperty)->trim('.')->value(); + + $this->propertyName = $matchingProperty; + + $arguments = array_merge([$additionalArgument], $arguments); + } + } + $hook = $context->{$this->hookName}[$this->propertyName] ?? fn () => null; return call_user_func_array( diff --git a/tests/Feature/Actions/CallPropertyHookTest.php b/tests/Feature/Actions/CallPropertyHookTest.php index 65d8756..8399f28 100644 --- a/tests/Feature/Actions/CallPropertyHookTest.php +++ b/tests/Feature/Actions/CallPropertyHookTest.php @@ -17,3 +17,45 @@ expect($result)->toBe('bar'); }); + +it('calls array property hooks on the component', function () { + $context = CompileContext::make(); + + $context->updating = ['foo' => fn ($prop) => $prop]; + + $component = new class extends Component + { + }; + + $result = (new CallPropertyHook('updating', 'foo.bar'))->execute($context, $component, []); + + expect($result)->toBe('bar'); +}); + +it('calls array property hooks for deep key changed on the component', function () { + $context = CompileContext::make(); + + $context->updating = ['foo' => fn ($prop) => $prop]; + + $component = new class extends Component + { + }; + + $result = (new CallPropertyHook('updating', 'foo.bar.baz'))->execute($context, $component, []); + + expect($result)->toBe('bar.baz'); +}); + +it('calls sub-key property hooks on the component', function () { + $context = CompileContext::make(); + + $context->updating = ['foo.bar' => fn ($prop) => $prop]; + + $component = new class extends Component + { + }; + + $result = (new CallPropertyHook('updating', 'foo.bar.baz'))->execute($context, $component, []); + + expect($result)->toBe('baz'); +});