Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Actions/CallPropertyHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/Actions/CallPropertyHookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});