react-native-reorderable-list 0.8.0 → 0.9.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.
Files changed (55) hide show
  1. package/README.md +90 -12
  2. package/lib/commonjs/components/ReorderableListCell.js +8 -5
  3. package/lib/commonjs/components/ReorderableListCell.js.map +1 -1
  4. package/lib/commonjs/components/ReorderableListCore/ReorderableListCore.js +9 -11
  5. package/lib/commonjs/components/ReorderableListCore/ReorderableListCore.js.map +1 -1
  6. package/lib/commonjs/components/ReorderableListCore/useReorderableListCore.js +24 -4
  7. package/lib/commonjs/components/ReorderableListCore/useReorderableListCore.js.map +1 -1
  8. package/lib/commonjs/contexts/ReorderableCellContext.js.map +1 -1
  9. package/lib/commonjs/contexts/ReorderableListContext.js.map +1 -1
  10. package/lib/commonjs/hooks/index.js +11 -0
  11. package/lib/commonjs/hooks/index.js.map +1 -1
  12. package/lib/commonjs/hooks/useIsActive.js +16 -0
  13. package/lib/commonjs/hooks/useIsActive.js.map +1 -0
  14. package/lib/commonjs/index.js +6 -0
  15. package/lib/commonjs/index.js.map +1 -1
  16. package/lib/module/components/ReorderableListCell.js +8 -5
  17. package/lib/module/components/ReorderableListCell.js.map +1 -1
  18. package/lib/module/components/ReorderableListCore/ReorderableListCore.js +9 -11
  19. package/lib/module/components/ReorderableListCore/ReorderableListCore.js.map +1 -1
  20. package/lib/module/components/ReorderableListCore/useReorderableListCore.js +25 -5
  21. package/lib/module/components/ReorderableListCore/useReorderableListCore.js.map +1 -1
  22. package/lib/module/contexts/ReorderableCellContext.js.map +1 -1
  23. package/lib/module/contexts/ReorderableListContext.js.map +1 -1
  24. package/lib/module/hooks/index.js +1 -0
  25. package/lib/module/hooks/index.js.map +1 -1
  26. package/lib/module/hooks/useIsActive.js +9 -0
  27. package/lib/module/hooks/useIsActive.js.map +1 -0
  28. package/lib/module/index.js +2 -2
  29. package/lib/module/index.js.map +1 -1
  30. package/lib/typescript/components/ReorderableListCell.d.ts.map +1 -1
  31. package/lib/typescript/components/ReorderableListCore/ReorderableListCore.d.ts.map +1 -1
  32. package/lib/typescript/components/ReorderableListCore/useReorderableListCore.d.ts +5 -1
  33. package/lib/typescript/components/ReorderableListCore/useReorderableListCore.d.ts.map +1 -1
  34. package/lib/typescript/contexts/ReorderableCellContext.d.ts +1 -0
  35. package/lib/typescript/contexts/ReorderableCellContext.d.ts.map +1 -1
  36. package/lib/typescript/contexts/ReorderableListContext.d.ts +1 -0
  37. package/lib/typescript/contexts/ReorderableListContext.d.ts.map +1 -1
  38. package/lib/typescript/hooks/index.d.ts +1 -0
  39. package/lib/typescript/hooks/index.d.ts.map +1 -1
  40. package/lib/typescript/hooks/useIsActive.d.ts +2 -0
  41. package/lib/typescript/hooks/useIsActive.d.ts.map +1 -0
  42. package/lib/typescript/index.d.ts +2 -2
  43. package/lib/typescript/index.d.ts.map +1 -1
  44. package/lib/typescript/types/props.d.ts +12 -0
  45. package/lib/typescript/types/props.d.ts.map +1 -1
  46. package/package.json +1 -1
  47. package/src/components/ReorderableListCell.tsx +7 -6
  48. package/src/components/ReorderableListCore/ReorderableListCore.tsx +7 -9
  49. package/src/components/ReorderableListCore/useReorderableListCore.ts +33 -3
  50. package/src/contexts/ReorderableCellContext.ts +1 -0
  51. package/src/contexts/ReorderableListContext.ts +1 -0
  52. package/src/hooks/index.ts +1 -0
  53. package/src/hooks/useIsActive.ts +7 -0
  54. package/src/index.ts +2 -0
  55. package/src/types/props.ts +12 -0
@@ -43,7 +43,6 @@ interface ReorderableListCoreProps<T> extends ReorderableListProps<T> {
43
43
 
44
44
  const ReorderableListCore = <T,>(
45
45
  {
46
- data,
47
46
  autoscrollThreshold = 0.1,
48
47
  autoscrollSpeedScale = 1,
49
48
  autoscrollDelay = AUTOSCROLL_CONFIG.delay,
@@ -54,17 +53,17 @@ const ReorderableListCore = <T,>(
54
53
  onScroll,
55
54
  onDragStart,
56
55
  onDragEnd,
57
- keyExtractor,
58
- extraData,
59
56
  scrollViewContainerRef,
60
57
  scrollViewHeightY,
61
58
  scrollViewScrollOffsetY,
62
59
  scrollViewScrollEnabled,
63
- scrollEnabled,
64
60
  initialScrollViewScrollEnabled,
65
61
  scrollable,
66
62
  outerScrollGesture,
67
63
  cellAnimations,
64
+ shouldUpdateActiveItem,
65
+ panEnabled = true,
66
+ panActivateAfterLongPress,
68
67
  ...rest
69
68
  }: ReorderableListCoreProps<T>,
70
69
  ref: React.ForwardedRef<FlatList<T>>,
@@ -99,13 +98,16 @@ const ReorderableListCore = <T,>(
99
98
  // flatlist will default to true if we pass explicitly undefined,
100
99
  // but internally we would treat it as false, so we force true
101
100
  initialScrollEnabled:
102
- typeof scrollEnabled === 'undefined' ? true : scrollEnabled,
101
+ typeof rest.scrollEnabled === 'undefined' ? true : rest.scrollEnabled,
103
102
  initialScrollViewScrollEnabled:
104
103
  typeof initialScrollViewScrollEnabled === 'undefined'
105
104
  ? true
106
105
  : initialScrollViewScrollEnabled,
107
106
  nestedScrollable: scrollable,
108
107
  cellAnimations,
108
+ shouldUpdateActiveItem,
109
+ panEnabled,
110
+ panActivateAfterLongPress,
109
111
  });
110
112
 
111
113
  const combinedGesture = useMemo(() => {
@@ -145,17 +147,13 @@ const ReorderableListCore = <T,>(
145
147
  <AnimatedFlatList
146
148
  {...rest}
147
149
  ref={handleRef}
148
- data={data}
149
150
  CellRendererComponent={renderAnimatedCell}
150
151
  onLayout={handleFlatListLayout}
151
152
  onScroll={composedScrollHandler}
152
153
  scrollEventThrottle={1}
153
154
  horizontal={false}
154
155
  removeClippedSubviews={false}
155
- keyExtractor={keyExtractor}
156
- extraData={extraData}
157
156
  numColumns={1}
158
- scrollEnabled={scrollEnabled}
159
157
  />
160
158
  </GestureDetector>
161
159
  </ReorderableListContext.Provider>
@@ -1,4 +1,4 @@
1
- import React, {useCallback, useEffect, useMemo} from 'react';
1
+ import React, {useCallback, useEffect, useMemo, useState} from 'react';
2
2
  import {
3
3
  FlatList,
4
4
  LayoutChangeEvent,
@@ -59,6 +59,9 @@ interface UseReorderableListCoreArgs<T> {
59
59
  initialScrollViewScrollEnabled: boolean | undefined;
60
60
  nestedScrollable: boolean | undefined;
61
61
  cellAnimations: ReorderableListCellAnimations | undefined;
62
+ shouldUpdateActiveItem: boolean | undefined;
63
+ panEnabled: boolean;
64
+ panActivateAfterLongPress: number | undefined;
62
65
  }
63
66
 
64
67
  export const useReorderableListCore = <T>({
@@ -80,8 +83,12 @@ export const useReorderableListCore = <T>({
80
83
  initialScrollViewScrollEnabled,
81
84
  nestedScrollable,
82
85
  cellAnimations,
86
+ shouldUpdateActiveItem,
87
+ panActivateAfterLongPress,
88
+ panEnabled,
83
89
  }: UseReorderableListCoreArgs<T>) => {
84
90
  const flatListRef = useAnimatedRef<FlatList>();
91
+ const [activeIndex, setActiveIndex] = useState(-1);
85
92
  const scrollEnabled = useSharedValue(initialScrollEnabled);
86
93
  const gestureState = useSharedValue<State>(State.UNDETERMINED);
87
94
  const currentY = useSharedValue(0);
@@ -124,6 +131,7 @@ export const useReorderableListCore = <T>({
124
131
  currentIndex,
125
132
  draggedIndex,
126
133
  dragEndHandlers,
134
+ activeIndex,
127
135
  scale: scale || scaleDefault,
128
136
  opacity: opacity || opacityDefault,
129
137
  }),
@@ -132,6 +140,7 @@ export const useReorderableListCore = <T>({
132
140
  currentIndex,
133
141
  draggedIndex,
134
142
  dragEndHandlers,
143
+ activeIndex,
135
144
  scale,
136
145
  scaleDefault,
137
146
  opacity,
@@ -177,9 +186,21 @@ export const useReorderableListCore = <T>({
177
186
  ],
178
187
  );
179
188
 
189
+ const panGestureHandlerWithOptions = useMemo(() => {
190
+ if (typeof panActivateAfterLongPress === 'number') {
191
+ panGestureHandler.activateAfterLongPress(panActivateAfterLongPress);
192
+ }
193
+
194
+ if (!panEnabled) {
195
+ panGestureHandler.enabled(panEnabled);
196
+ }
197
+
198
+ return panGestureHandler;
199
+ }, [panActivateAfterLongPress, panEnabled, panGestureHandler]);
200
+
180
201
  const gestureHandler = useMemo(
181
- () => Gesture.Simultaneous(Gesture.Native(), panGestureHandler),
182
- [panGestureHandler],
202
+ () => Gesture.Simultaneous(Gesture.Native(), panGestureHandlerWithOptions),
203
+ [panGestureHandlerWithOptions],
183
204
  );
184
205
 
185
206
  const setScrollEnabled = useCallback(
@@ -367,6 +388,10 @@ export const useReorderableListCore = <T>({
367
388
  // enable back scroll on releasing
368
389
  runOnJS(setScrollEnabled)(true);
369
390
 
391
+ if (shouldUpdateActiveItem) {
392
+ runOnJS(setActiveIndex)(-1);
393
+ }
394
+
370
395
  // trigger onDragEnd event
371
396
  let e = {from: draggedIndex.value, to: currentIndex.value};
372
397
  onDragEnd?.(e);
@@ -644,6 +669,10 @@ export const useReorderableListCore = <T>({
644
669
  // after scrolling the parent list it would offset the new dragged item in another nested list
645
670
  resetSharedValues();
646
671
 
672
+ if (shouldUpdateActiveItem) {
673
+ runOnJS(setActiveIndex)(index);
674
+ }
675
+
647
676
  dragInitialScrollOffsetY.value = flatListScrollOffsetY.value;
648
677
  scrollViewDragInitialScrollOffsetY.value = scrollViewScrollOffsetY
649
678
  ? scrollViewScrollOffsetY.value
@@ -664,6 +693,7 @@ export const useReorderableListCore = <T>({
664
693
  },
665
694
  [
666
695
  resetSharedValues,
696
+ shouldUpdateActiveItem,
667
697
  dragInitialScrollOffsetY,
668
698
  scrollViewScrollOffsetY,
669
699
  scrollViewDragInitialScrollOffsetY,
@@ -6,6 +6,7 @@ interface ReorderableCellContextData {
6
6
  index: number;
7
7
  dragHandler: () => void;
8
8
  draggedIndex: SharedValue<number>;
9
+ isActive: boolean;
9
10
  }
10
11
 
11
12
  export const ReorderableCellContext = React.createContext<
@@ -8,6 +8,7 @@ interface ReorderableListContextData {
8
8
  dragEndHandlers: SharedValue<((from: number, to: number) => void)[][]>;
9
9
  scale: SharedValue<number>;
10
10
  opacity: SharedValue<number>;
11
+ activeIndex: number;
11
12
  }
12
13
 
13
14
  export const ReorderableListContext = React.createContext<
@@ -1,3 +1,4 @@
1
+ export * from './useIsActive';
1
2
  export * from './useReorderableDrag';
2
3
  export * from './useReorderableDragStart';
3
4
  export * from './useReorderableDragEnd';
@@ -0,0 +1,7 @@
1
+ import {useContext} from './useContext';
2
+ import {ReorderableCellContext} from '../contexts';
3
+
4
+ export const useIsActive = () => {
5
+ const {isActive} = useContext(ReorderableCellContext);
6
+ return isActive;
7
+ };
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import {
4
4
  ScrollViewContainer,
5
5
  } from './components';
6
6
  import {
7
+ useIsActive,
7
8
  useReorderableDrag,
8
9
  useReorderableDragEnd,
9
10
  useReorderableDragStart,
@@ -19,6 +20,7 @@ import type {
19
20
  import {reorderItems} from './utils';
20
21
 
21
22
  export {
23
+ useIsActive,
22
24
  useReorderableDrag,
23
25
  useReorderableDragStart,
24
26
  useReorderableDragEnd,
@@ -74,6 +74,18 @@ export interface ReorderableListProps<T>
74
74
  * Allows passing an object with shared values that can animate a cell by using the `onDragStart` and `onDragEnd` events.
75
75
  */
76
76
  cellAnimations?: ReorderableListCellAnimations;
77
+ /**
78
+ * Whether the active item should be updated. Enables usage of `useIsActive` hook. Default: `false`.
79
+ */
80
+ shouldUpdateActiveItem?: boolean;
81
+ /**
82
+ * Wether the pan gestures necessary for dragging are enabled. Default: `true`.
83
+ */
84
+ panEnabled?: boolean;
85
+ /**
86
+ * Duration in milliseconds of a long press on the list before pan gestures, necessary for dragging, are allowed to activate.
87
+ */
88
+ panActivateAfterLongPress?: number;
77
89
  /**
78
90
  * Event fired after an item is released and the list is reordered.
79
91
  */