Skip to content

Commit 7e6d73b

Browse files
committed
feat(reorder): add applyTo method to ionItemReorder event
This allows to apply reordering inside template without importing any other helper functions
1 parent 0005e34 commit 7e6d73b

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/components/item/item-reorder.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { Content } from '../content/content';
44
import { CSS, zoneRafFrames } from '../../util/dom';
55
import { Item } from './item';
66
import { ItemReorderGesture } from '../item/item-reorder-gesture';
7-
import { isTrueProperty } from '../../util/util';
7+
import { isTrueProperty, reorderArray } from '../../util/util';
88

99

1010
export interface ReorderIndexes {
1111
from: number;
1212
to: number;
13+
applyTo: Function;
1314
}
1415

1516
/**
@@ -118,6 +119,16 @@ export interface ReorderIndexes {
118119
* }
119120
* }
120121
* ```
122+
* Alternatevely you can execute helper function inside template:
123+
*
124+
* ```html
125+
* <ion-list>
126+
* <ion-list-header>Header</ion-list-header>
127+
* <ion-item-group reorder="true" (ionItemReorder)="$event.applyTo(items)">
128+
* <ion-item *ngFor="let item of items">{% raw %}{{ item }}{% endraw %}</ion-item>
129+
* </ion-item-group>
130+
* </ion-list>
131+
* ```
121132
*
122133
* @demo /docs/v2/demos/src/item-reorder/
123134
* @see {@link /docs/v2/components#lists List Component Docs}
@@ -223,10 +234,10 @@ export class ItemReorder {
223234
this.reorderReset();
224235
if (fromIndex !== toIndex) {
225236
this._zone.run(() => {
226-
this.ionItemReorder.emit({
227-
from: fromIndex,
228-
to: toIndex,
229-
});
237+
const indexes = { from: fromIndex, to: toIndex };
238+
this.ionItemReorder.emit(Object.assign({
239+
applyTo: (array) => reorderArray(array, indexes)
240+
}, indexes));
230241
});
231242
}
232243
}
@@ -368,4 +379,3 @@ export function indexForItem(element: any): number {
368379
export function reorderListForItem(element: any): any {
369380
return element['$ionReorderList'];
370381
}
371-

src/components/item/test/reorder/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</button>
2020
</ion-list>
2121

22-
<ion-list [reorder]="isReordering" (ionItemReorder)="reorder($event)">
22+
<ion-list [reorder]="isReordering" (ionItemReorder)="$event.applyTo(items)">
2323
<ion-item-sliding *ngFor="let item of items">
2424
<button ion-item (click)="clickedButton(item)">
2525
<h2>Sliding item {{item}}</h2>

0 commit comments

Comments
 (0)