Skip to content

Commit 28883f7

Browse files
committed
[Live] Updating docs for removal of data-action-name in favor or Stimulus action params
1 parent 70f8ca4 commit 28883f7

File tree

2 files changed

+83
-32
lines changed

2 files changed

+83
-32
lines changed

src/LiveComponent/CHANGELOG.md

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

3+
## 2.16.0
4+
5+
- [BC BREAK] The `data-action-name` attribute behavior was removed in favor of
6+
using Stimulus "action parameters" and `data-live-action-param`. This is a
7+
breaking change if you were using the `data-action-name` attribute directly
8+
in your templates.
9+
10+
To upgrade your application, follow these changes:
11+
12+
```diff
13+
<button
14+
data-action="live#action"
15+
- data-action-name="debounce(300)|save"
16+
+ data-live-action-param="debounce(300)|save"
17+
>Save</button>
18+
```
19+
20+
To pass arguments to an action, also use the Stimulus "action parameters" syntax:
21+
22+
```diff
23+
<button
24+
data-action="live#action"
25+
- data-action-name="addItem(id={{ item.id }}, itemName=CustomItem)"
26+
+ data-live-action-param="addItem"
27+
+ data-live-id-param="{{ item.id }}"
28+
+ data-live-item-name-param="CustomItem"
29+
>Add Item</button>
30+
```
31+
32+
Additionally, the `prevent` modifier (e.g. `prevent|save`) was removed. Replace
33+
this with the standard Stimulus `:prevent` action option:
34+
35+
```diff
36+
<button
37+
- data-action="live#action
38+
+ data-action="live#action:prevent"
39+
- data-action-name="prevent|save"
40+
+ data-live-action-param="save"
41+
>Save</button>
42+
```
43+
344
## 2.15.0
445

546
- [BC BREAK] The `data-live-id` attribute was changed to `id` #1484

src/LiveComponent/doc/index.rst

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,14 +1036,21 @@ the work::
10361036
// ...
10371037
}
10381038

1039-
To call this, add ``data-action="live#action"`` and ``data-action-name``
1040-
to an element (e.g. a button or form):
1039+
.. versionadded:: 2.14
1040+
1041+
The ``data-live-action-param`` attribute way of specifying the action
1042+
was added in Live Components 2.14. Previously, this was done with
1043+
``data-live-action-name``.
1044+
1045+
To call this, trigger the ``action`` method on the ``live`` Stimulus
1046+
controller and pass ``resetMax`` as a `Stimulus action parameter`_ called
1047+
``action``:
10411048

10421049
.. code-block:: html+twig
10431050

10441051
<button
10451052
data-action="live#action"
1046-
data-action-name="resetMax"
1053+
data-live-action-param="resetMax"
10471054
>Reset Min/Max</button>
10481055

10491056
Done! When the user clicks this button, a POST request will be sent that
@@ -1058,14 +1065,12 @@ You can also add several "modifiers" to the action:
10581065
<form>
10591066
<button
10601067
data-action="live#action"
1061-
data-action-name="prevent|debounce(300)|save"
1068+
data-live-action-param="debounce(300)|save"
10621069
>Save</button>
10631070
</form>
10641071

1065-
The ``prevent`` modifier would prevent the form from submitting
1066-
(``event.preventDefault()``). The ``debounce(300)`` modifier will add
1067-
300ms of "debouncing" before the action is executed. In other words, if
1068-
you click really fast 5 times, only one Ajax request will be made!
1072+
The ``debounce(300)`` adds 300ms of "debouncing" before the action is executed.
1073+
In other words, if you click really fast 5 times, only one Ajax request will be made!
10691074

10701075
Actions & Services
10711076
~~~~~~~~~~~~~~~~~~
@@ -1099,22 +1104,28 @@ This means that, for example, you can use action autowiring::
10991104
Actions & Arguments
11001105
~~~~~~~~~~~~~~~~~~~
11011106

1102-
.. versionadded:: 2.1
1107+
.. versionadded:: 2.14
11031108

1104-
The ability to pass arguments to actions was added in version 2.1.
1109+
The ``data-live-{NAME}-param`` attribute way of specifying action
1110+
arguments was added in Live Components 2.14. Previously, this was done
1111+
inside the ``data-live-action-name`` attribute.
11051112

1106-
You can also provide custom arguments to your action:
1113+
You can also pass arguments to your action by adding each as a
1114+
`Stimulus action parameter`_:
11071115

11081116
.. code-block:: html+twig
11091117

11101118
<form>
11111119
<button
11121120
data-action="live#action"
1113-
data-action-name="addItem(id={{ item.id }}, itemName=CustomItem)"
1121+
data-live-action-param="addItem"
1122+
1123+
data-live-id-param="{{ item.id }}"
1124+
data-live-item-name-param="CustomItem"
11141125
>Add Item</button>
11151126
</form>
11161127

1117-
In your component, to allow each argument to be passed, we need to add
1128+
In your component, to allow each argument to be passed, add
11181129
the ``#[LiveArg()]`` attribute::
11191130

11201131
// src/Components/ItemList.php
@@ -1135,10 +1146,6 @@ the ``#[LiveArg()]`` attribute::
11351146
}
11361147
}
11371148

1138-
Normally, the argument name in PHP - e.g. ``$id`` - should match the
1139-
argument name used in Twig ``id={{ item.id }}``. But if they don't
1140-
match, you can pass an argument to ``LiveArg``, like we did with ``itemName``.
1141-
11421149
Actions and CSRF Protection
11431150
~~~~~~~~~~~~~~~~~~~~~~~~~~~
11441151

@@ -1216,10 +1223,11 @@ to handle the files and tell the component when the file should be sent:
12161223

12171224
.. code-block:: html+twig
12181225

1219-
<p>
1220-
<input type="file" name="my_file" />
1221-
<button data-action="live#action" data-action-name="files|my_action" />
1222-
</p>
1226+
<input type="file" name="my_file" />
1227+
<button
1228+
data-action="live#action"
1229+
data-live-action-param="files|my_action"
1230+
/>
12231231

12241232
To send a file (or files) with an action use ``files`` modifier.
12251233
Without an argument it will send all pending files to your action.
@@ -1233,11 +1241,11 @@ You can also specify a modifier parameter to choose which files should be upload
12331241
<input type="file" name="multiple[]" multiple />
12341242

12351243
{# Send only file from first input #}
1236-
<button data-action="live#action" data-action-name="files(my_file)|myAction" />
1244+
<button data-action="live#action" data-live-action-param="files(my_file)|myAction" />
12371245
{# You can chain modifiers to send multiple files #}
1238-
<button data-action="live#action" data-action-name="files(my_file)|files(multiple[])|myAction" />
1246+
<button data-action="live#action" data-live-action-param="files(my_file)|files(multiple[])|myAction" />
12391247
{# Or send all pending files #}
1240-
<button data-action="live#action" data-action-name="files|myAction" />
1248+
<button data-action="live#action" data-live-action-param="files|myAction" />
12411249
</p>
12421250

12431251
The files will be available in a regular ``$request->files`` files bag::
@@ -1456,8 +1464,8 @@ Next, tell the ``form`` element to use this action:
14561464
14571465
{{ form_start(form, {
14581466
attr: {
1459-
'data-action': 'live#action',
1460-
'data-action-name': 'prevent|save'
1467+
'data-action': 'live#action:prevent',
1468+
'data-live-action-param': 'save'
14611469
}
14621470
}) }}
14631471
@@ -1757,7 +1765,8 @@ and ``removeComment()`` actions:
17571765
{% for key, commentForm in form.comments %}
17581766
<button
17591767
data-action="live#action"
1760-
data-action-name="removeComment(index={{ key }})"
1768+
data-live-action-param="removeComment"
1769+
data-live-index-param="{{ key }}"
17611770
type="button"
17621771
>X</button>
17631772

@@ -1769,7 +1778,7 @@ and ``removeComment()`` actions:
17691778

17701779
<button
17711780
data-action="live#action"
1772-
data-action-name="addComment"
1781+
data-live-action-param="addComment"
17731782
type="button"
17741783
>+ Add Comment</button>
17751784

@@ -2172,8 +2181,8 @@ re-rendered. In your template, render errors using an ``_errors`` variable:
21722181

21732182
<button
21742183
type="submit"
2175-
data-action="live#action"
2176-
data-action-name="prevent|save"
2184+
data-action="live#action:prevent"
2185+
data-live-action-param="save"
21772186
>Save</button>
21782187

21792188
Once a component has been validated, the component will "remember" that
@@ -2766,7 +2775,7 @@ suppose your child component has:
27662775

27672776
.. code-block:: html
27682777

2769-
<button data-action="live#action" data-action-name="save">Save</button>
2778+
<button data-action="live#action" data-live-action-param="save">Save</button>
27702779

27712780
When the user clicks that button, it will attempt to call the ``save``
27722781
action in the *child* component only, even if the ``save`` action
@@ -2930,7 +2939,7 @@ In the ``EditPost`` template, you render the
29302939

29312940
<button
29322941
data-action="live#action"
2933-
data-action-name="save"
2942+
data-live-action-param="save"
29342943
>Save</button>
29352944
</form>
29362945
</div>
@@ -3467,3 +3476,4 @@ bound to Symfony's BC policy for the moment.
34673476
.. _`PostMount hook`: https://symfony.com/bundles/ux-twig-component/current/index.html#postmount-hook
34683477
.. _`validation groups`: https://symfony.com/doc/current/form/validation_groups.html
34693478
.. _morphing library: https://github.com/bigskysoftware/idiomorph
3479+
.. _`Stimulus action parameter`: https://stimulus.hotwired.dev/reference/actions#action-parameters

0 commit comments

Comments
 (0)