@@ -2639,7 +2639,7 @@ understand which element is which between re-renders (i.e. if something re-order
2639
2639
or removes some of those elements), you can add a ``data-live-id `` attribute to
2640
2640
each element
2641
2641
2642
- .. code-block :: twig
2642
+ .. code-block :: html+ twig
2643
2643
2644
2644
{# templates/components/Invoice.html.twig #}
2645
2645
{% for lineItem in lineItems %}
@@ -2665,9 +2665,9 @@ to that component:
2665
2665
2666
2666
.. code-block :: twig
2667
2667
2668
- {# templates/components/Invoice .html.twig #}
2669
- {% for lineItem in lineItems %}
2670
- {{ component('InvoiceLineItem ', {
2668
+ {# templates/components/InvoiceCreator .html.twig #}
2669
+ {% for lineItem in invoice. lineItems %}
2670
+ {{ component('InvoiceLineItemForm ', {
2671
2671
lineItem: lineItem,
2672
2672
key: lineItem.id,
2673
2673
}) }}
@@ -2681,32 +2681,34 @@ a bit more convenient.
2681
2681
Tricks with a Loop + a "New" Item
2682
2682
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2683
2683
2684
- Let's get fancier. Suppose you're rendering all of the line items
2685
- inside of an `InvoiceCreator ` component. After the loop, you might
2684
+ Let's get fancier. After looping over the current line items, you
2686
2685
decide to render one more component to create a *new * line item.
2687
2686
In that case, you can pass in a ``key `` set to something like ``new_line_item ``:
2688
2687
2689
- .. code-block :: twig
2688
+ .. code-block :: html+ twig
2690
2689
2691
- {{ component('InvoiceLineItem', {
2690
+ {# templates/components/InvoiceCreator.html.twig #}
2691
+ // ... loop and render the existing line item components
2692
+
2693
+ {{ component('InvoiceLineItemForm', {
2692
2694
key: 'new_line_item',
2693
2695
}) }}
2694
2696
2695
- Imagine you also have a ``LiveAction `` inside of ``InvoiceLineItem ``
2697
+ Imagine you also have a ``LiveAction `` inside of ``InvoiceLineItemForm ``
2696
2698
that saves the new line item to the database. To be extra fancy,
2697
2699
it emits a ``lineItem:created `` event to the parent::
2698
2700
2699
- // src/Twig/InvoiceLineItem .php
2701
+ // src/Twig/InvoiceLineItemForm .php
2700
2702
// ...
2701
2703
2702
2704
#[AsLiveComponent]
2703
- final class InvoiceLineItem
2705
+ final class InvoiceLineItemForm
2704
2706
{
2705
2707
// ...
2706
2708
2707
2709
#[LiveProp]
2708
2710
#[Valid]
2709
- public InvoiceLineItem $lineItem;
2711
+ public ? InvoiceLineItem $lineItem = null ;
2710
2712
2711
2713
#[PostMount]
2712
2714
public function postMount(): void
@@ -2760,22 +2762,21 @@ component on the page is the *same* new component that it's about to render. And
2760
2762
because the props passed into that component haven't changed, it doesn't see any
2761
2763
reason to re-render it.
2762
2764
2763
- To fix this, you haev two options:
2765
+ To fix this, you have two options:
2764
2766
2765
2767
1. Make the ``key `` dynamic so it will be different after adding a new item::
2766
2768
2767
2769
.. code-block :: twig
2768
2770
2769
- {{ component('InvoiceLineItem ', {
2770
- key: 'new_line_item_' lineItems|length,
2771
+ {{ component('InvoiceLineItemForm ', {
2772
+ key: 'new_line_item_'~ lineItems|length,
2771
2773
}) }}
2772
2774
2773
- 2. Reset the state of the ``InvoiceLineItem `` component after it's saved::
2774
-
2775
- // src/Twig/InvoiceLineItem.php
2775
+ 2. Reset the state of the ``InvoiceLineItemForm `` component after it's saved::
2776
2776
2777
+ // src/Twig/InvoiceLineItemForm.php
2777
2778
// ...
2778
- class InvoiceLineItem
2779
+ class InvoiceLineItemForm
2779
2780
{
2780
2781
// ...
2781
2782
0 commit comments