react-reorder-list 0.4.0 → 0.5.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/README.md CHANGED
@@ -43,9 +43,9 @@ export default function App() {
43
43
  #### Usage with ReorderIcon
44
44
  The dragging behavior can be changed using the `useOnlyIconToDrag` prop of `<ReorderList>` component.
45
45
 
46
- If set to `true`, an item can be dragged only using the `<ReorderIcon>` present inside the item.
47
-
48
46
  If set to `false`, an item can be dragged by clicking anywhere inside the item.
47
+
48
+ If set to `true`, an item can be dragged only using the `<ReorderIcon>` present inside the item.
49
49
  ```jsx
50
50
  import React from 'react'
51
51
  import ReorderList, { ReorderIcon } from 'react-reorder-list'
@@ -67,9 +67,13 @@ export default function App() {
67
67
  #### Listen to Children Updates
68
68
  `<ReorderList>` can listen to updates to it's children components using the `watchChildrenUpdates` prop as shown below.
69
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.
70
+ If set to `false`, any updates made in children component except reordering by user won't reflect.
71
+
72
+ If set to `true`, updates to children like state changes, additions/omissions of children components will reflect in real time.<br>
73
+ Further if `preserveOrder` is set to false, the order in which new children appear will be maintained.<br>
74
+ Whereas if `preserveOrder` is set to true, the order of existing items will be preserved as before the update occured and new items will be placed at the end irrespective of their order in children. Also, if an item is being dragged and an update occurs at that moment, that item will be placed at respective location and `onPositionChange` will be called to prevent any inconsistency.
71
75
 
72
- If set to `false`, any updates made in children component except reordering won't reflect.
76
+ NOTE: The props `watchChildrenUpdates` and `preserveOrder` should be used carefully to avoid any unexpected behaviour
73
77
  ```jsx
74
78
  import React, { useState } from 'react'
75
79
  import ReorderList from 'react-reorder-list'
@@ -130,6 +134,7 @@ Here is the full API for the `<ReorderList>` component, these properties can be
130
134
  | `selectedItemOpacity` | `Number (0 to 1)` | No | 0.5 | This determines the opacity of the item being dragged, until released. |
131
135
  | `animationDuration` | `Number` | No | 400 | The duration (in ms) of swapping animation between items. If set to 0, animation will be disabled. |
132
136
  | `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) |
137
+ | `preserveOrder` | `Boolean` | No | false | Works along woth `watchChildrenUpdates` to determine whether to preserve existing order or not. See [listen to children updates](#listen-to-children-updates) |
133
138
  | `onPositionChange` | [`PositionChangeHandler`](#positionchangehandler) | No | - | Function to be executed on item position change. |
134
139
  | `disabled` | `Boolean` | No | false | When set to true, `<ReorderList>` will work as a plain `div` with no functionality. |
135
140
  | `props` | `React.DetailedHTMLProps` | No | - | Props to customize the `<ReorderList>` component. |
package/dist/index.d.ts CHANGED
@@ -11,12 +11,13 @@ export type ReorderListProps = {
11
11
  useOnlyIconToDrag?: boolean;
12
12
  selectedItemOpacity?: number;
13
13
  animationDuration?: number;
14
- watchChildrenUpdates: boolean;
14
+ watchChildrenUpdates?: boolean;
15
+ preserveOrder?: boolean;
15
16
  onPositionChange?: PositionChangeHandler;
16
17
  disabled?: boolean;
17
18
  props?: Props;
18
19
  children?: ReactNode;
19
20
  };
20
21
  export type { IconProps } from './icons.js';
21
- export default function ReorderList({ useOnlyIconToDrag, selectedItemOpacity, animationDuration, watchChildrenUpdates, onPositionChange, disabled, props, children }: ReorderListProps): React.JSX.Element;
22
- export declare const ReorderIcon: ({ children, style, ...props }: Props) => React.JSX.Element;
22
+ export default function ReorderList({ useOnlyIconToDrag, selectedItemOpacity, animationDuration, watchChildrenUpdates, preserveOrder, onPositionChange, disabled, props, children }: ReorderListProps): React.JSX.Element;
23
+ export declare function ReorderIcon({ children, style, ...props }: Props): React.JSX.Element;
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ import React, { Children, cloneElement, createRef, forwardRef, isValidElement, u
13
13
  import { PiDotsSixVerticalBold } from "./icons.js";
14
14
  import Animation from "./animation.js";
15
15
  const ReorderItemRef = forwardRef(ReorderItem);
16
- export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpacity = 0.5, animationDuration = 400, watchChildrenUpdates = false, onPositionChange, disabled = false, props, children }) {
16
+ export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpacity = 0.5, animationDuration = 400, watchChildrenUpdates = false, preserveOrder = false, onPositionChange, disabled = false, props, children }) {
17
17
  const ref = useRef(null);
18
18
  const [start, setStart] = useState(-1);
19
19
  const [selected, setSelected] = useState(-1);
@@ -26,21 +26,25 @@ export default function ReorderList({ useOnlyIconToDrag = false, selectedItemOpa
26
26
  if (!watchChildrenUpdates)
27
27
  return;
28
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));
29
+ handleDragEnd(selected, preserveOrder);
30
+ if (preserveOrder) {
31
+ const items = [];
32
+ const newItems = [];
33
+ Children.forEach(children, child => {
34
+ var _a;
35
+ 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());
36
+ if (index === -1)
37
+ newItems.push(child);
38
+ else
39
+ items[index] = child;
40
+ });
41
+ setItems(items.filter(item => item !== undefined).concat(newItems));
42
+ }
43
+ else
44
+ setItems(Children.toArray(children));
41
45
  }, [children]);
42
- function handleDragEnd(end) {
43
- if (end !== start)
46
+ function handleDragEnd(end, handlePositionChange = true) {
47
+ if (handlePositionChange && end !== start)
44
48
  onPositionChange === null || onPositionChange === void 0 ? void 0 : onPositionChange({ start, end, oldItems: temp.items, newItems: items, revert: () => setItems(temp.items) });
45
49
  setStart(-1);
46
50
  setSelected(-1);
@@ -84,13 +88,13 @@ function ReorderItem({ useOnlyIconToDrag, style, onDragStart, onDragEnter, onDra
84
88
  const recursiveClone = (children) => Children.map(children, child => {
85
89
  if (!isValidElement(child))
86
90
  return child;
87
- const childProps = useOnlyIconToDrag && child.type.name === 'ReorderIcon' ? { onPointerEnter, onPointerLeave } : {};
91
+ const childProps = child.type === ReorderIcon ? { onPointerEnter, onPointerLeave } : {};
88
92
  return cloneElement(child, Object.assign({ children: recursiveClone(child.props.children) }, childProps));
89
93
  });
90
- const recursiveChildren = useMemo(() => recursiveClone(children), [children]);
94
+ const recursiveChildren = useMemo(() => useOnlyIconToDrag ? recursiveClone(children) : children, [children]);
91
95
  return React.createElement("div", Object.assign({ ref: ref, style: style }, props), recursiveChildren);
92
96
  }
93
- export const ReorderIcon = (_a) => {
97
+ export function ReorderIcon(_a) {
94
98
  var { children = React.createElement(PiDotsSixVerticalBold, null), style } = _a, props = __rest(_a, ["children", "style"]);
95
99
  return React.createElement("span", Object.assign({ style: Object.assign({ cursor: "grab" }, style) }, props), children);
96
- };
100
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-reorder-list",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
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",