Skip to content

Commit d815a1b

Browse files
committed
feature #2744 [LiveComponent] LiveProp: Pass the property name as second parameter of the modifier callback (jannes-io)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] `LiveProp`: Pass the property name as second parameter of the `modifier` callback supersedes #2652 Commits ------- 8fb5245 [LiveComponent] Add property name to modifier function
2 parents 0cf3a6d + 8fb5245 commit d815a1b

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/LiveComponent/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.26.0
4+
5+
- `LiveProp`: Pass the property name as second parameter of the `modifier` callable
6+
37
## 2.25.0
48

59
- Add support for [Symfony UID](https://symfony.com/doc/current/components/uid.html) hydration/dehydration

src/LiveComponent/doc/index.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,47 @@ This way you can also use the component multiple times in the same page and avoi
26252625
<twig:SearchModule alias="q1" />
26262626
<twig:SearchModule alias="q2" />
26272627

2628+
.. versionadded:: 2.26
2629+
2630+
The property name is passed into the modifier function since LiveComponents 2.26.
2631+
2632+
The ``modifier`` function can also take the name of the property as a secondary parameter.
2633+
It can be used to perform more generic operations inside of the modifier that can be re-used for multiple props::
2634+
2635+
abstract class AbstractSearchModule
2636+
{
2637+
#[LiveProp(writable: true, url: true, modifier: 'modifyQueryProp')]
2638+
public string $query = '';
2639+
2640+
protected string $urlPrefix = '';
2641+
2642+
public function modifyQueryProp(LiveProp $liveProp, string $propName): LiveProp
2643+
{
2644+
if ($this->urlPrefix) {
2645+
return $liveProp->withUrl(new UrlMapping(as: $this->urlPrefix.'-'.$propName));
2646+
}
2647+
return $liveProp;
2648+
}
2649+
}
2650+
2651+
#[AsLiveComponent]
2652+
class ImportantSearchModule extends AbstractSearchModule
2653+
{
2654+
}
2655+
2656+
#[AsLiveComponent]
2657+
class SecondarySearchModule extends AbstractSearchModule
2658+
{
2659+
protected string $urlPrefix = 'secondary';
2660+
}
2661+
2662+
.. code-block:: html+twig
2663+
2664+
<twig:ImportantSearchModule />
2665+
<twig:SecondarySearchModule />
2666+
2667+
The ``query`` value will appear in the URL like ``/search?query=my+important+query&secondary-query=my+secondary+query``.
2668+
26282669
Validating the Query Parameter Values
26292670
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26302671

src/LiveComponent/src/Metadata/LivePropMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function withModifier(object $component): self
135135
throw new \LogicException(\sprintf('Method "%s::%s()" given in LiveProp "modifier" does not exist.', $component::class, $modifier));
136136
}
137137

138-
$modifiedLiveProp = $component->{$modifier}($this->liveProp);
138+
$modifiedLiveProp = $component->{$modifier}($this->liveProp, $this->getName());
139139
if (!$modifiedLiveProp instanceof LiveProp) {
140140
throw new \LogicException(\sprintf('Method "%s::%s()" should return an instance of "%s" (given: "%s").', $component::class, $modifier, LiveProp::class, get_debug_type($modifiedLiveProp)));
141141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testWithModifier()
2929
$component
3030
->expects($this->once())
3131
->method('modifyProp')
32-
->with($liveProp)
32+
->with($liveProp, 'propWithModifier')
3333
->willReturn($liveProp->withFieldName('customField'));
3434

3535
$livePropMetadata = $livePropMetadata->withModifier($component);

0 commit comments

Comments
 (0)