Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LiveComponent] Fix issue with onUpdated not triggered on nested array proprety #2013

Open
wants to merge 4 commits into
base: 2.x
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,13 @@ private function processOnUpdatedHook(object $component, string $frontendName, L
}

$key = \sprintf('%s.%s', $frontendName, $propName);
if (!$dehydratedUpdatedProps->hasPropValue($key)) {
continue;
}
$fullPaths = $dehydratedUpdatedProps->searchFullPathsForProperty($key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this only under conditions, and keep previous code if no "*"


$this->ensureOnUpdatedMethodExists($component, $funcName);
$propertyOldValue = $dehydratedOriginalProps->getPropValue($key);
$component->{$funcName}($propertyOldValue);
foreach ($fullPaths as $fullPath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop concern one callback for a prop. It should not be called multiple times. I this you could "just" replace the prevous "hasPropValue" to some "matchPropValue" or something like this and let the rest flow.

$this->ensureOnUpdatedMethodExists($component, $funcName);
$propertyOldValue = $dehydratedOriginalProps->getPropValue($fullPath);
Comment on lines +692 to +693
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not compute this every loop

$component->{$funcName}($propertyOldValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the receiving callable as no way to get what is "old value". If a "*" is used, we should pass the full prop

}
}
}
}
7 changes: 7 additions & 0 deletions src/LiveComponent/src/Util/DehydratedProps.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public function getNestedPathsForProperty(string $prop): array
return $nestedPaths;
}

public function searchFullPathsForProperty(string $prop): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some tests if possible (with failing / problematic cases, not just happy path)

{
$regex = '/^'.preg_replace(['/\\\\\*$/i', '/\\\\\*/i'], ['', '.*?'], preg_quote($prop, '/')).'/i';

return preg_grep($regex, array_keys($this->propValues));
}

public function calculateUnexpectedNestedPathsForProperty(string $prop, array $expectedNestedPaths): array
{
$clone = clone $this;
Expand Down
21 changes: 21 additions & 0 deletions src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ public function onEntireEntityUpdated($oldValue)
});
}];

yield 'onUpdated: with wildcard' => [function () {
return HydrationTest::create(new class() {
#[LiveProp(writable: true, onUpdated: ['*' => 'onArrayUpdated'])]
public array $array = [];

public function onArrayUpdated($oldValue)
{
if ('Kevin' === $this->array['name']) {
$this->array['name'] = 'Simon';
}
}
})
->mountWith(['array' => ['name' => 'Ryan']])
->userUpdatesProps([
'array.name' => 'Kevin',
])
->assertObjectAfterHydration(function (object $object) {
self::assertSame('Simon', $object->array['name']);
});
}];

yield 'string: (de)hydrates correctly' => [function () {
return HydrationTest::create(new class() {
#[LiveProp()]
Expand Down