Skip to content

Commit 19fe704

Browse files
committed
[LiveComponent] Fix new URL calculation when having #[LiveProp] with custom modifier which returns a new LiveProp
1 parent 07e73bf commit 19fe704

File tree

6 files changed

+29
-5
lines changed

6 files changed

+29
-5
lines changed

src/LiveComponent/src/EventListener/LiveUrlSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private function getLiveProps(MountedComponent $mounted): array
9090
'query' => [],
9191
];
9292

93-
foreach ($metadata->getAllUrlMappings() as $name => $urlMapping) {
93+
foreach ($metadata->getAllUrlMappings($mounted->getComponent()) as $name => $urlMapping) {
9494
if (isset($values[$name]) && $urlMapping) {
9595
$urlLiveProps[$urlMapping->mapPath ? 'path' : 'query'][$urlMapping->as ?? $name] = $values[$name];
9696
}

src/LiveComponent/src/Metadata/LiveComponentMetadata.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public function getOnlyPropsThatAcceptUpdatesFromParent(array $inputProps): arra
6969
/**
7070
* @return UrlMapping[]
7171
*/
72-
public function getAllUrlMappings(): iterable
72+
public function getAllUrlMappings(object $component): iterable
7373
{
7474
$urlMappings = [];
75-
foreach ($this->livePropsMetadata as $livePropMetadata) {
75+
foreach ($this->getAllLivePropsMetadata($component) as $livePropMetadata) {
7676
if ($livePropMetadata->urlMapping()) {
7777
$urlMappings[$livePropMetadata->getName()] = $livePropMetadata->urlMapping();
7878
}
@@ -81,7 +81,7 @@ public function getAllUrlMappings(): iterable
8181
return $urlMappings;
8282
}
8383

84-
public function hasQueryStringBindings($component): bool
84+
public function hasQueryStringBindings(object $component): bool
8585
{
8686
foreach ($this->getAllLivePropsMetadata($component) as $livePropMetadata) {
8787
if ($livePropMetadata->urlMapping()) {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ class ComponentWithUrlBoundProps
5454
#[LiveProp]
5555
public ?bool $maybeBoundPropInUrl = false;
5656

57+
#[LiveProp(url: new UrlMapping(as: 'p'), modifier: 'modifyPropertyWithModifierAndAlias')]
58+
public ?string $propertyWithModifierAndAlias = null;
59+
60+
public function modifyPropertyWithModifierAndAlias(LiveProp $liveProp): LiveProp
61+
{
62+
$urlMapping = $liveProp->url();
63+
if (!$urlMapping instanceof UrlMapping) {
64+
return $liveProp;
65+
}
66+
67+
return $liveProp->withUrl(new UrlMapping(as: 'alias_' . $urlMapping->as));
68+
}
69+
5770
public function getField2(): string
5871
{
5972
return 'field2';

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public function testQueryStringMappingAttribute()
142142
'pathProp' => ['name' => 'pathProp'],
143143
'pathPropWithAlias' => ['name' => 'pathAlias'],
144144
'objectPropWithSerializerForHydration' => ['name' => 'objectPropWithSerializerForHydration'],
145+
'propertyWithModifierAndAlias' => ['name' => 'alias_p'],
145146
];
146147

147148
$this->assertEquals($expected, $queryMapping);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public function getTestData(): iterable
112112
'propValue' => 'search',
113113
],
114114
];
115+
115116
$address = new Address();
116117
$address->address = '123 Main St';
117118
$address->city = 'Anytown';
@@ -126,6 +127,15 @@ public function getTestData(): iterable
126127
'propValue' => '3',
127128
],
128129
];
130+
131+
yield 'query with alias ("p") and modifier (prefix by "alias_")' => [
132+
'previousLocation' => '/',
133+
'expectedLocation' => '/?alias_p=test',
134+
'props' => [
135+
'propertyWithModifierAndAlias' => 'test',
136+
],
137+
'args' => [],
138+
];
129139
}
130140

131141
/**

src/LiveComponent/tests/Unit/Metadata/LiveComponentMetadataTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testGetAllUrlMappings()
4848
new LivePropMetadata('aliasUrlMapping', new LiveProp(url: $aliasUrlMapping), null, false, false, null),
4949
];
5050
$liveComponentMetadata = new LiveComponentMetadata(new ComponentMetadata([]), $propMetadas);
51-
$urlMappings = $liveComponentMetadata->getAllUrlMappings();
51+
$urlMappings = $liveComponentMetadata->getAllUrlMappings(new \stdClass());
5252
$this->assertCount(2, $urlMappings);
5353
$this->assertInstanceOf(UrlMapping::class, $urlMappings['basicUrlMapping']);
5454
$this->assertEquals($aliasUrlMapping, $urlMappings['aliasUrlMapping']);

0 commit comments

Comments
 (0)