react-reorder-list 0.1.0
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/LICENSE +9 -0
- package/README.md +99 -0
- package/dist/icons.d.ts +3 -0
- package/dist/icons.js +6 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +66 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Sahil Aggawal, <aggarwalsahil2004@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# react-reorder-list
|
|
2
|
+
A simple react component that facilitates the reordering of JSX/HTML elements through drag-and-drop functionality, allowing for easy position changes.
|
|
3
|
+
## Features
|
|
4
|
+
- Reorders list of elements using drag and drop.
|
|
5
|
+
- Easy to use
|
|
6
|
+
- Fully Customizable
|
|
7
|
+
- Handles nested lists easily. See [nested list usage](#nested-list-usage)
|
|
8
|
+
## Installation
|
|
9
|
+
To install react-reorder-list
|
|
10
|
+
```bash
|
|
11
|
+
# with npm:
|
|
12
|
+
npm install react-reorder-list --save
|
|
13
|
+
|
|
14
|
+
# with yarn:
|
|
15
|
+
yarn add react-reorder-list
|
|
16
|
+
|
|
17
|
+
# with pnpm:
|
|
18
|
+
pnpm add react-reorder-list
|
|
19
|
+
|
|
20
|
+
# with bun:
|
|
21
|
+
bun add react-reorder-list
|
|
22
|
+
```
|
|
23
|
+
## Usage
|
|
24
|
+
`react-reorder-list` default exports `<ReorderList>` component which encapsulates all the list items as its children.
|
|
25
|
+
|
|
26
|
+
Items in this list can be reordered by simply dragging an item and dropping it in place of another item.
|
|
27
|
+
#### Basic Usage
|
|
28
|
+
An item can be dragged by clicking anywhere inside the item.
|
|
29
|
+
```jsx
|
|
30
|
+
import React from 'react'
|
|
31
|
+
import ReorderList from 'react-reorder-list'
|
|
32
|
+
|
|
33
|
+
export default function App() {
|
|
34
|
+
return <ReorderList useOnlyIconToDrag={false}>
|
|
35
|
+
{[0, 1, 2, 3, 4].map(i=> {
|
|
36
|
+
{/* Having a unique key is important */}
|
|
37
|
+
return <div key={i}>{i}</div>
|
|
38
|
+
})}
|
|
39
|
+
</ReorderList>
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
#### Usage with ReorderIcon
|
|
43
|
+
An item can be dragged only using the `<ReorderIcon>` present inside the item.
|
|
44
|
+
```jsx
|
|
45
|
+
import React from 'react'
|
|
46
|
+
import ReorderList, { ReorderIcon } from 'react-reorder-list'
|
|
47
|
+
|
|
48
|
+
export default function App() {
|
|
49
|
+
return <ReorderList>
|
|
50
|
+
{[0, 1, 2, 3, 4].map(i=> {
|
|
51
|
+
return <div key={i}>
|
|
52
|
+
<ReorderIcon />
|
|
53
|
+
<span>{i}</span>
|
|
54
|
+
</div>
|
|
55
|
+
})}
|
|
56
|
+
</ReorderList>
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
#### Nested List Usage
|
|
60
|
+
```jsx
|
|
61
|
+
import React from 'react'
|
|
62
|
+
import ReorderList, { ReorderIcon } from 'react-reorder-list'
|
|
63
|
+
|
|
64
|
+
export default function App() {
|
|
65
|
+
return <ReorderList>
|
|
66
|
+
{[0, 1, 2].map(i => {
|
|
67
|
+
return <div key={i}>
|
|
68
|
+
<ReorderIcon />
|
|
69
|
+
<span>{'Parent' + i}</span>
|
|
70
|
+
<ReorderList>
|
|
71
|
+
{[0, 1, 2].map(j => {
|
|
72
|
+
return <div key={j} style={{ paddingLeft: '16px' }}>
|
|
73
|
+
<ReorderIcon />
|
|
74
|
+
<span>{'Child' + i + j}</span>
|
|
75
|
+
</div>
|
|
76
|
+
})}
|
|
77
|
+
</ReorderList>
|
|
78
|
+
</div>
|
|
79
|
+
})}
|
|
80
|
+
</ReorderList>
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
## Speech Component API Reference
|
|
84
|
+
Here is the full API for the `<ReorderList>` component, these properties can be set on an instance of ReorderList:
|
|
85
|
+
| Parameter | Type | Required | Default | Description |
|
|
86
|
+
| - | - | - | - | - |
|
|
87
|
+
| `useOnlyIconToDrag` | `Boolean` | No | true | See [usage with ReorderIcon](#usage-with-reordericon) |
|
|
88
|
+
| `selectedItemOpacity` | `Number (0 to 1)` | No | 0.5 | This determines the opacity of the item being dragged, until released. |
|
|
89
|
+
| `onPositionChange` | [`PositionChangeHandler`](#positionchangehandler) | No | - | |
|
|
90
|
+
| `disable` | `Boolean` | No | false | When set to true, `ReorderList` will work as a plain `div` with no functionality. |
|
|
91
|
+
| `props` | `React.DetailedHTMLProps` | No | - | Props to customize the `<ReorderList>` component. |
|
|
92
|
+
### Types
|
|
93
|
+
#### PositionChangeHandler
|
|
94
|
+
```typescript
|
|
95
|
+
import { ReactNode } from 'react';
|
|
96
|
+
type PositionChangeHandler = (params?: { start?: number, end?: number, oldItems?: ReactNode, newItems?: ReactNode }) => void
|
|
97
|
+
```
|
|
98
|
+
## Author
|
|
99
|
+
[Sahil Aggarwal](https://www.github.com/SahilAggarwal2004)
|
package/dist/icons.d.ts
ADDED
package/dist/icons.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export function PiDotsSixVerticalBold(props) {
|
|
3
|
+
return React.createElement("span", Object.assign({}, props),
|
|
4
|
+
React.createElement("svg", { viewBox: "0 0 256 256", fill: "currentColor", width: "1.25rem", height: "1.25rem" },
|
|
5
|
+
React.createElement("path", { d: "M108,60A16,16,0,1,1,92,44,16,16,0,0,1,108,60Zm56,16a16,16,0,1,0-16-16A16,16,0,0,0,164,76ZM92,112a16,16,0,1,0,16,16A16,16,0,0,0,92,112Zm72,0a16,16,0,1,0,16,16A16,16,0,0,0,164,112ZM92,180a16,16,0,1,0,16,16A16,16,0,0,0,92,180Zm72,0a16,16,0,1,0,16,16A16,16,0,0,0,164,180Z" })));
|
|
6
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React, { DetailedHTMLProps, HTMLAttributes, ReactNode } from 'react';
|
|
2
|
+
export type Props = DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
|
3
|
+
export type PositionChangeHandler = (params?: {
|
|
4
|
+
start?: number;
|
|
5
|
+
end?: number;
|
|
6
|
+
oldItems?: ReactNode;
|
|
7
|
+
newItems?: ReactNode;
|
|
8
|
+
}) => void;
|
|
9
|
+
export type ReorderListProps = {
|
|
10
|
+
useOnlyIconToDrag?: boolean;
|
|
11
|
+
selectedItemOpacity?: number;
|
|
12
|
+
onPositionChange?: PositionChangeHandler;
|
|
13
|
+
disable?: boolean;
|
|
14
|
+
props?: Props;
|
|
15
|
+
children?: ReactNode;
|
|
16
|
+
};
|
|
17
|
+
export type { IconProps } from './icons.js';
|
|
18
|
+
export default function ReorderList({ useOnlyIconToDrag, selectedItemOpacity, onPositionChange, disable, props, children }: ReorderListProps): React.JSX.Element;
|
|
19
|
+
export declare const ReorderIcon: ({ children, style, ...props }: Props) => React.JSX.Element;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4
|
+
t[p] = s[p];
|
|
5
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
6
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
7
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
8
|
+
t[p[i]] = s[p[i]];
|
|
9
|
+
}
|
|
10
|
+
return t;
|
|
11
|
+
};
|
|
12
|
+
import React, { Children, cloneElement, isValidElement, useMemo, useState } from 'react';
|
|
13
|
+
import { PiDotsSixVerticalBold } from './icons.js';
|
|
14
|
+
export default function ReorderList({ useOnlyIconToDrag = true, selectedItemOpacity = 0.5, onPositionChange, disable = false, props, children }) {
|
|
15
|
+
const [start, setStart] = useState(-1);
|
|
16
|
+
const [selected, setSelected] = useState(-1);
|
|
17
|
+
const [items, setItems] = useState(children);
|
|
18
|
+
const [temp, setTemp] = useState([]);
|
|
19
|
+
return React.createElement("div", Object.assign({}, props), disable ? children : Children.map(items, (child, i) => {
|
|
20
|
+
if (!isValidElement(child))
|
|
21
|
+
return child;
|
|
22
|
+
return React.createElement(ReorderItem, { key: child.key || i, opacity: selected === i ? selectedItemOpacity : 1, useOnlyIconToDrag: useOnlyIconToDrag, onDragStart: event => {
|
|
23
|
+
event.stopPropagation();
|
|
24
|
+
setStart(i);
|
|
25
|
+
setSelected(i);
|
|
26
|
+
setTemp(items);
|
|
27
|
+
}, onDragEnter: event => {
|
|
28
|
+
event.stopPropagation();
|
|
29
|
+
if (start === -1)
|
|
30
|
+
return;
|
|
31
|
+
setSelected(i);
|
|
32
|
+
setItems(() => {
|
|
33
|
+
const items = temp.filter((_, i) => i !== start);
|
|
34
|
+
items.splice(i, 0, temp[start]);
|
|
35
|
+
return items;
|
|
36
|
+
});
|
|
37
|
+
}, onDragEnd: event => {
|
|
38
|
+
event.stopPropagation();
|
|
39
|
+
if (i !== start)
|
|
40
|
+
onPositionChange === null || onPositionChange === void 0 ? void 0 : onPositionChange({ start, end: i, oldItems: temp, newItems: items });
|
|
41
|
+
setStart(-1);
|
|
42
|
+
setSelected(-1);
|
|
43
|
+
} }, child);
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
function ReorderItem({ useOnlyIconToDrag, opacity, onDragStart, onDragEnter, onDragEnd, children }) {
|
|
47
|
+
const [draggable, setDraggable] = useState(!useOnlyIconToDrag);
|
|
48
|
+
const recursiveChildren = useMemo(() => recursiveClone(children), [children]);
|
|
49
|
+
function recursiveClone(children) {
|
|
50
|
+
return Children.map(children, child => {
|
|
51
|
+
if (!isValidElement(child))
|
|
52
|
+
return child;
|
|
53
|
+
const childProps = {};
|
|
54
|
+
if (useOnlyIconToDrag && child.type.name === 'ReorderIcon') {
|
|
55
|
+
childProps.onPointerEnter = () => setDraggable(true);
|
|
56
|
+
childProps.onPointerLeave = () => setDraggable(false);
|
|
57
|
+
}
|
|
58
|
+
return cloneElement(child, Object.assign({ children: recursiveClone(child.props.children) }, childProps));
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return React.createElement("div", { draggable: draggable, onDragStart: onDragStart, onDragEnter: onDragEnter, onDragEnd: onDragEnd, style: { opacity } }, recursiveChildren);
|
|
62
|
+
}
|
|
63
|
+
export const ReorderIcon = (_a) => {
|
|
64
|
+
var { children = React.createElement(PiDotsSixVerticalBold, null), style } = _a, props = __rest(_a, ["children", "style"]);
|
|
65
|
+
return React.createElement("span", Object.assign({ style: Object.assign({ cursor: "grab" }, style) }, props), children);
|
|
66
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-reorder-list",
|
|
3
|
+
"version": "0.1.0",
|
|
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
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/SahilAggarwal2004/react-reorder-list.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"react-reorder-list",
|
|
14
|
+
"react",
|
|
15
|
+
"reorder",
|
|
16
|
+
"list",
|
|
17
|
+
"component",
|
|
18
|
+
"drag-and-drop",
|
|
19
|
+
"dnd",
|
|
20
|
+
"typescript",
|
|
21
|
+
"jsx",
|
|
22
|
+
"html"
|
|
23
|
+
],
|
|
24
|
+
"author": "Sahil Aggarwal",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/SahilAggarwal2004/react-reorder-list/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/SahilAggarwal2004/react-reorder-list#readme",
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"react": ">=16.0.0",
|
|
32
|
+
"react-dom": ">=16.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/react": "^18.2.48"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "pnpm i && tsc"
|
|
39
|
+
}
|
|
40
|
+
}
|