react-native-reorderable-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/LICENSE +1 -1
- package/README.md +118 -60
- package/lib/commonjs/components/ReorderableList/ReorderableList.js +99 -0
- package/lib/commonjs/components/ReorderableList/ReorderableList.js.map +1 -0
- package/lib/commonjs/components/ReorderableList/constants.ios.js +10 -0
- package/lib/commonjs/components/ReorderableList/constants.ios.js.map +1 -0
- package/lib/commonjs/components/ReorderableList/constants.js +10 -0
- package/lib/commonjs/components/ReorderableList/constants.js.map +1 -0
- package/lib/commonjs/components/ReorderableList/index.js +17 -0
- package/lib/commonjs/components/ReorderableList/index.js.map +1 -0
- package/lib/commonjs/components/ReorderableList/useReorderableList.js +311 -0
- package/lib/commonjs/components/ReorderableList/useReorderableList.js.map +1 -0
- package/lib/commonjs/components/ReorderableListCell.js +93 -0
- package/lib/commonjs/components/ReorderableListCell.js.map +1 -0
- package/lib/commonjs/components/ReorderableListItem.js +87 -0
- package/lib/commonjs/components/ReorderableListItem.js.map +1 -0
- package/lib/commonjs/components/index.js +39 -0
- package/lib/commonjs/components/index.js.map +1 -0
- package/lib/commonjs/contexts/ReorderableCellContext.js +10 -0
- package/lib/commonjs/contexts/ReorderableCellContext.js.map +1 -0
- package/lib/commonjs/contexts/ReorderableListContext.js +10 -0
- package/lib/commonjs/contexts/ReorderableListContext.js.map +1 -0
- package/lib/commonjs/contexts/index.js +28 -0
- package/lib/commonjs/contexts/index.js.map +1 -0
- package/lib/commonjs/hooks/index.js +50 -0
- package/lib/commonjs/hooks/index.js.map +1 -0
- package/lib/commonjs/hooks/useContext.js +16 -0
- package/lib/commonjs/hooks/useContext.js.map +1 -0
- package/lib/commonjs/hooks/useReorderableDrag.js +16 -0
- package/lib/commonjs/hooks/useReorderableDrag.js.map +1 -0
- package/lib/commonjs/hooks/useReorderableDragEnd.js +28 -0
- package/lib/commonjs/hooks/useReorderableDragEnd.js.map +1 -0
- package/lib/commonjs/hooks/useReorderableDragStart.js +22 -0
- package/lib/commonjs/hooks/useReorderableDragStart.js.map +1 -0
- package/lib/commonjs/index.js +41 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/types/index.js +28 -0
- package/lib/commonjs/types/index.js.map +1 -0
- package/lib/commonjs/types/misc.js +14 -0
- package/lib/commonjs/types/misc.js.map +1 -0
- package/lib/commonjs/types/props.js +6 -0
- package/lib/commonjs/types/props.js.map +1 -0
- package/lib/commonjs/utils.js +23 -0
- package/lib/commonjs/utils.js.map +1 -0
- package/lib/module/components/ReorderableList/ReorderableList.js +91 -0
- package/lib/module/components/ReorderableList/ReorderableList.js.map +1 -0
- package/lib/module/components/ReorderableList/constants.ios.js +4 -0
- package/lib/module/components/ReorderableList/constants.ios.js.map +1 -0
- package/lib/module/components/ReorderableList/constants.js +4 -0
- package/lib/module/components/ReorderableList/constants.js.map +1 -0
- package/lib/module/components/ReorderableList/index.js +2 -0
- package/lib/module/components/ReorderableList/index.js.map +1 -0
- package/lib/module/components/ReorderableList/useReorderableList.js +302 -0
- package/lib/module/components/ReorderableList/useReorderableList.js.map +1 -0
- package/lib/module/components/ReorderableListCell.js +85 -0
- package/lib/module/components/ReorderableListCell.js.map +1 -0
- package/lib/module/components/ReorderableListItem.js +78 -0
- package/lib/module/components/ReorderableListItem.js.map +1 -0
- package/lib/module/components/index.js +4 -0
- package/lib/module/components/index.js.map +1 -0
- package/lib/module/contexts/ReorderableCellContext.js +3 -0
- package/lib/module/contexts/ReorderableCellContext.js.map +1 -0
- package/lib/module/contexts/ReorderableListContext.js +3 -0
- package/lib/module/contexts/ReorderableListContext.js.map +1 -0
- package/lib/module/contexts/index.js +3 -0
- package/lib/module/contexts/index.js.map +1 -0
- package/lib/module/hooks/index.js +5 -0
- package/lib/module/hooks/index.js.map +1 -0
- package/lib/module/hooks/useContext.js +9 -0
- package/lib/module/hooks/useContext.js.map +1 -0
- package/lib/module/hooks/useReorderableDrag.js +9 -0
- package/lib/module/hooks/useReorderableDrag.js.map +1 -0
- package/lib/module/hooks/useReorderableDragEnd.js +21 -0
- package/lib/module/hooks/useReorderableDragEnd.js.map +1 -0
- package/lib/module/hooks/useReorderableDragStart.js +15 -0
- package/lib/module/hooks/useReorderableDragStart.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/types/index.js +3 -0
- package/lib/module/types/index.js.map +1 -0
- package/lib/module/types/misc.js +8 -0
- package/lib/module/types/misc.js.map +1 -0
- package/lib/module/types/props.js +2 -0
- package/lib/module/types/props.js.map +1 -0
- package/lib/module/utils.js +16 -0
- package/lib/module/utils.js.map +1 -0
- package/lib/typescript/components/ReorderableList/ReorderableList.d.ts +8 -0
- package/lib/typescript/components/ReorderableList/ReorderableList.d.ts.map +1 -0
- package/lib/typescript/components/ReorderableList/constants.d.ts +3 -0
- package/lib/typescript/components/ReorderableList/constants.d.ts.map +1 -0
- package/lib/typescript/components/ReorderableList/constants.ios.d.ts +3 -0
- package/lib/typescript/components/ReorderableList/constants.ios.d.ts.map +1 -0
- package/lib/typescript/components/ReorderableList/index.d.ts +2 -0
- package/lib/typescript/components/ReorderableList/index.d.ts.map +1 -0
- package/lib/typescript/components/ReorderableList/useReorderableList.d.ts +34 -0
- package/lib/typescript/components/ReorderableList/useReorderableList.d.ts.map +1 -0
- package/lib/typescript/components/ReorderableListCell.d.ts +20 -0
- package/lib/typescript/components/ReorderableListCell.d.ts.map +1 -0
- package/lib/typescript/components/ReorderableListItem.d.ts +4 -0
- package/lib/typescript/components/ReorderableListItem.d.ts.map +1 -0
- package/lib/typescript/components/index.d.ts +4 -0
- package/lib/typescript/components/index.d.ts.map +1 -0
- package/lib/typescript/contexts/ReorderableCellContext.d.ts +11 -0
- package/lib/typescript/contexts/ReorderableCellContext.d.ts.map +1 -0
- package/lib/typescript/contexts/ReorderableListContext.d.ts +9 -0
- package/lib/typescript/contexts/ReorderableListContext.d.ts.map +1 -0
- package/lib/typescript/contexts/index.d.ts +3 -0
- package/lib/typescript/contexts/index.d.ts.map +1 -0
- package/lib/typescript/hooks/index.d.ts +5 -0
- package/lib/typescript/hooks/index.d.ts.map +1 -0
- package/lib/typescript/hooks/useContext.d.ts +3 -0
- package/lib/typescript/hooks/useContext.d.ts.map +1 -0
- package/lib/typescript/hooks/useReorderableDrag.d.ts +2 -0
- package/lib/typescript/hooks/useReorderableDrag.d.ts.map +1 -0
- package/lib/typescript/hooks/useReorderableDragEnd.d.ts +2 -0
- package/lib/typescript/hooks/useReorderableDragEnd.d.ts.map +1 -0
- package/lib/typescript/hooks/useReorderableDragStart.d.ts +2 -0
- package/lib/typescript/hooks/useReorderableDragStart.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +7 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/types/index.d.ts +3 -0
- package/lib/typescript/types/index.d.ts.map +1 -0
- package/lib/typescript/types/misc.d.ts +7 -0
- package/lib/typescript/types/misc.d.ts.map +1 -0
- package/lib/typescript/types/props.d.ts +90 -0
- package/lib/typescript/types/props.d.ts.map +1 -0
- package/lib/typescript/utils.d.ts +12 -0
- package/lib/typescript/utils.d.ts.map +1 -0
- package/package.json +199 -39
- package/src/components/ReorderableList/ReorderableList.tsx +123 -0
- package/src/components/ReorderableList/constants.ios.ts +3 -0
- package/src/components/ReorderableList/constants.ts +3 -0
- package/src/components/ReorderableList/index.ts +1 -0
- package/src/components/ReorderableList/useReorderableList.ts +487 -0
- package/src/components/ReorderableListCell.tsx +143 -0
- package/src/components/ReorderableListItem.tsx +108 -0
- package/src/components/index.ts +3 -0
- package/src/contexts/ReorderableCellContext.ts +14 -0
- package/src/contexts/ReorderableListContext.ts +12 -0
- package/src/contexts/index.ts +2 -0
- package/src/hooks/index.ts +4 -0
- package/src/hooks/useContext.ts +11 -0
- package/src/hooks/useReorderableDrag.ts +7 -0
- package/src/hooks/useReorderableDragEnd.ts +25 -0
- package/src/hooks/useReorderableDragStart.ts +18 -0
- package/src/index.ts +26 -0
- package/src/types/index.ts +2 -0
- package/src/types/misc.ts +6 -0
- package/src/types/props.ts +101 -0
- package/src/utils.ts +15 -0
- package/dist/components/ReorderableList.d.ts +0 -7
- package/dist/components/ReorderableList.js +0 -314
- package/dist/components/ReorderableListItem.d.ts +0 -14
- package/dist/components/ReorderableListItem.js +0 -57
- package/dist/hooks/useAnimatedSharedValues.d.ts +0 -3
- package/dist/hooks/useAnimatedSharedValues.js +0 -33
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -2
- package/dist/types/misc.d.ts +0 -15
- package/dist/types/misc.js +0 -7
- package/dist/types/props.d.ts +0 -29
- package/dist/types/props.js +0 -1
|
@@ -1,314 +0,0 @@
|
|
|
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, { useEffect, useRef, useState, useCallback } from 'react';
|
|
13
|
-
import { FlatList, StatusBar, unstable_batchedUpdates, Platform, } from 'react-native';
|
|
14
|
-
import { NativeViewGestureHandler, PanGestureHandler, State, } from 'react-native-gesture-handler';
|
|
15
|
-
import Animated, { useAnimatedGestureHandler, useSharedValue, useAnimatedReaction, runOnJS, useAnimatedStyle, useAnimatedRef, useAnimatedScrollHandler, scrollTo, withTiming, Easing, runOnUI, } from 'react-native-reanimated';
|
|
16
|
-
import composeRefs from '@seznam/compose-react-refs';
|
|
17
|
-
import memoize from 'fast-memoize';
|
|
18
|
-
import ReorderableListItem from './ReorderableListItem';
|
|
19
|
-
import useAnimatedSharedValues from '../hooks/useAnimatedSharedValues';
|
|
20
|
-
import { ReorderableListState } from '../types/misc';
|
|
21
|
-
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
|
|
22
|
-
const ReorderableList = (_a, ref) => {
|
|
23
|
-
var { data, containerStyle, scrollAreaSize = 0.1, scrollSpeed = 2, dragScale = 1, renderItem, onLayout, onReorder, keyExtractor } = _a, rest = __rest(_a, ["data", "containerStyle", "scrollAreaSize", "scrollSpeed", "dragScale", "renderItem", "onLayout", "onReorder", "keyExtractor"]);
|
|
24
|
-
const container = useAnimatedRef();
|
|
25
|
-
const flatList = useAnimatedRef();
|
|
26
|
-
const nativeHandler = useRef(null);
|
|
27
|
-
const itemSeparators = useRef([]);
|
|
28
|
-
const gestureState = useSharedValue(State.UNDETERMINED);
|
|
29
|
-
const currentY = useSharedValue(0);
|
|
30
|
-
const startY = useSharedValue(0);
|
|
31
|
-
const containerPositionX = useSharedValue(0);
|
|
32
|
-
const containerPositionY = useSharedValue(0);
|
|
33
|
-
const scrollOffset = useSharedValue(0);
|
|
34
|
-
const itemOffsets = useAnimatedSharedValues(() => ({ length: 0, offset: 0 }), data.length);
|
|
35
|
-
const topMoveThreshold = useSharedValue(0);
|
|
36
|
-
const bottomMoveThreshold = useSharedValue(0);
|
|
37
|
-
const flatListHeight = useSharedValue(0);
|
|
38
|
-
const offsetY = useSharedValue(0);
|
|
39
|
-
const draggedTranslateY = useSharedValue(null);
|
|
40
|
-
const currentIndex = useSharedValue(-1);
|
|
41
|
-
const draggedIndex = useSharedValue(-1);
|
|
42
|
-
const draggedInfoIndex = useSharedValue(-1);
|
|
43
|
-
const state = useSharedValue(ReorderableListState.IDLE);
|
|
44
|
-
const autoScrollOffset = useSharedValue(-1);
|
|
45
|
-
const autoScrollSpeed = useSharedValue(Math.max(0, scrollSpeed));
|
|
46
|
-
const enabledOpacity = useSharedValue(false);
|
|
47
|
-
const draggedItemScale = useSharedValue(1);
|
|
48
|
-
const [dragged, setDragged] = useState(false);
|
|
49
|
-
const relativeToContainer = (y, x) => {
|
|
50
|
-
'worklet';
|
|
51
|
-
return {
|
|
52
|
-
y: y - containerPositionY.value - (StatusBar.currentHeight || 0),
|
|
53
|
-
x: x - containerPositionX.value,
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
const handleGestureEvent = useAnimatedGestureHandler({
|
|
57
|
-
onStart: (e, ctx) => {
|
|
58
|
-
if (state.value !== ReorderableListState.RELEASING) {
|
|
59
|
-
const { y } = relativeToContainer(e.absoluteY, e.absoluteX);
|
|
60
|
-
ctx.startY = y;
|
|
61
|
-
startY.value = y;
|
|
62
|
-
currentY.value = y;
|
|
63
|
-
gestureState.value = e.state;
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
onActive: (e, ctx) => {
|
|
67
|
-
if (state.value !== ReorderableListState.RELEASING) {
|
|
68
|
-
currentY.value = ctx.startY + e.translationY;
|
|
69
|
-
gestureState.value = e.state;
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
onEnd: (e) => (gestureState.value = e.state),
|
|
73
|
-
onFinish: (e) => (gestureState.value = e.state),
|
|
74
|
-
onCancel: (e) => (gestureState.value = e.state),
|
|
75
|
-
onFail: (e) => (gestureState.value = e.state),
|
|
76
|
-
});
|
|
77
|
-
useEffect(() => {
|
|
78
|
-
if (!dragged) {
|
|
79
|
-
runOnUI(() => {
|
|
80
|
-
'worklet';
|
|
81
|
-
state.value = ReorderableListState.IDLE;
|
|
82
|
-
})();
|
|
83
|
-
}
|
|
84
|
-
}, [dragged, state, enabledOpacity]);
|
|
85
|
-
useEffect(() => {
|
|
86
|
-
runOnUI(() => {
|
|
87
|
-
'worklet';
|
|
88
|
-
enabledOpacity.value = dragged;
|
|
89
|
-
if (dragged) {
|
|
90
|
-
draggedItemScale.value = withTiming(dragScale, {
|
|
91
|
-
duration: 100,
|
|
92
|
-
easing: Easing.out(Easing.ease),
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
})();
|
|
96
|
-
}, [dragged, draggedItemScale, enabledOpacity, dragScale]);
|
|
97
|
-
const enableDragged = useCallback((enabled) => {
|
|
98
|
-
flatList.current.setNativeProps({ scrollEnabled: !enabled });
|
|
99
|
-
setDragged(enabled);
|
|
100
|
-
}, [setDragged, flatList]);
|
|
101
|
-
const reorder = (fromIndex, toIndex) => {
|
|
102
|
-
if (fromIndex !== toIndex) {
|
|
103
|
-
unstable_batchedUpdates(() => {
|
|
104
|
-
onReorder({ fromIndex, toIndex });
|
|
105
|
-
enableDragged(false);
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
enableDragged(false);
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
const endDrag = () => {
|
|
113
|
-
'worklet';
|
|
114
|
-
const draggedIndexTemp = draggedIndex.value;
|
|
115
|
-
const currentIndexTemp = currentIndex.value;
|
|
116
|
-
draggedIndex.value = -1;
|
|
117
|
-
currentIndex.value = -1;
|
|
118
|
-
enabledOpacity.value = false;
|
|
119
|
-
draggedInfoIndex.value = currentIndexTemp;
|
|
120
|
-
runOnJS(reorder)(draggedIndexTemp, currentIndexTemp);
|
|
121
|
-
};
|
|
122
|
-
const getIndexFromY = (y, scrollY) => {
|
|
123
|
-
'worklet';
|
|
124
|
-
const maxOffset = itemOffsets[itemOffsets.length - 1].value;
|
|
125
|
-
const relativeY = Math.max(0, Math.min((scrollY || scrollOffset.value) + y, maxOffset.offset + maxOffset.length));
|
|
126
|
-
const index = itemOffsets.findIndex((x, i) => ((x === null || x === void 0 ? void 0 : x.value.offset) && i === 0 && relativeY < x.value.offset) ||
|
|
127
|
-
(i === itemOffsets.length - 1 && relativeY > x.value.offset) ||
|
|
128
|
-
(relativeY >= x.value.offset &&
|
|
129
|
-
relativeY <= x.value.offset + x.value.length));
|
|
130
|
-
return { index, relativeY };
|
|
131
|
-
};
|
|
132
|
-
useAnimatedReaction(() => gestureState.value, () => {
|
|
133
|
-
if (gestureState.value !== State.ACTIVE &&
|
|
134
|
-
gestureState.value !== State.BEGAN &&
|
|
135
|
-
(state.value === ReorderableListState.DRAGGING ||
|
|
136
|
-
state.value === ReorderableListState.AUTO_SCROLL)) {
|
|
137
|
-
state.value = ReorderableListState.RELEASING;
|
|
138
|
-
const offsetCorrection = currentIndex.value > draggedIndex.value
|
|
139
|
-
? itemOffsets[currentIndex.value].value.length -
|
|
140
|
-
itemOffsets[draggedIndex.value].value.length
|
|
141
|
-
: 0;
|
|
142
|
-
const newTopPosition = itemOffsets[currentIndex.value].value.offset +
|
|
143
|
-
offsetCorrection -
|
|
144
|
-
scrollOffset.value;
|
|
145
|
-
const duration = 100;
|
|
146
|
-
draggedItemScale.value = withTiming(1, {
|
|
147
|
-
duration,
|
|
148
|
-
easing: Easing.out(Easing.ease),
|
|
149
|
-
}, () => {
|
|
150
|
-
if (draggedTranslateY.value === newTopPosition) {
|
|
151
|
-
endDrag();
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
if (draggedTranslateY.value !== newTopPosition) {
|
|
155
|
-
draggedTranslateY.value = withTiming(newTopPosition, {
|
|
156
|
-
duration,
|
|
157
|
-
easing: Easing.out(Easing.ease),
|
|
158
|
-
}, () => {
|
|
159
|
-
endDrag();
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
useAnimatedReaction(() => currentY.value, (y) => {
|
|
165
|
-
if (state.value === ReorderableListState.DRAGGING ||
|
|
166
|
-
state.value === ReorderableListState.AUTO_SCROLL) {
|
|
167
|
-
draggedTranslateY.value = y - offsetY.value;
|
|
168
|
-
const { index } = getIndexFromY(y);
|
|
169
|
-
currentIndex.value = index;
|
|
170
|
-
if (y <= topMoveThreshold.value || y >= bottomMoveThreshold.value) {
|
|
171
|
-
state.value = ReorderableListState.AUTO_SCROLL;
|
|
172
|
-
autoScrollOffset.value = scrollOffset.value;
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
state.value = ReorderableListState.DRAGGING;
|
|
176
|
-
autoScrollOffset.value = -1;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
useAnimatedReaction(() => autoScrollOffset.value, () => {
|
|
181
|
-
if (state.value === ReorderableListState.AUTO_SCROLL) {
|
|
182
|
-
let speed = 0;
|
|
183
|
-
if (currentY.value <= topMoveThreshold.value) {
|
|
184
|
-
speed = -autoScrollSpeed.value;
|
|
185
|
-
}
|
|
186
|
-
else if (currentY.value >= bottomMoveThreshold.value) {
|
|
187
|
-
speed = autoScrollSpeed.value;
|
|
188
|
-
}
|
|
189
|
-
if (speed !== 0) {
|
|
190
|
-
scrollTo(flatList, 0, autoScrollOffset.value + speed, Platform.OS === 'android');
|
|
191
|
-
autoScrollOffset.value += speed;
|
|
192
|
-
}
|
|
193
|
-
const { index } = getIndexFromY(currentY.value, autoScrollOffset.value);
|
|
194
|
-
currentIndex.value = index;
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
const scrollHandler = useAnimatedScrollHandler({
|
|
198
|
-
onScroll: (e) => {
|
|
199
|
-
scrollOffset.value = e.contentOffset.y;
|
|
200
|
-
},
|
|
201
|
-
});
|
|
202
|
-
const handleItemLayout = useCallback(memoize((index, onLayoutCell) => (e) => {
|
|
203
|
-
itemOffsets[index].value = {
|
|
204
|
-
offset: e.nativeEvent.layout.y,
|
|
205
|
-
length: e.nativeEvent.layout.height,
|
|
206
|
-
};
|
|
207
|
-
if (onLayoutCell) {
|
|
208
|
-
onLayoutCell(e);
|
|
209
|
-
}
|
|
210
|
-
}), [itemOffsets]);
|
|
211
|
-
const drag = useCallback(memoize((index) => () => runOnUI(() => {
|
|
212
|
-
'worklet';
|
|
213
|
-
offsetY.value =
|
|
214
|
-
startY.value -
|
|
215
|
-
(itemOffsets[index].value.offset - scrollOffset.value);
|
|
216
|
-
draggedTranslateY.value = startY.value - offsetY.value;
|
|
217
|
-
draggedIndex.value = index;
|
|
218
|
-
draggedInfoIndex.value = index;
|
|
219
|
-
currentIndex.value = index;
|
|
220
|
-
state.value = ReorderableListState.DRAGGING;
|
|
221
|
-
runOnJS(enableDragged)(true);
|
|
222
|
-
})()), [
|
|
223
|
-
offsetY,
|
|
224
|
-
startY,
|
|
225
|
-
scrollOffset,
|
|
226
|
-
draggedTranslateY,
|
|
227
|
-
draggedIndex,
|
|
228
|
-
draggedInfoIndex,
|
|
229
|
-
currentIndex,
|
|
230
|
-
state,
|
|
231
|
-
itemOffsets,
|
|
232
|
-
enableDragged,
|
|
233
|
-
]);
|
|
234
|
-
const renderAnimatedCell = useCallback((_a) => {
|
|
235
|
-
var { index, children } = _a, cellProps = __rest(_a, ["index", "children"]);
|
|
236
|
-
return (React.createElement(ReorderableListItem, { key: cellProps.keyExtractor
|
|
237
|
-
? cellProps.keyExtractor(cellProps.data[index], index)
|
|
238
|
-
: index, index: index, currentIndex: currentIndex, draggedIndex: draggedIndex, itemOffsets: itemOffsets, enabledOpacity: enabledOpacity, onLayout: handleItemLayout(index, cellProps.onLayout), children: children }));
|
|
239
|
-
}, [currentIndex, draggedIndex, itemOffsets, enabledOpacity, handleItemLayout]);
|
|
240
|
-
const renderDraggableItem = useCallback((info) => {
|
|
241
|
-
itemSeparators.current[info.index] = info.separators;
|
|
242
|
-
return renderItem(Object.assign(Object.assign({}, info), { drag: drag(info.index) }));
|
|
243
|
-
}, [renderItem, drag]);
|
|
244
|
-
const handleContainerLayout = () => {
|
|
245
|
-
container.current.measureInWindow((x, y) => {
|
|
246
|
-
containerPositionX.value = x;
|
|
247
|
-
containerPositionY.value = y;
|
|
248
|
-
});
|
|
249
|
-
};
|
|
250
|
-
const handleFlatListLayout = useCallback((e) => {
|
|
251
|
-
const { height } = e.nativeEvent.layout;
|
|
252
|
-
const portion = height * Math.max(0, Math.min(scrollAreaSize, 0.5));
|
|
253
|
-
topMoveThreshold.value = portion;
|
|
254
|
-
bottomMoveThreshold.value = height - portion;
|
|
255
|
-
flatListHeight.value = height;
|
|
256
|
-
if (onLayout) {
|
|
257
|
-
onLayout(e);
|
|
258
|
-
}
|
|
259
|
-
}, [
|
|
260
|
-
bottomMoveThreshold,
|
|
261
|
-
topMoveThreshold,
|
|
262
|
-
flatListHeight,
|
|
263
|
-
onLayout,
|
|
264
|
-
scrollAreaSize,
|
|
265
|
-
]);
|
|
266
|
-
const draggedItemStyle = useAnimatedStyle(() => {
|
|
267
|
-
if (draggedTranslateY.value !== null) {
|
|
268
|
-
return {
|
|
269
|
-
transform: [
|
|
270
|
-
{ translateY: draggedTranslateY.value },
|
|
271
|
-
{ scale: draggedItemScale.value },
|
|
272
|
-
],
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
return {};
|
|
276
|
-
});
|
|
277
|
-
const draggedItemInfo = {
|
|
278
|
-
index: draggedInfoIndex.value,
|
|
279
|
-
item: data[draggedInfoIndex.value],
|
|
280
|
-
separators: itemSeparators.current[draggedInfoIndex.value],
|
|
281
|
-
isDragged: true,
|
|
282
|
-
};
|
|
283
|
-
const draggedItemFallbackStyle = {
|
|
284
|
-
transform: itemOffsets[draggedInfoIndex.value]
|
|
285
|
-
? [
|
|
286
|
-
{
|
|
287
|
-
translateY: itemOffsets[draggedInfoIndex.value].value.offset -
|
|
288
|
-
scrollOffset.value,
|
|
289
|
-
},
|
|
290
|
-
]
|
|
291
|
-
: undefined,
|
|
292
|
-
};
|
|
293
|
-
return (React.createElement(PanGestureHandler, { maxPointers: 1, onGestureEvent: handleGestureEvent, onHandlerStateChange: handleGestureEvent, simultaneousHandlers: nativeHandler },
|
|
294
|
-
React.createElement(Animated.View, { ref: container, style: containerStyle, onLayout: handleContainerLayout },
|
|
295
|
-
React.createElement(NativeViewGestureHandler, { ref: nativeHandler },
|
|
296
|
-
React.createElement(AnimatedFlatList, Object.assign({}, rest, { ref: composeRefs(flatList, ref), data: data, CellRendererComponent: renderAnimatedCell, renderItem: renderDraggableItem, onLayout: handleFlatListLayout, onScroll: scrollHandler, keyExtractor: keyExtractor, scrollEventThrottle: 1, horizontal: false, numColumns: 1 }))),
|
|
297
|
-
dragged && (React.createElement(Animated.View, { style: [
|
|
298
|
-
styles.draggedItem,
|
|
299
|
-
draggedItemFallbackStyle,
|
|
300
|
-
draggedItemStyle,
|
|
301
|
-
] }, renderItem(draggedItemInfo))))));
|
|
302
|
-
};
|
|
303
|
-
const styles = {
|
|
304
|
-
draggedItem: {
|
|
305
|
-
position: 'absolute',
|
|
306
|
-
left: 0,
|
|
307
|
-
right: 0,
|
|
308
|
-
top: 0,
|
|
309
|
-
},
|
|
310
|
-
dragged: {
|
|
311
|
-
opacity: 0,
|
|
312
|
-
},
|
|
313
|
-
};
|
|
314
|
-
export default React.memo(React.forwardRef(ReorderableList));
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { ViewProps } from 'react-native';
|
|
3
|
-
import Animated from 'react-native-reanimated';
|
|
4
|
-
import { ItemOffset } from '../types/misc';
|
|
5
|
-
interface ReorderableListItemProps extends Animated.AnimateProps<ViewProps> {
|
|
6
|
-
index: number;
|
|
7
|
-
itemOffsets: Animated.SharedValue<ItemOffset>[];
|
|
8
|
-
draggedIndex: Animated.SharedValue<number>;
|
|
9
|
-
currentIndex: Animated.SharedValue<number>;
|
|
10
|
-
enabledOpacity: Animated.SharedValue<boolean>;
|
|
11
|
-
children: React.ReactNode;
|
|
12
|
-
}
|
|
13
|
-
declare const ReorderableListItem: React.FC<ReorderableListItemProps>;
|
|
14
|
-
export default ReorderableListItem;
|
|
@@ -1,57 +0,0 @@
|
|
|
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 from 'react';
|
|
13
|
-
import Animated, { useAnimatedStyle, withTiming, Easing, useSharedValue, useAnimatedReaction, } from 'react-native-reanimated';
|
|
14
|
-
const ReorderableListItem = (_a) => {
|
|
15
|
-
var { index, itemOffsets, draggedIndex, currentIndex, enabledOpacity, children } = _a, rest = __rest(_a, ["index", "itemOffsets", "draggedIndex", "currentIndex", "enabledOpacity", "children"]);
|
|
16
|
-
const translateY = useSharedValue(0);
|
|
17
|
-
const opacity = useSharedValue(1);
|
|
18
|
-
useAnimatedReaction(() => currentIndex.value, () => {
|
|
19
|
-
if (currentIndex.value >= 0 && draggedIndex.value >= 0) {
|
|
20
|
-
const moveDown = currentIndex.value > draggedIndex.value;
|
|
21
|
-
const startMove = Math.min(draggedIndex.value, currentIndex.value);
|
|
22
|
-
const endMove = Math.max(draggedIndex.value, currentIndex.value);
|
|
23
|
-
let newValue = 0;
|
|
24
|
-
if (index === draggedIndex.value) {
|
|
25
|
-
for (let i = startMove; i < endMove; i++) {
|
|
26
|
-
newValue = moveDown
|
|
27
|
-
? newValue + itemOffsets[i].value.length
|
|
28
|
-
: newValue - itemOffsets[i].value.length;
|
|
29
|
-
}
|
|
30
|
-
const offsetCorrection = moveDown
|
|
31
|
-
? itemOffsets[currentIndex.value].value.length -
|
|
32
|
-
itemOffsets[draggedIndex.value].value.length
|
|
33
|
-
: 0;
|
|
34
|
-
newValue += offsetCorrection;
|
|
35
|
-
}
|
|
36
|
-
else if (index >= startMove && index <= endMove) {
|
|
37
|
-
const draggedHeight = itemOffsets[draggedIndex.value].value.length;
|
|
38
|
-
newValue = moveDown ? -draggedHeight : draggedHeight;
|
|
39
|
-
}
|
|
40
|
-
if (newValue !== translateY.value) {
|
|
41
|
-
translateY.value = withTiming(newValue, {
|
|
42
|
-
duration: 100,
|
|
43
|
-
easing: Easing.out(Easing.ease),
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
useAnimatedReaction(() => enabledOpacity.value, (enabled) => {
|
|
49
|
-
opacity.value = enabled && index === draggedIndex.value ? 0 : 1;
|
|
50
|
-
}, [index]);
|
|
51
|
-
const animatedStyle = useAnimatedStyle(() => ({
|
|
52
|
-
transform: [{ translateY: translateY.value }],
|
|
53
|
-
opacity: opacity.value,
|
|
54
|
-
}));
|
|
55
|
-
return (React.createElement(Animated.View, Object.assign({}, rest, { style: [rest.style, animatedStyle] }), children));
|
|
56
|
-
};
|
|
57
|
-
export default ReorderableListItem;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from 'react';
|
|
2
|
-
import { cancelAnimation, makeMutable } from 'react-native-reanimated';
|
|
3
|
-
function useAnimatedSharedValues(initFunc, size, shrink = true) {
|
|
4
|
-
const ref = useRef([]);
|
|
5
|
-
if (size !== 0 && ref.current.length === 0) {
|
|
6
|
-
ref.current = [];
|
|
7
|
-
for (let i = 0; i < size; i++) {
|
|
8
|
-
ref.current[i] = makeMutable(initFunc(i));
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
if (size > ref.current.length) {
|
|
13
|
-
for (let i = ref.current.length; i < size; i++) {
|
|
14
|
-
ref.current[i] = makeMutable(initFunc(i));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
else if (shrink && size < ref.current.length) {
|
|
18
|
-
for (let i = size; i < ref.current.length; i++) {
|
|
19
|
-
cancelAnimation(ref.current[i]);
|
|
20
|
-
}
|
|
21
|
-
ref.current.splice(size, ref.current.length - size);
|
|
22
|
-
}
|
|
23
|
-
}, [size, initFunc, shrink]);
|
|
24
|
-
useEffect(() => {
|
|
25
|
-
return () => {
|
|
26
|
-
for (let i = 0; i < ref.current.length; i++) {
|
|
27
|
-
cancelAnimation(ref.current[i]);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}, []);
|
|
31
|
-
return ref.current;
|
|
32
|
-
}
|
|
33
|
-
export default useAnimatedSharedValues;
|
package/dist/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import ReorderableList from './components/ReorderableList';
|
|
2
|
-
import { ReorderableListProps, ReorderableListRenderItemInfo, ReorderableListReorderEvent } from './types/props';
|
|
3
|
-
export { ReorderableListProps, ReorderableListRenderItemInfo, ReorderableListReorderEvent, };
|
|
4
|
-
export default ReorderableList;
|
package/dist/index.js
DELETED
package/dist/types/misc.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export interface ItemOffset {
|
|
2
|
-
length: number;
|
|
3
|
-
offset: number;
|
|
4
|
-
}
|
|
5
|
-
export interface ItemSeparators {
|
|
6
|
-
highlight: () => void;
|
|
7
|
-
unhighlight: () => void;
|
|
8
|
-
updateProps: (select: 'leading' | 'trailing', newProps: any) => void;
|
|
9
|
-
}
|
|
10
|
-
export declare enum ReorderableListState {
|
|
11
|
-
IDLE = 0,
|
|
12
|
-
DRAGGING = 1,
|
|
13
|
-
RELEASING = 2,
|
|
14
|
-
AUTO_SCROLL = 3
|
|
15
|
-
}
|
package/dist/types/misc.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export var ReorderableListState;
|
|
2
|
-
(function (ReorderableListState) {
|
|
3
|
-
ReorderableListState[ReorderableListState["IDLE"] = 0] = "IDLE";
|
|
4
|
-
ReorderableListState[ReorderableListState["DRAGGING"] = 1] = "DRAGGING";
|
|
5
|
-
ReorderableListState[ReorderableListState["RELEASING"] = 2] = "RELEASING";
|
|
6
|
-
ReorderableListState[ReorderableListState["AUTO_SCROLL"] = 3] = "AUTO_SCROLL";
|
|
7
|
-
})(ReorderableListState || (ReorderableListState = {}));
|
package/dist/types/props.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
import { FlatListProps, ListRenderItemInfo, StyleProp, ViewStyle, LayoutChangeEvent } from 'react-native';
|
|
3
|
-
import Animated from 'react-native-reanimated';
|
|
4
|
-
export interface CellProps<T> {
|
|
5
|
-
index: number;
|
|
6
|
-
children?: React.ReactElement;
|
|
7
|
-
data: T[];
|
|
8
|
-
onLayout?: (e: LayoutChangeEvent) => void;
|
|
9
|
-
keyExtractor?: (item: T, index: number) => string;
|
|
10
|
-
}
|
|
11
|
-
export interface ReorderableListRenderItemInfo<T> extends ListRenderItemInfo<T> {
|
|
12
|
-
drag?: () => void;
|
|
13
|
-
isDragged?: boolean;
|
|
14
|
-
}
|
|
15
|
-
export interface ReorderableListReorderEvent {
|
|
16
|
-
fromIndex: number;
|
|
17
|
-
toIndex: number;
|
|
18
|
-
}
|
|
19
|
-
declare type UnsupportedProps = 'horizontal' | 'onScroll' | 'scrollEventThrottle' | 'numColumns';
|
|
20
|
-
export interface ReorderableListProps<T> extends Omit<FlatListProps<T>, UnsupportedProps> {
|
|
21
|
-
data: T[];
|
|
22
|
-
containerStyle?: StyleProp<Animated.AnimateStyle<StyleProp<ViewStyle>>>;
|
|
23
|
-
scrollAreaSize?: number;
|
|
24
|
-
scrollSpeed?: number;
|
|
25
|
-
dragScale?: number;
|
|
26
|
-
renderItem: (info: ReorderableListRenderItemInfo<T>) => React.ReactElement;
|
|
27
|
-
onReorder: (event: ReorderableListReorderEvent) => void;
|
|
28
|
-
}
|
|
29
|
-
export {};
|
package/dist/types/props.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|