Skip to content

Commit 883dbaf

Browse files
committed
[LiveComponent] Fix new URL generation when using LiveProp with custom fieldName
1 parent e0976b1 commit 883dbaf

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/LiveComponent/src/Metadata/LiveComponentMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getAllUrlMappings(object $component): array
7575

7676
foreach ($this->getAllLivePropsMetadata($component) as $livePropMetadata) {
7777
if ($livePropMetadata->urlMapping()) {
78-
$urlMappings[$livePropMetadata->getName()] = $livePropMetadata->urlMapping();
78+
$urlMappings[$livePropMetadata->calculateFieldName($component, $livePropMetadata->getName())] = $livePropMetadata->urlMapping();
7979
}
8080
}
8181

src/LiveComponent/tests/Fixtures/Component/ComponentWithUrlBoundProps.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class ComponentWithUrlBoundProps
3333
#[LiveProp(url: true)]
3434
public array $arrayProp = [];
3535

36+
#[LiveProp(url: new UrlMapping(as: 'arr_alias'))]
37+
public array $arrayPropAlias = [];
38+
39+
#[LiveProp(writable: true, fieldName: 'getArrayFieldName()', url: true)]
40+
public array $arrayPropFieldName = [];
41+
3642
#[LiveProp]
3743
public ?string $unboundProp = null;
3844

@@ -113,4 +119,9 @@ public function updateLiveProp(#[LiveArg] string $propName, #[LiveArg] mixed $pr
113119

114120
$this->{$propName} = $propValue;
115121
}
122+
123+
public function getArrayFieldName(): string
124+
{
125+
return 'arr_field_name';
126+
}
116127
}

src/LiveComponent/tests/Functional/EventListener/LiveUrlSubscriberTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function getTestData(): iterable
118118
'propValue' => 'bar',
119119
],
120120
];
121+
121122
yield 'Changes in prop, with two path params but only one prop' => [
122123
'previousLocation' => '/route_with_two_path_params_but_one_prop/foo/30',
123124
'expectedLocation' => '/route_with_two_path_params_but_one_prop/bar/30',
@@ -140,6 +141,42 @@ public function getTestData(): iterable
140141
],
141142
];
142143

144+
yield 'Change in query (array)' => [
145+
'previousLocation' => '/route_with_prop/foo',
146+
'expectedLocation' => '/route_with_prop/foo?arrayProp%5B0%5D=hello&arrayProp%5B1%5D=world',
147+
'initialComponentData' => [
148+
'pathProp' => 'foo',
149+
],
150+
'args' => [
151+
'propName' => 'arrayProp',
152+
'propValue' => ['hello', 'world'],
153+
],
154+
];
155+
156+
yield 'Change in query (array & alias)' => [
157+
'previousLocation' => '/route_with_prop/foo',
158+
'expectedLocation' => '/route_with_prop/foo?arr_alias%5B0%5D=hello&arr_alias%5B1%5D=world',
159+
'initialComponentData' => [
160+
'pathProp' => 'foo',
161+
],
162+
'args' => [
163+
'propName' => 'arrayPropAlias',
164+
'propValue' => ['hello', 'world'],
165+
],
166+
];
167+
168+
yield 'Change in query (array & field name)' => [
169+
'previousLocation' => '/route_with_prop/foo',
170+
'expectedLocation' => '/route_with_prop/foo?arr_field_name%5B0%5D=hello&arr_field_name%5B1%5D=world',
171+
'initialComponentData' => [
172+
'pathProp' => 'foo',
173+
],
174+
'args' => [
175+
'propName' => 'arrayPropFieldName',
176+
'propValue' => ['hello', 'world'],
177+
],
178+
];
179+
143180
yield 'Changes in props and query' => [
144181
'previousLocation' => '/route_with_prop/foo',
145182
'expectedLocation' => '/route_with_prop/baz?q=foo+bar',

0 commit comments

Comments
 (0)