Skip to content
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

docs(reorder): assign stable identity to loop items #3239

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/api/reorder.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ import Wrapper from '@site/static/usage/v7/reorder/wrapper/index.md';

## Updating Data

When the `complete` method is called on the reorder group with no parameters, the DOM nodes will be reordered. If the items are rendered from an array of data that needs to be sorted, this can result in the data and DOM being out of sync. In order to sort the array upon completion of the reorder, the array should be passed as a parameter to the `complete` method. The `complete` method will sort the array and return it so it can be reassigned.
When the `complete` method is called on the reorder group with no parameters, the DOM nodes will be reordered. If the items are rendered from an array of data that needs to be sorted, this can result in the data and DOM being out of sync.

In order to sort the array upon completion of the reorder, the array should be passed as a parameter to the `complete` method. The `complete` method will sort the array and return it so it can be reassigned. Note that passing the array will prevent Ionic from reordering the DOM nodes.

In some cases, it may be necessary for an app to reorder both the array and the DOM nodes on its own. If this is required, `false` should be passed as a parameter to the `complete` method. This will prevent Ionic from reordering any DOM nodes inside of the reorder group.

Regardless of the approach taken, a stable identity should be provided to reorder items if provided in a loop. This means using `trackBy` for Angular, and `key` for React and Vue.

import UpdatingData from '@site/static/usage/v7/reorder/updating-data/index.md';

<UpdatingData />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- The reorder gesture is disabled by default, enable it to drag and drop items -->
<!-- Casting $event to $any is a temporary fix for this bug https://github.com/ionic-team/ionic-framework/issues/24245 -->
<ion-reorder-group [disabled]="false" (ionItemReorder)="handleReorder($any($event))">
<ion-item *ngFor="let item of items">
<ion-item *ngFor="let item of items; trackBy: trackItems">
<ion-label> Item {{ item }} </ion-label>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export class ExampleComponent {
// After complete is called the items will be in the new order
console.log('After complete', this.items);
}

trackItems(index: number, itemNumber: number) {
return itemNumber;
}
}
```
2 changes: 1 addition & 1 deletion static/usage/v7/reorder/updating-data/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Example() {
{/* The reorder gesture is disabled by default, enable it to drag and drop items */}
<IonReorderGroup disabled={false} onIonItemReorder={handleReorder}>
{items.map((item) => (
<IonItem>
<IonItem key={item}>
<IonLabel>Item {item}</IonLabel>
<IonReorder slot="end"></IonReorder>
</IonItem>
Expand Down
2 changes: 1 addition & 1 deletion static/usage/v7/reorder/updating-data/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ion-list>
<!-- The reorder gesture is disabled by default, enable it to drag and drop items -->
<ion-reorder-group :disabled="false" @ionItemReorder="handleReorder($event)">
<ion-item v-for="item in items">
<ion-item v-for="item in items" :key="item">
<ion-label> Item {{ item }} </ion-label>
<ion-reorder slot="end"></ion-reorder>
</ion-item>
Expand Down
Loading