Skip to content

Commit 1314605

Browse files
authored
feat(list): Add calciteListDragStart and calciteListDragEnd events for when a drag starts and ends. (#8361)
**Related Issue:** #8367 ## Summary - Adds event `calciteListDragEnd` to `list` - Adds event `calciteListDragStart` to `list` - Refactors global drag event name in `SortableComponent`. - Adds test
1 parent 2e80416 commit 1314605

File tree

5 files changed

+78
-21
lines changed

5 files changed

+78
-21
lines changed

packages/calcite-components/src/components/list/list.e2e.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ describe("calcite-list", () => {
659659
calledTimes: number;
660660
newIndex: number;
661661
oldIndex: number;
662+
startCalledTimes: number;
663+
endCalledTimes: number;
662664
}>;
663665

664666
it("works using a mouse", async () => {
@@ -670,11 +672,19 @@ describe("calcite-list", () => {
670672
testWindow.calledTimes = 0;
671673
testWindow.newIndex = -1;
672674
testWindow.oldIndex = -1;
675+
testWindow.startCalledTimes = 0;
676+
testWindow.endCalledTimes = 0;
673677
list.addEventListener("calciteListOrderChange", (event: CustomEvent<DragDetail>) => {
674678
testWindow.calledTimes++;
675679
testWindow.newIndex = event?.detail?.newIndex;
676680
testWindow.oldIndex = event?.detail?.oldIndex;
677681
});
682+
list.addEventListener("calciteListDragEnd", () => {
683+
testWindow.endCalledTimes++;
684+
});
685+
list.addEventListener("calciteListDragStart", () => {
686+
testWindow.startCalledTimes++;
687+
});
678688
});
679689

680690
await dragAndDrop(
@@ -696,10 +706,19 @@ describe("calcite-list", () => {
696706

697707
const results = await page.evaluate(() => {
698708
const testWindow = window as TestWindow;
699-
return { calledTimes: testWindow.calledTimes, oldIndex: testWindow.oldIndex, newIndex: testWindow.newIndex };
709+
710+
return {
711+
calledTimes: testWindow.calledTimes,
712+
oldIndex: testWindow.oldIndex,
713+
newIndex: testWindow.newIndex,
714+
endCalledTimes: testWindow.endCalledTimes,
715+
startCalledTimes: testWindow.startCalledTimes,
716+
};
700717
});
701718

702719
expect(results.calledTimes).toBe(1);
720+
expect(results.startCalledTimes).toBe(1);
721+
expect(results.endCalledTimes).toBe(1);
703722
expect(results.oldIndex).toBe(0);
704723
expect(results.newIndex).toBe(1);
705724
});

packages/calcite-components/src/components/list/list.tsx

+20-2
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ export class List
220220
*/
221221
@Event({ cancelable: false }) calciteListChange: EventEmitter<void>;
222222

223+
/**
224+
* Emits when the component's dragging has ended.
225+
*/
226+
@Event({ cancelable: false }) calciteListDragEnd: EventEmitter<ListDragDetail>;
227+
228+
/**
229+
* Emits when the component's dragging has started.
230+
*/
231+
@Event({ cancelable: false }) calciteListDragStart: EventEmitter<ListDragDetail>;
232+
223233
/**
224234
* Emits when the component's filter has changed.
225235
*/
@@ -598,14 +608,22 @@ export class List
598608
connectSortableComponent(this);
599609
}
600610

601-
onDragStart(): void {
611+
onGlobalDragStart(): void {
602612
this.disconnectObserver();
603613
}
604614

605-
onDragEnd(): void {
615+
onGlobalDragEnd(): void {
606616
this.connectObserver();
607617
}
608618

619+
onDragEnd(): void {
620+
this.calciteListDragEnd.emit();
621+
}
622+
623+
onDragStart(): void {
624+
this.calciteListDragStart.emit();
625+
}
626+
609627
onDragSort(detail: ListDragDetail): void {
610628
this.setParentList();
611629
this.updateListItems();

packages/calcite-components/src/components/sortable-list/sortable-list.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,18 @@ export class SortableList implements InteractiveComponent, SortableComponent {
146146
//
147147
// --------------------------------------------------------------------------
148148

149-
onDragStart(): void {
149+
onGlobalDragStart(): void {
150150
this.endObserving();
151151
}
152152

153-
onDragEnd(): void {
153+
onGlobalDragEnd(): void {
154154
this.beginObserving();
155155
}
156156

157+
onDragEnd(): void {}
158+
159+
onDragStart(): void {}
160+
157161
onDragSort(): void {
158162
this.items = Array.from(this.el.children);
159163
this.calciteListOrderChange.emit();

packages/calcite-components/src/components/value-list/value-list.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,18 @@ export class ValueList<
333333
//
334334
// --------------------------------------------------------------------------
335335

336-
onDragStart(): void {
336+
onGlobalDragStart(): void {
337337
cleanUpObserver.call(this);
338338
}
339339

340-
onDragEnd(): void {
340+
onGlobalDragEnd(): void {
341341
initializeObserver.call(this);
342342
}
343343

344+
onDragEnd(): void {}
345+
346+
onDragStart(): void {}
347+
344348
onDragSort(): void {
345349
this.items = Array.from(this.el.querySelectorAll<ItemElement>("calcite-value-list-item"));
346350
const values = this.items.map((item) => item.value);

packages/calcite-components/src/utils/sortableComponent.ts

+26-14
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,29 @@ export interface SortableComponent {
6060
canPut: (detail: DragDetail) => boolean;
6161

6262
/**
63-
* Called by any change to the list (add / update / remove).
63+
* Called when any sortable component drag starts. For internal use only. Any public drag events should emit within `onDragStart()`.
6464
*/
65-
onDragSort: (detail: DragDetail) => void;
65+
onGlobalDragStart: () => void;
66+
67+
/**
68+
* Called when any sortable component drag ends. For internal use only. Any public drag events should emit within `onDragEnd()`.
69+
*/
70+
onGlobalDragEnd: () => void;
6671

6772
/**
68-
* Called when a sortable component drag starts.
73+
* Called when a component's dragging ends.
6974
*/
70-
onDragStart: () => void;
75+
onDragEnd: (detail: DragDetail) => void;
7176

7277
/**
73-
* Called when a sortable component drag ends.
78+
* Called when a component's dragging starts.
7479
*/
75-
onDragEnd: () => void;
80+
onDragStart: (detail: DragDetail) => void;
81+
82+
/**
83+
* Called by any change to the list (add / update / remove).
84+
*/
85+
onDragSort: (detail: DragDetail) => void;
7686
}
7787

7888
export interface SortableComponentItem {
@@ -119,13 +129,15 @@ export function connectSortableComponent(component: SortableComponent): void {
119129
}),
120130
handle,
121131
filter: "[drag-disabled]",
122-
onStart: () => {
132+
onStart: ({ from: fromEl, item: dragEl, to: toEl, newIndex, oldIndex }) => {
123133
dragState.active = true;
124-
onDragStart();
134+
onGlobalDragStart();
135+
component.onDragStart({ fromEl, dragEl, toEl, newIndex, oldIndex });
125136
},
126-
onEnd: () => {
137+
onEnd: ({ from: fromEl, item: dragEl, to: toEl, newIndex, oldIndex }) => {
127138
dragState.active = false;
128-
onDragEnd();
139+
onGlobalDragEnd();
140+
component.onDragEnd({ fromEl, dragEl, toEl, newIndex, oldIndex });
129141
},
130142
onSort: ({ from: fromEl, item: dragEl, to: toEl, newIndex, oldIndex }) => {
131143
component.onDragSort({ fromEl, dragEl, toEl, newIndex, oldIndex });
@@ -157,10 +169,10 @@ export function dragActive(component: SortableComponent): boolean {
157169
return component.dragEnabled && dragState.active;
158170
}
159171

160-
function onDragStart(): void {
161-
Array.from(sortableComponentSet).forEach((component) => component.onDragStart());
172+
function onGlobalDragStart(): void {
173+
Array.from(sortableComponentSet).forEach((component) => component.onGlobalDragStart());
162174
}
163175

164-
function onDragEnd(): void {
165-
Array.from(sortableComponentSet).forEach((component) => component.onDragEnd());
176+
function onGlobalDragEnd(): void {
177+
Array.from(sortableComponentSet).forEach((component) => component.onGlobalDragEnd());
166178
}

0 commit comments

Comments
 (0)