Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/three-plants-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-movable": minor
---

feat: implement afterDrag
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ __diff_output__
/playwright-report/
/blob-report/
/playwright/.cache/
/e2e/**/*.png
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ An array of values. The value can be a string or any more complex object. The le
### onChange

```ts
onChange: (meta: { oldIndex: number; newIndex: number, targetRect: ClientRect }) => void
onChange: (meta: { oldIndex: number; newIndex: number, targetRect: DOMRect }) => void
```

Called when the item is dropped to a new location:
Expand All @@ -144,6 +144,14 @@ beforeDrag?: (params: { elements: Element[]; index: number }) => void;

Called when a valid drag is initiated. It provides a direct access to all list DOM elements and the index of dragged item. This can be useful when you need to do some upfront measurements like when building a [table with variable column widths](https://react-movable.netlify.app/?story=list--table-auto-cell-widths).

### afterDrag

```ts
afterDrag?: (params: { elements: Element[]; oldIndex: number; newIndex: number }) => void;
```

Called when a drag is completed. It provides a direct access to all list DOM elements, the old as well as the new index of the dragged item. This can be useful when you need to perform some cleanup, e.g. in conjunction with `beforeDrag`. Please note this is different to `onChange`, which will _only_ fire when the drag results in a changed order.

### removableByMove

```ts
Expand Down
3 changes: 3 additions & 0 deletions examples/TableAuto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const TableAuto: React.FC = () => {
);
setWidths(widths);
}}
afterDrag={() => {
setWidths([]);
}}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Demonstrate usage for cleanup.

values={items}
onChange={({ oldIndex, newIndex }) =>
setItems(arrayMove(items, oldIndex, newIndex))
Expand Down
22 changes: 16 additions & 6 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,26 @@ class List<Value = string> extends React.Component<IProps<Value>> {
finishDrop = () => {
const removeItem =
this.props.removableByMove && this.isDraggedItemOutOfBounds();
if (
removeItem ||
(this.afterIndex > -2 && this.state.itemDragged !== this.afterIndex)
) {
const oldIndex = this.state.itemDragged;
const hasChanged = this.afterIndex > -2 && oldIndex !== this.afterIndex;
const newIndex = hasChanged
? removeItem
? -1
: Math.max(this.afterIndex, 0)
: oldIndex;
if (removeItem || hasChanged) {
this.props.onChange({
oldIndex: this.state.itemDragged,
newIndex: removeItem ? -1 : Math.max(this.afterIndex, 0),
oldIndex,
newIndex,
targetRect: this.ghostRef.current!.getBoundingClientRect(),
});
}
this.props.afterDrag &&
this.props.afterDrag({
elements: this.getChildren(),
oldIndex,
newIndex,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Execute afterDrag after onChange, without further conditions.

this.getChildren().forEach((item) => {
setItemTransition(item, 0);
transformItem(item, null);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
RenderItemParams,
RenderListParams,
BeforeDragParams,
AfterDragParams,
OnChangeMeta,
IProps,
TEvent,
Expand All @@ -16,6 +17,7 @@ export type {
RenderItemParams,
RenderListParams,
BeforeDragParams,
AfterDragParams,
OnChangeMeta,
IProps,
TEvent,
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,22 @@ export interface BeforeDragParams {
index: number;
}

export interface AfterDragParams {
elements: Element[];
oldIndex: number;
newIndex: number;
}

export interface OnChangeMeta {
oldIndex: number;
newIndex: number;
targetRect: ClientRect;
targetRect: DOMRect;
}

export interface IProps<Value> {
disabled?: boolean;
beforeDrag?: (params: BeforeDragParams) => void;
afterDrag?: (params: AfterDragParams) => void;
renderItem: (params: RenderItemParams<Value>) => React.ReactNode;
renderList: (props: RenderListParams) => React.ReactNode;
values: Value[];
Expand Down
Loading