-
-
Notifications
You must be signed in to change notification settings - Fork 356
[LiveComponent] add LiveProp name to modifier function #2652
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
base: 2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2651,6 +2651,47 @@ This way you can also use the component multiple times in the same page and avoi | |
<twig:SearchModule alias="q1" /> | ||
<twig:SearchModule alias="q2" /> | ||
|
||
.. versionadded:: 2.25 | ||
|
||
The property name is passed into the modifier function since LiveComponents 2.25. | ||
|
||
The ``modifier`` function can also take the name of the property as a secondary parameter. | ||
It can be used to perform more generic operations inside of the modifier that can be re-used for multiple props:: | ||
|
||
abstract class AbstractSearchModule | ||
{ | ||
#[LiveProp(writable: true, url: true, modifier: 'modifyQueryProp')] | ||
public string $query = ''; | ||
|
||
protected string $urlPrefix = ''; | ||
|
||
public function modifyQueryProp(LiveProp $liveProp, string $propName): LiveProp | ||
{ | ||
if ($this->urlPrefix) { | ||
return $liveProp->withUrl(new UrlMapping(as: $this->urlPrefix.'-'.$propName)); | ||
} | ||
return $liveProp; | ||
} | ||
} | ||
|
||
#[AsLiveComponent] | ||
class ImportantSearchModule extends AbstractSearchModule | ||
{ | ||
} | ||
|
||
#[AsLiveComponent] | ||
class SecondarySearchModule extends AbstractSearchModule | ||
{ | ||
protected string $urlPrefix = 'secondary'; | ||
} | ||
Comment on lines
+2661
to
+2686
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering.. could we find use an example more focused on the feature than the PHP code structure abstract/extends here ? I was thinking something like "imagine you have a CMS theme editor with a split view allowing you to adjust your design both in light and dark mode. ... You could use a ColorTheme LiveComponent for both, but then [describe problem]
Thanks to the second parameter of YYY, you can dynamically prefix [...].
Now your query parameters will be: XXX ZZZ It even works with the as: property .. // query param will be ZZZ
#[LiveProp(url: true, as: 'bg' modifier: 'prefixParam')]
public string $background; wdyt ? |
||
|
||
.. code-block:: html+twig | ||
|
||
<twig:ImportantSearchModule /> | ||
<twig:SecondarySearchModule /> | ||
|
||
The ``query`` value will appear in the URL like ``/search?query=my+important+query&secondary-query=my+secondary+query``. | ||
|
||
Validating the Query Parameter Values | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,7 +135,7 @@ public function withModifier(object $component): self | |
throw new \LogicException(\sprintf('Method "%s::%s()" given in LiveProp "modifier" does not exist.', $component::class, $modifier)); | ||
} | ||
|
||
$modifiedLiveProp = $component->{$modifier}($this->liveProp); | ||
$modifiedLiveProp = $component->{$modifier}($this->liveProp, $this->getName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a change like this, a test would be needed. Especially for things related to query, URL, etc. (The fact tests do pass after your changes does not proves it does work (nor that our current suite of test is good enough, to be fair)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added to the existing test, I don't see the need to make a separate test for just this param unless there are some edge cases that aren't entirely obvious to me at this time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's test a page with two instances of same component, different prefix used in the modifier ... and check that the correct one as the good value and not the other :) |
||
if (!$modifiedLiveProp instanceof LiveProp) { | ||
throw new \LogicException(\sprintf('Method "%s::%s()" should return an instance of "%s" (given: "%s").', $component::class, $modifier, LiveProp::class, get_debug_type($modifiedLiveProp))); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This'd have to be updated to the actual release version, I just took the next one.