Build a reorderable list! Each row carries its own Up and Down buttons that move that row one position within the list.
The component must render a <ul> with the class items containing one <li> per item. Every <li> must have the class item and a data-id attribute holding that item's id. Inside each <li> render a <span> with the class label holding the item name, a <button> with the class up labeled Up, and a <button> with the class down labeled Down. Both buttons must also carry the item's data-id so the handler knows which row was clicked.
The list always starts with these three items, in this order:
- id
1, nameAlpha - id
2, nameBeta - id
3, nameGamma
Clicking Up swaps that item with the one directly above it. Clicking Down swaps it with the one directly below it. Clicking Up on the first item or Down on the last item does nothing at all — the list must stay exactly as it was.
Reassign a brand new array when you reorder (this.items = next;) so LWC notices the change and re-renders.
Examples:
Initial render:
<ul class="items">
<li class="item" data-id="1"><span class="label">Alpha</span><button class="up" data-id="1">Up</button><button class="down" data-id="1">Down</button></li>
<li class="item" data-id="2">...Beta...</li>
<li class="item" data-id="3">...Gamma...</li>
</ul>
Click Down on item 1 -> order becomes: Beta, Alpha, Gamma (data-ids 2, 1, 3)
Click Up on item 3 -> order becomes: Alpha, Gamma, Beta (data-ids 1, 3, 2)
Click Up on item 1 -> order stays: Alpha, Beta, Gamma (data-ids 1, 2, 3)
Click Down on item 3 -> order stays: Alpha, Beta, Gamma (data-ids 1, 2, 3)