react-reorder-list 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -4
- package/dist/animation.d.ts +2 -2
- package/dist/animation.js +12 -17
- package/dist/index.d.ts +4 -3
- package/dist/index.js +53 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,8 @@ A simple react component that facilitates the reordering of JSX/HTML elements th
|
|
|
3
3
|
## Features
|
|
4
4
|
- Reorders list of elements using drag and drop.
|
|
5
5
|
- Easy to use
|
|
6
|
-
-
|
|
6
|
+
- Smooth transition using animation.
|
|
7
|
+
- Listen to children updates. See [listen to children updates](#listen-to-children-updates)
|
|
7
8
|
- Handles nested lists easily. See [nested list usage](#nested-list-usage)
|
|
8
9
|
## Installation
|
|
9
10
|
To install react-reorder-list
|
|
@@ -34,7 +35,7 @@ export default function App() {
|
|
|
34
35
|
return <ReorderList>
|
|
35
36
|
{[0, 1, 2, 3, 4].map(i => {
|
|
36
37
|
{/* Having a unique key is important */}
|
|
37
|
-
return <div key={i}>{i}</div>
|
|
38
|
+
return <div key={i}>Item {i}</div>
|
|
38
39
|
})}
|
|
39
40
|
</ReorderList>
|
|
40
41
|
}
|
|
@@ -63,6 +64,40 @@ export default function App() {
|
|
|
63
64
|
</ReorderList>
|
|
64
65
|
}
|
|
65
66
|
```
|
|
67
|
+
#### Listen to Children Updates
|
|
68
|
+
`<ReorderList>` can listen to updates to it's children components using the `watchChildrenUpdates` prop as shown below.
|
|
69
|
+
|
|
70
|
+
If set to `true`, updates to children like state changes, additions/omissions of children components will reflect in real time. Note that if an item is being dragged and an update occurs at the moment, that item will be placed at respective location and `onPositionChange` will be called to prevent any inconsistency.
|
|
71
|
+
|
|
72
|
+
If set to `false`, any updates made in children component except reordering won't reflect.
|
|
73
|
+
```jsx
|
|
74
|
+
import React, { useState } from 'react'
|
|
75
|
+
import ReorderList from 'react-reorder-list'
|
|
76
|
+
|
|
77
|
+
export default function App() {
|
|
78
|
+
const [array, setArray] = useState([0, 1, 2, 3, 4])
|
|
79
|
+
|
|
80
|
+
function setNewArray() {
|
|
81
|
+
setArray(prev => {
|
|
82
|
+
const array = []
|
|
83
|
+
prev.forEach(_ => {
|
|
84
|
+
do {
|
|
85
|
+
var item = Math.floor(Math.random() * 9)
|
|
86
|
+
} while (array.includes(item))
|
|
87
|
+
array.push(item)
|
|
88
|
+
})
|
|
89
|
+
return array
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return <div>
|
|
94
|
+
<ReorderList watchChildrenUpdates={true} animationDuration={200}>
|
|
95
|
+
{array.map(i => <div key={i}>Item {i}</div>)}
|
|
96
|
+
</ReorderList>
|
|
97
|
+
<button onClick={setNewArray}>Click me</button>
|
|
98
|
+
</div>
|
|
99
|
+
}
|
|
100
|
+
```
|
|
66
101
|
#### Nested List Usage
|
|
67
102
|
```jsx
|
|
68
103
|
import React from 'react'
|
|
@@ -93,9 +128,10 @@ Here is the full API for the `<ReorderList>` component, these properties can be
|
|
|
93
128
|
| - | - | - | - | - |
|
|
94
129
|
| `useOnlyIconToDrag` | `Boolean` | No | false | See [usage with ReorderIcon](#usage-with-reordericon) |
|
|
95
130
|
| `selectedItemOpacity` | `Number (0 to 1)` | No | 0.5 | This determines the opacity of the item being dragged, until released. |
|
|
96
|
-
| `animationDuration` | `Number` | No | 400 | The duration of swapping animation between items. If set to 0, animation will be disabled. |
|
|
131
|
+
| `animationDuration` | `Number` | No | 400 | The duration (in ms) of swapping animation between items. If set to 0, animation will be disabled. |
|
|
132
|
+
| `watchChildrenUpdates` | `Boolean` | No | false | Enable this to listen to any updates in children of `<ReorderList>` and update the state accordingly. See [listen to children updates](#listen-to-children-updates) |
|
|
97
133
|
| `onPositionChange` | [`PositionChangeHandler`](#positionchangehandler) | No | - | Function to be executed on item position change. |
|
|
98
|
-
| `
|
|
134
|
+
| `disabled` | `Boolean` | No | false | When set to true, `<ReorderList>` will work as a plain `div` with no functionality. |
|
|
99
135
|
| `props` | `React.DetailedHTMLProps` | No | - | Props to customize the `<ReorderList>` component. |
|
|
100
136
|
### Types
|
|
101
137
|
#### PositionChangeHandler
|
package/dist/animation.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
2
|
type AnimationProps = {
|
|
3
3
|
duration: number;
|
|
4
4
|
children: ReactNode;
|
|
5
5
|
};
|
|
6
|
-
export default function Animation({ duration, children }: AnimationProps):
|
|
6
|
+
export default function Animation({ duration, children }: AnimationProps): ReactNode;
|
|
7
7
|
export {};
|
package/dist/animation.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useState, useLayoutEffect, Children, useEffect, useRef } from "react";
|
|
2
2
|
function usePrevious(value) {
|
|
3
3
|
const prevChildrenRef = useRef();
|
|
4
4
|
useEffect(() => { prevChildrenRef.current = value; }, [value]);
|
|
@@ -7,28 +7,26 @@ function usePrevious(value) {
|
|
|
7
7
|
;
|
|
8
8
|
function calculateBoundingBoxes(children) {
|
|
9
9
|
const boundingBoxes = {};
|
|
10
|
-
Children.forEach(children,
|
|
11
|
-
if (isValidElement(child))
|
|
12
|
-
boundingBoxes[child.key || i] = child.ref.current.getBoundingClientRect();
|
|
13
|
-
});
|
|
10
|
+
Children.forEach(children, child => boundingBoxes[child.key.split("/.")[0]] = child.ref.current.getBoundingClientRect());
|
|
14
11
|
return boundingBoxes;
|
|
15
12
|
}
|
|
16
13
|
;
|
|
17
|
-
function
|
|
14
|
+
export default function Animation({ duration, children }) {
|
|
18
15
|
const [boundingBox, setBoundingBox] = useState({});
|
|
19
16
|
const prevBoundingBox = usePrevious(boundingBox);
|
|
20
17
|
useLayoutEffect(() => {
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
if (duration > 0)
|
|
19
|
+
setBoundingBox(calculateBoundingBoxes(children));
|
|
20
|
+
else
|
|
21
|
+
setBoundingBox({});
|
|
23
22
|
}, [children]);
|
|
24
23
|
useLayoutEffect(() => {
|
|
25
|
-
if (prevBoundingBox && Object.keys(prevBoundingBox).length)
|
|
26
|
-
Children.forEach(children,
|
|
27
|
-
if (!isValidElement(child))
|
|
28
|
-
return;
|
|
24
|
+
if (duration > 0 && prevBoundingBox && Object.keys(prevBoundingBox).length)
|
|
25
|
+
Children.forEach(children, child => {
|
|
29
26
|
const domNode = child.ref.current;
|
|
30
|
-
const
|
|
31
|
-
const { left, top } =
|
|
27
|
+
const key = child.key.split("/.")[0];
|
|
28
|
+
const { left: prevLeft, top: prevTop } = prevBoundingBox[key] || {};
|
|
29
|
+
const { left, top } = boundingBox[key];
|
|
32
30
|
const changeInX = prevLeft - left, changeInY = prevTop - top;
|
|
33
31
|
if (changeInX || changeInY)
|
|
34
32
|
requestAnimationFrame(() => {
|
|
@@ -44,6 +42,3 @@ function Animate({ duration, children }) {
|
|
|
44
42
|
return children;
|
|
45
43
|
}
|
|
46
44
|
;
|
|
47
|
-
export default function Animation({ duration, children }) {
|
|
48
|
-
return duration ? React.createElement(Animate, { duration: duration }, children) : children;
|
|
49
|
-
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { DetailedHTMLProps, HTMLAttributes, ReactNode } from
|
|
1
|
+
import React, { DetailedHTMLProps, HTMLAttributes, ReactNode } from "react";
|
|
2
2
|
export type Props = DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
|
3
3
|
export type PositionChangeHandler = (params?: {
|
|
4
4
|
start?: number;
|
|
@@ -10,11 +10,12 @@ export type ReorderListProps = {
|
|
|
10
10
|
useOnlyIconToDrag?: boolean;
|
|
11
11
|
selectedItemOpacity?: number;
|
|
12
12
|
animationDuration?: number;
|
|
13
|
+
watchChildrenUpdates: boolean;
|
|
13
14
|
onPositionChange?: PositionChangeHandler;
|
|
14
|
-
|
|
15
|
+
disabled?: boolean;
|
|
15
16
|
props?: Props;
|
|
16
17
|
children?: ReactNode;
|
|
17
18
|
};
|
|
18
19
|
export type { IconProps } from './icons.js';
|
|
19
|
-
export default function ReorderList({ useOnlyIconToDrag, selectedItemOpacity, animationDuration, onPositionChange,
|
|
20
|
+
export default function ReorderList({ useOnlyIconToDrag, selectedItemOpacity, animationDuration, watchChildrenUpdates, onPositionChange, disabled, props, children }: ReorderListProps): React.JSX.Element;
|
|
20
21
|
export declare const ReorderIcon: ({ children, style, ...props }: Props) => React.JSX.Element;
|
package/dist/index.js
CHANGED
|
@@ -9,36 +9,45 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
9
9
|
}
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
|
-
import React, { Children, cloneElement, createRef, forwardRef, isValidElement, useMemo, useRef, useState } from
|
|
13
|
-
import { PiDotsSixVerticalBold } from
|
|
14
|
-
import Animation from
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
if (!isValidElement(child))
|
|
19
|
-
return child;
|
|
20
|
-
const childProps = {};
|
|
21
|
-
if (useOnlyIconToDrag && child.type.name === 'ReorderIcon') {
|
|
22
|
-
childProps.onPointerEnter = onPointerEnter;
|
|
23
|
-
childProps.onPointerLeave = onPointerLeave;
|
|
24
|
-
}
|
|
25
|
-
return cloneElement(child, Object.assign({ children: recursiveClone(child.props.children) }, childProps));
|
|
26
|
-
});
|
|
27
|
-
const recursiveChildren = useMemo(() => recursiveClone(children), [children]);
|
|
28
|
-
return React.createElement("div", Object.assign({ ref: ref, style: style }, props), recursiveChildren);
|
|
29
|
-
});
|
|
30
|
-
export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpacity = 0.5, animationDuration = 400, onPositionChange, disable = false, props, children }) {
|
|
31
|
-
const ref = useRef();
|
|
32
|
-
const [draggable, setDraggable] = useState(!useOnlyIconToDrag);
|
|
12
|
+
import React, { Children, cloneElement, createRef, forwardRef, isValidElement, useEffect, useMemo, useRef, useState } from "react";
|
|
13
|
+
import { PiDotsSixVerticalBold } from "./icons.js";
|
|
14
|
+
import Animation from "./animation.js";
|
|
15
|
+
const ReorderItemRef = forwardRef(ReorderItem);
|
|
16
|
+
export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpacity = 0.5, animationDuration = 400, watchChildrenUpdates = false, onPositionChange, disabled = false, props, children }) {
|
|
17
|
+
const ref = useRef(null);
|
|
33
18
|
const [start, setStart] = useState(-1);
|
|
34
19
|
const [selected, setSelected] = useState(-1);
|
|
35
|
-
const [items, setItems] = useState(children);
|
|
20
|
+
const [items, setItems] = useState(Children.toArray(children));
|
|
36
21
|
const [temp, setTemp] = useState({});
|
|
37
22
|
const [isAnimating, setIsAnimating] = useState(false);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
23
|
+
const refs = useMemo(() => items.map(_ => createRef()), [items]);
|
|
24
|
+
const findIndex = (key) => key ? items.findIndex(item => { var _a, _b; return ((_b = (_a = item === null || item === void 0 ? void 0 : item.key) === null || _a === void 0 ? void 0 : _a.split(".$").at(-1)) !== null && _b !== void 0 ? _b : item === null || item === void 0 ? void 0 : item.toString()) === key; }) : -1;
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (!watchChildrenUpdates)
|
|
27
|
+
return;
|
|
28
|
+
if (selected !== -1)
|
|
29
|
+
handleDragEnd(selected);
|
|
30
|
+
const items = [];
|
|
31
|
+
const newItems = [];
|
|
32
|
+
Children.forEach(children, child => {
|
|
33
|
+
var _a;
|
|
34
|
+
const index = findIndex((_a = child === null || child === void 0 ? void 0 : child.key) !== null && _a !== void 0 ? _a : child === null || child === void 0 ? void 0 : child.toString());
|
|
35
|
+
if (index === -1)
|
|
36
|
+
newItems.push(child);
|
|
37
|
+
else
|
|
38
|
+
items[index] = child;
|
|
39
|
+
});
|
|
40
|
+
setItems(items.filter(item => item !== undefined).concat(newItems));
|
|
41
|
+
}, [children]);
|
|
42
|
+
function handleDragEnd(end) {
|
|
43
|
+
if (end !== start)
|
|
44
|
+
onPositionChange === null || onPositionChange === void 0 ? void 0 : onPositionChange({ start, end, oldItems: temp.items, newItems: items });
|
|
45
|
+
setStart(-1);
|
|
46
|
+
setSelected(-1);
|
|
47
|
+
}
|
|
48
|
+
return React.createElement("div", Object.assign({ ref: ref }, props), disabled ? children : React.createElement(Animation, { duration: +(start !== -1) && animationDuration }, Children.map(items, (child, i) => {
|
|
49
|
+
var _a;
|
|
50
|
+
return React.createElement(ReorderItemRef, { key: (_a = child === null || child === void 0 ? void 0 : child.key) !== null && _a !== void 0 ? _a : child === null || child === void 0 ? void 0 : child.toString(), ref: refs[i], useOnlyIconToDrag: useOnlyIconToDrag, style: { opacity: selected === i ? selectedItemOpacity : 1 }, onDragStart: event => {
|
|
42
51
|
event.stopPropagation();
|
|
43
52
|
setStart(i);
|
|
44
53
|
setSelected(i);
|
|
@@ -61,13 +70,26 @@ export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpa
|
|
|
61
70
|
setTimeout(() => setIsAnimating(false), animationDuration);
|
|
62
71
|
}, onDragEnd: event => {
|
|
63
72
|
event.stopPropagation();
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
setStart(-1);
|
|
67
|
-
setSelected(-1);
|
|
68
|
-
}, onPointerEnter: () => setDraggable(true), onPointerLeave: () => setDraggable(false) }, child);
|
|
73
|
+
handleDragEnd(i);
|
|
74
|
+
} }, child);
|
|
69
75
|
})));
|
|
70
76
|
}
|
|
77
|
+
function ReorderItem({ useOnlyIconToDrag, style, onDragStart, onDragEnter, onDragEnd, children }, ref) {
|
|
78
|
+
const [draggable, setDraggable] = useState(false);
|
|
79
|
+
const onPointerEnter = () => setDraggable(true);
|
|
80
|
+
const onPointerLeave = () => setDraggable(false);
|
|
81
|
+
let props = { draggable, onDragStart, onDragEnter, onDragEnd };
|
|
82
|
+
if (!useOnlyIconToDrag)
|
|
83
|
+
props = Object.assign(Object.assign({}, props), { onPointerEnter, onPointerLeave });
|
|
84
|
+
const recursiveClone = (children) => Children.map(children, child => {
|
|
85
|
+
if (!isValidElement(child))
|
|
86
|
+
return child;
|
|
87
|
+
const childProps = useOnlyIconToDrag && child.type.name === 'ReorderIcon' ? { onPointerEnter, onPointerLeave } : {};
|
|
88
|
+
return cloneElement(child, Object.assign({ children: recursiveClone(child.props.children) }, childProps));
|
|
89
|
+
});
|
|
90
|
+
const recursiveChildren = useMemo(() => recursiveClone(children), [children]);
|
|
91
|
+
return React.createElement("div", Object.assign({ ref: ref, style: style }, props), recursiveChildren);
|
|
92
|
+
}
|
|
71
93
|
export const ReorderIcon = (_a) => {
|
|
72
94
|
var { children = React.createElement(PiDotsSixVerticalBold, null), style } = _a, props = __rest(_a, ["children", "style"]);
|
|
73
95
|
return React.createElement("span", Object.assign({ style: Object.assign({ cursor: "grab" }, style) }, props), children);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-reorder-list",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "A simple react component that facilitates the reordering of JSX/HTML elements through drag-and-drop functionality, allowing for easy position changes.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|