Skip to content

Commit 4359b5e

Browse files
authored
Merge pull request #165 from minop1205/feature-relative-index
feat: add relativeIndex to options passed onDrop callback
2 parents b1e3765 + 4a98ce7 commit 4359b5e

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## 3.3.0
4+
5+
_Nov 2, 2022_
6+
7+
### Added
8+
9+
- Added `relativeIndex` to options passed `onDrop` callback.
10+
311
## 3.2.2
412

513
_Oct 31, 2022_

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ The arguments passed to the onDrop callback function are as follows
379379
| options.dropTargetId | `number` \| `string` | node id of the drop destination.<br>If the drop destination is the root node, it will be the value of the `rootId` property. |
380380
| options.dragSource | `object` | node item of the dragging source.<br>If the drag source is an element external to `DndProvider` or a file or selected text, these will be `undefined`. |
381381
| options.dropTarget | `object` \| `undefined` | node item of the drop destination.<br>If the drop destination is the root node, it will be `undefined` |
382-
| options.destinationIndex | `number` \| `undefined` | If the `sort` property is `false`, the insertion destination index value of dragSource is given. Otherwise, it will be `undefined`. |
382+
| options.destinationIndex | `number` \| `undefined` | When the `sort` property is `false`, it indicates the index to which the drag source in the tree data array should be moved.<br>When the `sort` property is `true`, it will be `undefined`. |
383+
| options.relativeIndex | `number` \| `undefined` | When the `sort` property is `false`, it indicates the relative index of the drop destination with respect to the parent node.<br>When the `sort` property is `true`, it will be `undefined`. |
383384
| options.monitor | `object` | Provides various methods for accessing react-dnd's internal state, e.g. for accessing drag sources from outside DndProvider.<br>See this [definition](https://github.com/react-dnd/react-dnd/blob/f835e26d81094b4aebc9d5b5f7b172beaeddf4b0/packages/dnd-core/src/interfaces.ts#L26) for details. |
384385

385386
### canDrop callback

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@minoru/react-dnd-treeview",
33
"description": "A draggable / droppable React-based treeview component.",
4-
"version": "3.2.2",
4+
"version": "3.3.0",
55
"license": "MIT",
66
"repository": {
77
"type": "git",

src/providers/TreeProvider.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const TreeProvider = <T,>(props: Props<T>): ReactElement => {
5151
initialOpen: false,
5252
...props,
5353
openIds,
54-
onDrop: (dragSource, dropTargetId, index) => {
54+
onDrop: (dragSource, dropTargetId, placeholderIndex) => {
5555
// if dragSource is null,
5656
// it means that the drop is from the outside of the react-dnd.
5757
if (!dragSource) {
@@ -65,8 +65,10 @@ export const TreeProvider = <T,>(props: Props<T>): ReactElement => {
6565
options.destinationIndex = getDestIndex(
6666
props.tree,
6767
dropTargetId,
68-
index
68+
placeholderIndex
6969
);
70+
71+
options.relativeIndex = placeholderIndex;
7072
}
7173

7274
props.onDrop(props.tree, options);
@@ -92,11 +94,17 @@ export const TreeProvider = <T,>(props: Props<T>): ReactElement => {
9294
tree,
9395
dragSource.id,
9496
dropTargetId,
95-
index
97+
placeholderIndex
9698
);
9799
options.destinationIndex = destIndex;
100+
options.relativeIndex = placeholderIndex;
98101
props.onDrop(
99-
mutateTreeWithIndex<T>(tree, dragSource.id, dropTargetId, index),
102+
mutateTreeWithIndex<T>(
103+
tree,
104+
dragSource.id,
105+
dropTargetId,
106+
placeholderIndex
107+
),
100108
options
101109
);
102110

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export type DropOptions<T = unknown> = {
159159
dragSource?: NodeModel<T>;
160160
dropTarget?: NodeModel<T>;
161161
destinationIndex?: number;
162+
relativeIndex?: number;
162163
monitor: DragDropMonitor;
163164
};
164165

0 commit comments

Comments
 (0)