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

feat(list): Add calciteListDragStart and calciteListDragEnd events for when a drag starts and ends. #8361

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 20 additions & 1 deletion packages/calcite-components/src/components/list/list.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ describe("calcite-list", () => {
calledTimes: number;
newIndex: number;
oldIndex: number;
startCalledTimes: number;
endCalledTimes: number;
}>;

it("works using a mouse", async () => {
Expand All @@ -670,11 +672,19 @@ describe("calcite-list", () => {
testWindow.calledTimes = 0;
testWindow.newIndex = -1;
testWindow.oldIndex = -1;
testWindow.startCalledTimes = 0;
testWindow.endCalledTimes = 0;
list.addEventListener("calciteListOrderChange", (event: CustomEvent<DragDetail>) => {
testWindow.calledTimes++;
testWindow.newIndex = event?.detail?.newIndex;
testWindow.oldIndex = event?.detail?.oldIndex;
});
list.addEventListener("calciteListDragEnd", () => {
testWindow.endCalledTimes++;
});
list.addEventListener("calciteListDragStart", () => {
testWindow.startCalledTimes++;
});
});

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

const results = await page.evaluate(() => {
const testWindow = window as TestWindow;
return { calledTimes: testWindow.calledTimes, oldIndex: testWindow.oldIndex, newIndex: testWindow.newIndex };

return {
calledTimes: testWindow.calledTimes,
oldIndex: testWindow.oldIndex,
newIndex: testWindow.newIndex,
endCalledTimes: testWindow.endCalledTimes,
startCalledTimes: testWindow.startCalledTimes,
};
});

expect(results.calledTimes).toBe(1);
expect(results.startCalledTimes).toBe(1);
expect(results.endCalledTimes).toBe(1);
expect(results.oldIndex).toBe(0);
expect(results.newIndex).toBe(1);
});
Expand Down
22 changes: 20 additions & 2 deletions packages/calcite-components/src/components/list/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ export class List
*/
@Event({ cancelable: false }) calciteListChange: EventEmitter<void>;

/**
* Emits when the component's dragging has ended.
*/
@Event({ cancelable: false }) calciteListDragEnd: EventEmitter<ListDragDetail>;

/**
* Emits when the component's dragging has started.
*/
@Event({ cancelable: false }) calciteListDragStart: EventEmitter<ListDragDetail>;

/**
* Emits when the component's filter has changed.
*/
Expand Down Expand Up @@ -598,14 +608,22 @@ export class List
connectSortableComponent(this);
}

onDragStart(): void {
onGlobalDragStart(): void {
this.disconnectObserver();
}

onDragEnd(): void {
onGlobalDragEnd(): void {
this.connectObserver();
}

onDragEnd(): void {
this.calciteListDragEnd.emit();
}

onDragStart(): void {
this.calciteListDragStart.emit();
}

onDragSort(detail: ListDragDetail): void {
this.setParentList();
this.updateListItems();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,18 @@ export class SortableList implements InteractiveComponent, SortableComponent {
//
// --------------------------------------------------------------------------

onDragStart(): void {
onGlobalDragStart(): void {
this.endObserving();
}

onDragEnd(): void {
onGlobalDragEnd(): void {
this.beginObserving();
}

onDragEnd(): void {}

onDragStart(): void {}

onDragSort(): void {
this.items = Array.from(this.el.children);
this.calciteListOrderChange.emit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,18 @@ export class ValueList<
//
// --------------------------------------------------------------------------

onDragStart(): void {
onGlobalDragStart(): void {
cleanUpObserver.call(this);
}

onDragEnd(): void {
onGlobalDragEnd(): void {
initializeObserver.call(this);
}

onDragEnd(): void {}

onDragStart(): void {}

onDragSort(): void {
this.items = Array.from(this.el.querySelectorAll<ItemElement>("calcite-value-list-item"));
const values = this.items.map((item) => item.value);
Expand Down
40 changes: 26 additions & 14 deletions packages/calcite-components/src/utils/sortableComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,29 @@ export interface SortableComponent {
canPut: (detail: DragDetail) => boolean;

/**
* Called by any change to the list (add / update / remove).
* Called when any sortable component drag starts.
*/
onDragSort: (detail: DragDetail) => void;
onGlobalDragStart: () => void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the global drag start/end callbacks for internal purposes? If so, maybe we could doc that any public drag events should emit within onDragStart/onDragEnd?


/**
* Called when any sortable component drag ends.
*/
onGlobalDragEnd: () => void;

/**
* Called when a sortable component drag starts.
* Called when a component's dragging ends.
*/
onDragStart: () => void;
onDragEnd: (detail: DragDetail) => void;

/**
* Called when a sortable component drag ends.
* Called when a component's dragging starts.
*/
onDragEnd: () => void;
onDragStart: (detail: DragDetail) => void;

/**
* Called by any change to the list (add / update / remove).
*/
onDragSort: (detail: DragDetail) => void;
}

export interface SortableComponentItem {
Expand Down Expand Up @@ -119,13 +129,15 @@ export function connectSortableComponent(component: SortableComponent): void {
}),
handle,
filter: "[drag-disabled]",
onStart: () => {
onStart: ({ from: fromEl, item: dragEl, to: toEl, newIndex, oldIndex }) => {
dragState.active = true;
onDragStart();
onGlobalDragStart();
component.onDragStart({ fromEl, dragEl, toEl, newIndex, oldIndex });
},
onEnd: () => {
onEnd: ({ from: fromEl, item: dragEl, to: toEl, newIndex, oldIndex }) => {
dragState.active = false;
onDragEnd();
onGlobalDragEnd();
component.onDragEnd({ fromEl, dragEl, toEl, newIndex, oldIndex });
},
onSort: ({ from: fromEl, item: dragEl, to: toEl, newIndex, oldIndex }) => {
component.onDragSort({ fromEl, dragEl, toEl, newIndex, oldIndex });
Expand Down Expand Up @@ -157,10 +169,10 @@ export function dragActive(component: SortableComponent): boolean {
return component.dragEnabled && dragState.active;
}

function onDragStart(): void {
Array.from(sortableComponentSet).forEach((component) => component.onDragStart());
function onGlobalDragStart(): void {
Array.from(sortableComponentSet).forEach((component) => component.onGlobalDragStart());
}

function onDragEnd(): void {
Array.from(sortableComponentSet).forEach((component) => component.onDragEnd());
function onGlobalDragEnd(): void {
Array.from(sortableComponentSet).forEach((component) => component.onGlobalDragEnd());
}
Loading