@hero-design/rn 8.3.4 → 8.5.0-alpha.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/.turbo/turbo-build.log +66 -9
- package/assets/fonts/hero-icons-mobile.ttf +0 -0
- package/es/index.js +473 -133
- package/lib/index.js +473 -133
- package/package.json +5 -7
- package/rollup.config.js +0 -1
- package/src/components/Carousel/CarouselItem.tsx +3 -1
- package/src/components/Carousel/StyledCarousel.tsx +2 -0
- package/src/components/Carousel/__tests__/__snapshots__/index.spec.tsx.snap +18 -15
- package/src/components/Icon/HeroIcon/glyphMap.json +1 -1
- package/src/components/Icon/IconList.ts +9 -0
- package/src/components/List/ListItem.tsx +1 -5
- package/src/components/List/__tests__/__snapshots__/ListItem.spec.tsx.snap +8 -8
- package/src/components/Select/BaseOptionList.tsx +5 -12
- package/src/components/Select/MultiSelect/OptionList.tsx +6 -6
- package/src/components/Select/MultiSelect/__tests__/__snapshots__/index.spec.tsx.snap +3 -0
- package/src/components/Select/MultiSelect/index.tsx +9 -0
- package/src/components/Select/SingleSelect/OptionList.tsx +5 -3
- package/src/components/Select/SingleSelect/__tests__/__snapshots__/OptionList.spec.tsx.snap +24 -4
- package/src/components/Select/SingleSelect/__tests__/__snapshots__/index.spec.tsx.snap +21 -3
- package/src/components/Select/SingleSelect/index.tsx +8 -0
- package/src/components/Swipeable/Buttons.tsx +65 -0
- package/src/components/Swipeable/StyledSwipeable.tsx +14 -2
- package/src/components/Swipeable/SwipeableAction.tsx +15 -7
- package/src/components/Swipeable/index.tsx +531 -136
- package/src/theme/__tests__/__snapshots__/index.spec.ts.snap +1 -1
- package/src/theme/components/carousel.ts +1 -1
- package/types/components/Calendar/helpers.d.ts +2 -2
- package/types/components/Select/helpers.d.ts +1 -1
- package/types/components/Swipeable/Buttons.d.ts +15 -0
- package/types/components/Swipeable/StyledSwipeable.d.ts +15 -2
- package/types/components/Swipeable/SwipeableAction.d.ts +10 -6
- package/types/components/Swipeable/index.d.ts +24 -41
- package/types/components/Toolbar/index.d.ts +2 -3
- package/types/testHelpers/renderWithTheme.d.ts +1 -1
- package/.turbo/turbo-publish:npm.log +0 -0
|
@@ -1,160 +1,555 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
ComponentProps,
|
|
3
|
-
ReactNode,
|
|
4
|
-
useCallback,
|
|
5
|
-
useEffect,
|
|
6
|
-
useRef,
|
|
7
|
-
} from 'react';
|
|
8
|
-
import { Animated, useWindowDimensions } from 'react-native';
|
|
1
|
+
import React, { useEffect } from 'react';
|
|
9
2
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
3
|
+
Animated,
|
|
4
|
+
Easing,
|
|
5
|
+
GestureResponderEvent,
|
|
6
|
+
PanResponder,
|
|
7
|
+
PanResponderGestureState,
|
|
8
|
+
StyleProp,
|
|
9
|
+
StyleSheet,
|
|
10
|
+
ViewStyle,
|
|
11
|
+
} from 'react-native';
|
|
12
|
+
import { noop } from '../../utils/functions';
|
|
13
|
+
import { scale } from '../../utils/scale';
|
|
14
|
+
import Buttons from './Buttons';
|
|
15
|
+
import { StyledContent, StyledWrapper } from './StyledSwipeable';
|
|
14
16
|
import SwipeableAction from './SwipeableAction';
|
|
15
17
|
|
|
16
|
-
type
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*/
|
|
37
|
-
children: ReactNode;
|
|
38
|
-
/**
|
|
39
|
-
* State of the component.
|
|
40
|
-
*/
|
|
41
|
-
state?: 'closed' | 'leftOpen' | 'rightOpen';
|
|
42
|
-
/**
|
|
43
|
-
* Callback when the state of the component changes.
|
|
44
|
-
*/
|
|
45
|
-
onStateChange?: (state: State) => void;
|
|
46
|
-
/**
|
|
47
|
-
* Action panel that is going to be revealed from the left side when user swipes right.
|
|
48
|
-
*/
|
|
49
|
-
leftActions?: ReactNode;
|
|
50
|
-
/**
|
|
51
|
-
* Width of the left action panel.
|
|
52
|
-
* By default, it will take up the whole width of the device.
|
|
53
|
-
*/
|
|
18
|
+
export type SwipeState = 'closed' | 'leftOpen' | 'rightOpen';
|
|
19
|
+
|
|
20
|
+
const swipeStartMinDistance = 15;
|
|
21
|
+
|
|
22
|
+
export type SwipeableProps = {
|
|
23
|
+
leftContent?: React.ReactNode;
|
|
24
|
+
rightContent?: React.ReactNode;
|
|
25
|
+
|
|
26
|
+
leftActions?: [typeof SwipeableAction][];
|
|
27
|
+
rightActions?: [typeof SwipeableAction][];
|
|
28
|
+
|
|
29
|
+
onSwipeLeftStart?: (
|
|
30
|
+
event: GestureResponderEvent,
|
|
31
|
+
gestureState: PanResponderGestureState
|
|
32
|
+
) => void;
|
|
33
|
+
onSwipeLeftEnd?: (
|
|
34
|
+
event: GestureResponderEvent,
|
|
35
|
+
gestureState: PanResponderGestureState
|
|
36
|
+
) => void;
|
|
37
|
+
|
|
54
38
|
leftActionsWidth?: number;
|
|
55
|
-
/**
|
|
56
|
-
* Action panel that is going to be revealed from the right side when user swipes left.
|
|
57
|
-
*/
|
|
58
|
-
rightActions?: ReactNode;
|
|
59
|
-
/**
|
|
60
|
-
* Width of the right action panel.
|
|
61
|
-
* By default, it will take up the whole width of the device.
|
|
62
|
-
*/
|
|
63
39
|
rightActionsWidth?: number;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
40
|
+
|
|
41
|
+
onStateChange?: (state: SwipeState) => void;
|
|
42
|
+
|
|
43
|
+
onSwipeRightStart?: (
|
|
44
|
+
event: GestureResponderEvent,
|
|
45
|
+
gestureState: PanResponderGestureState
|
|
46
|
+
) => void;
|
|
47
|
+
onSwipeRightEnd?: (
|
|
48
|
+
event: GestureResponderEvent,
|
|
49
|
+
gestureState: PanResponderGestureState
|
|
50
|
+
) => void;
|
|
51
|
+
|
|
52
|
+
leftButtonWidth?: number;
|
|
53
|
+
rightButtonWidth?: number;
|
|
54
|
+
|
|
55
|
+
swipeState?: SwipeState;
|
|
56
|
+
style?: StyleProp<ViewStyle>;
|
|
57
|
+
children: React.ReactNode;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
type State = {
|
|
61
|
+
pan: Animated.ValueXY;
|
|
62
|
+
lastOffset: { x: number; y: number };
|
|
63
|
+
leftActionActivated: boolean;
|
|
64
|
+
rightActionActivated: boolean;
|
|
65
|
+
swipeState: SwipeState;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const getReleaseAnimationConfig = (
|
|
69
|
+
swipeState: SwipeState,
|
|
70
|
+
props: {
|
|
71
|
+
totalLeftButtonsWidth: number;
|
|
72
|
+
totalRightButtonsWidth: number;
|
|
73
|
+
}
|
|
75
74
|
) => {
|
|
76
|
-
const
|
|
77
|
-
inputRange: [0, 1],
|
|
78
|
-
outputRange: direction === 'left' ? [-width, 0] : [width, 0],
|
|
79
|
-
extrapolate: 'clamp',
|
|
80
|
-
});
|
|
75
|
+
const { totalLeftButtonsWidth, totalRightButtonsWidth } = props;
|
|
81
76
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
77
|
+
const swipeReleaseAnimationConfig = {
|
|
78
|
+
toValue: { x: 0, y: 0 },
|
|
79
|
+
duration: 100,
|
|
80
|
+
easing: Easing.elastic(0.5),
|
|
81
|
+
useNativeDriver: true,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
if (swipeState === 'leftOpen') {
|
|
85
|
+
return {
|
|
86
|
+
...swipeReleaseAnimationConfig,
|
|
87
|
+
toValue: {
|
|
88
|
+
x: totalLeftButtonsWidth,
|
|
89
|
+
y: 0,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (swipeState === 'rightOpen') {
|
|
95
|
+
return {
|
|
96
|
+
...swipeReleaseAnimationConfig,
|
|
97
|
+
toValue: {
|
|
98
|
+
x: totalRightButtonsWidth * -1,
|
|
99
|
+
y: 0,
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return swipeReleaseAnimationConfig;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const getPos = ({
|
|
108
|
+
swipeState,
|
|
109
|
+
totalLeftButtonsWidth,
|
|
110
|
+
totalRightButtonsWidth,
|
|
111
|
+
}: {
|
|
112
|
+
swipeState: SwipeState;
|
|
113
|
+
totalLeftButtonsWidth: number;
|
|
114
|
+
totalRightButtonsWidth: number;
|
|
115
|
+
}) => {
|
|
116
|
+
if (swipeState === 'leftOpen') {
|
|
117
|
+
return {
|
|
118
|
+
x: totalLeftButtonsWidth,
|
|
119
|
+
y: 0,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (swipeState === 'rightOpen') {
|
|
124
|
+
return {
|
|
125
|
+
x: totalRightButtonsWidth * -1,
|
|
126
|
+
y: 0,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return { x: 0, y: 0 };
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
type PropsWithDefaultValue = Required<
|
|
134
|
+
Omit<SwipeableProps, 'leftContent' | 'rightContent' | 'style'>
|
|
135
|
+
> &
|
|
136
|
+
Pick<SwipeableProps, 'leftContent' | 'rightContent' | 'style'>;
|
|
137
|
+
type ContextValues = PropsWithDefaultValue & {
|
|
138
|
+
canSwipeLeft: boolean;
|
|
139
|
+
canSwipeRight: boolean;
|
|
140
|
+
totalLeftButtonsWidth: number;
|
|
141
|
+
totalRightButtonsWidth: number;
|
|
142
|
+
hasLeftButtons: boolean;
|
|
143
|
+
hasRightButtons: boolean;
|
|
144
|
+
unmountedRef: React.MutableRefObject<boolean>;
|
|
93
145
|
};
|
|
94
146
|
|
|
95
147
|
const Swipeable = ({
|
|
96
148
|
children,
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
149
|
+
leftActions = [],
|
|
150
|
+
leftContent,
|
|
151
|
+
rightActions = [],
|
|
152
|
+
rightContent,
|
|
153
|
+
style,
|
|
154
|
+
leftActionsWidth = scale(85),
|
|
155
|
+
leftButtonWidth = scale(85),
|
|
156
|
+
onSwipeLeftStart = noop,
|
|
157
|
+
onSwipeLeftEnd = noop,
|
|
158
|
+
rightActionsWidth = scale(85),
|
|
159
|
+
rightButtonWidth = scale(85),
|
|
160
|
+
onSwipeRightStart = noop,
|
|
161
|
+
onSwipeRightEnd = noop,
|
|
162
|
+
swipeState = 'closed',
|
|
163
|
+
onStateChange = noop,
|
|
164
|
+
...rest
|
|
104
165
|
}: SwipeableProps) => {
|
|
105
|
-
const
|
|
106
|
-
|
|
166
|
+
const propsWithDefaultValue: PropsWithDefaultValue = {
|
|
167
|
+
children,
|
|
168
|
+
leftActions,
|
|
169
|
+
leftContent,
|
|
170
|
+
rightActions,
|
|
171
|
+
rightContent,
|
|
172
|
+
style,
|
|
173
|
+
leftActionsWidth,
|
|
174
|
+
leftButtonWidth,
|
|
175
|
+
onSwipeLeftStart,
|
|
176
|
+
onSwipeLeftEnd,
|
|
177
|
+
rightActionsWidth,
|
|
178
|
+
rightButtonWidth,
|
|
179
|
+
onSwipeRightStart,
|
|
180
|
+
onSwipeRightEnd,
|
|
181
|
+
swipeState,
|
|
182
|
+
onStateChange,
|
|
183
|
+
...rest,
|
|
184
|
+
};
|
|
107
185
|
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
186
|
+
const unmountedRef = React.useRef(false);
|
|
187
|
+
useEffect(() => {
|
|
188
|
+
return () => {
|
|
189
|
+
unmountedRef.current = true;
|
|
190
|
+
};
|
|
191
|
+
}, []);
|
|
192
|
+
|
|
193
|
+
const totalLeftButtonsWidth = leftActions.length * leftButtonWidth;
|
|
194
|
+
const totalRightButtonsWidth = rightActions.length * rightButtonWidth;
|
|
195
|
+
|
|
196
|
+
const [width, setWidth] = React.useState(0);
|
|
197
|
+
const hasLeftButtons = !leftContent && leftActions && leftActions.length > 0;
|
|
198
|
+
|
|
199
|
+
const hasRightButtons =
|
|
200
|
+
!rightContent && rightActions && rightActions.length > 0;
|
|
201
|
+
const canSwipeRight = !!leftContent || !!hasLeftButtons;
|
|
202
|
+
|
|
203
|
+
const canSwipeLeft = !!rightContent || !!hasRightButtons;
|
|
204
|
+
|
|
205
|
+
const propsRef = React.useRef<ContextValues>(
|
|
206
|
+
undefined as unknown as ContextValues
|
|
122
207
|
);
|
|
123
208
|
|
|
209
|
+
const [state, setState] = React.useState<State>({
|
|
210
|
+
pan: new Animated.ValueXY(
|
|
211
|
+
getPos({
|
|
212
|
+
swipeState,
|
|
213
|
+
totalLeftButtonsWidth,
|
|
214
|
+
totalRightButtonsWidth,
|
|
215
|
+
})
|
|
216
|
+
),
|
|
217
|
+
lastOffset: { x: 0, y: 0 },
|
|
218
|
+
leftActionActivated: false,
|
|
219
|
+
rightActionActivated: true,
|
|
220
|
+
swipeState: 'closed',
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
propsRef.current = {
|
|
224
|
+
...propsWithDefaultValue,
|
|
225
|
+
canSwipeLeft,
|
|
226
|
+
canSwipeRight,
|
|
227
|
+
totalLeftButtonsWidth,
|
|
228
|
+
totalRightButtonsWidth,
|
|
229
|
+
hasLeftButtons,
|
|
230
|
+
hasRightButtons,
|
|
231
|
+
unmountedRef,
|
|
232
|
+
};
|
|
233
|
+
|
|
124
234
|
useEffect(() => {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
case 'closed':
|
|
135
|
-
swipeableRef.current.close();
|
|
136
|
-
break;
|
|
235
|
+
let animation: Animated.CompositeAnimation | null = null;
|
|
236
|
+
if (state.swipeState !== swipeState) {
|
|
237
|
+
animation = animationToNewState(
|
|
238
|
+
swipeState,
|
|
239
|
+
state.pan,
|
|
240
|
+
totalLeftButtonsWidth,
|
|
241
|
+
totalRightButtonsWidth
|
|
242
|
+
);
|
|
243
|
+
animation.start();
|
|
137
244
|
}
|
|
138
|
-
|
|
245
|
+
return () => {
|
|
246
|
+
if (animation) {
|
|
247
|
+
animation.stop();
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
}, [swipeState]);
|
|
251
|
+
|
|
252
|
+
const transform = [
|
|
253
|
+
{
|
|
254
|
+
translateX: state.pan.x.interpolate({
|
|
255
|
+
inputRange: [canSwipeLeft ? -width : 0, canSwipeRight ? width : 0],
|
|
256
|
+
outputRange: [
|
|
257
|
+
canSwipeLeft ? -width + StyleSheet.hairlineWidth : 0,
|
|
258
|
+
canSwipeRight ? width - StyleSheet.hairlineWidth : 0,
|
|
259
|
+
],
|
|
260
|
+
extrapolate: 'clamp',
|
|
261
|
+
}),
|
|
262
|
+
},
|
|
263
|
+
];
|
|
264
|
+
|
|
265
|
+
const panResponder = React.useRef(
|
|
266
|
+
PanResponder.create({
|
|
267
|
+
onMoveShouldSetPanResponder: (
|
|
268
|
+
_: GestureResponderEvent,
|
|
269
|
+
gestureState: PanResponderGestureState
|
|
270
|
+
) => {
|
|
271
|
+
return Math.abs(gestureState.dx) > swipeStartMinDistance;
|
|
272
|
+
},
|
|
273
|
+
onMoveShouldSetPanResponderCapture: (
|
|
274
|
+
_: GestureResponderEvent,
|
|
275
|
+
gestureState: PanResponderGestureState
|
|
276
|
+
) => {
|
|
277
|
+
return Math.abs(gestureState.dx) > swipeStartMinDistance;
|
|
278
|
+
},
|
|
279
|
+
onPanResponderGrant: () => {
|
|
280
|
+
setState((prevState) => {
|
|
281
|
+
prevState.pan.setOffset(prevState.lastOffset);
|
|
282
|
+
return prevState;
|
|
283
|
+
});
|
|
284
|
+
},
|
|
285
|
+
onPanResponderMove: (event, gestureState) => {
|
|
286
|
+
setState((prevState) => {
|
|
287
|
+
return hanleOnPanResponderMove(
|
|
288
|
+
prevState,
|
|
289
|
+
propsRef.current,
|
|
290
|
+
event,
|
|
291
|
+
gestureState
|
|
292
|
+
);
|
|
293
|
+
});
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
onPanResponderRelease: (event, gestureState) => {
|
|
297
|
+
setState((prevState) => {
|
|
298
|
+
return hanleOnPanResponderEnd(
|
|
299
|
+
prevState,
|
|
300
|
+
propsRef.current,
|
|
301
|
+
event,
|
|
302
|
+
gestureState
|
|
303
|
+
);
|
|
304
|
+
});
|
|
305
|
+
},
|
|
306
|
+
onPanResponderTerminationRequest: () => {
|
|
307
|
+
return false;
|
|
308
|
+
},
|
|
309
|
+
onPanResponderTerminate: (event, gestureState) => {
|
|
310
|
+
setState((prevState) => {
|
|
311
|
+
return hanleOnPanResponderEnd(
|
|
312
|
+
prevState,
|
|
313
|
+
propsRef.current,
|
|
314
|
+
event,
|
|
315
|
+
gestureState
|
|
316
|
+
);
|
|
317
|
+
});
|
|
318
|
+
},
|
|
319
|
+
})
|
|
320
|
+
).current;
|
|
139
321
|
|
|
140
322
|
return (
|
|
141
|
-
<
|
|
142
|
-
|
|
143
|
-
{
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
323
|
+
<StyledWrapper
|
|
324
|
+
onLayout={(event) => {
|
|
325
|
+
const { width } = event.nativeEvent.layout;
|
|
326
|
+
setWidth(width);
|
|
327
|
+
}}
|
|
328
|
+
style={[
|
|
329
|
+
{
|
|
330
|
+
flexDirection: 'row',
|
|
331
|
+
},
|
|
332
|
+
style,
|
|
333
|
+
]}
|
|
334
|
+
{...panResponder.panHandlers}
|
|
335
|
+
{...rest}
|
|
336
|
+
>
|
|
337
|
+
{canSwipeRight && (
|
|
338
|
+
<Animated.View style={[{ transform, marginLeft: -width, width }]}>
|
|
339
|
+
{leftContent || (
|
|
340
|
+
<Buttons
|
|
341
|
+
buttons={leftActions}
|
|
342
|
+
isLeftButtons
|
|
343
|
+
panAnimatedValue={state.pan}
|
|
344
|
+
width={width}
|
|
345
|
+
canSwipeLeft={canSwipeLeft}
|
|
346
|
+
canSwipeRight={canSwipeRight}
|
|
347
|
+
buttonWidth={leftButtonWidth}
|
|
348
|
+
/>
|
|
349
|
+
)}
|
|
350
|
+
</Animated.View>
|
|
351
|
+
)}
|
|
352
|
+
<StyledContent style={{ transform }}>{children}</StyledContent>
|
|
353
|
+
{canSwipeLeft && (
|
|
354
|
+
<Animated.View style={[{ transform, marginRight: -width, width }]}>
|
|
355
|
+
{rightContent || (
|
|
356
|
+
<Buttons
|
|
357
|
+
buttons={rightActions}
|
|
358
|
+
isLeftButtons={false}
|
|
359
|
+
panAnimatedValue={state.pan}
|
|
360
|
+
width={width}
|
|
361
|
+
canSwipeRight={canSwipeRight}
|
|
362
|
+
canSwipeLeft={canSwipeLeft}
|
|
363
|
+
buttonWidth={rightButtonWidth}
|
|
364
|
+
/>
|
|
365
|
+
)}
|
|
366
|
+
</Animated.View>
|
|
367
|
+
)}
|
|
368
|
+
</StyledWrapper>
|
|
154
369
|
);
|
|
155
370
|
};
|
|
156
371
|
|
|
157
|
-
|
|
372
|
+
const animationToNewState = (
|
|
373
|
+
swipeState: SwipeState,
|
|
374
|
+
pan: Animated.ValueXY,
|
|
375
|
+
totalLeftButtonsWidth: number,
|
|
376
|
+
totalRightButtonsWidth: number
|
|
377
|
+
) => {
|
|
378
|
+
const animationConfig = getReleaseAnimationConfig(swipeState, {
|
|
379
|
+
totalLeftButtonsWidth,
|
|
380
|
+
totalRightButtonsWidth,
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
pan.flattenOffset();
|
|
384
|
+
|
|
385
|
+
return Animated.timing(pan, animationConfig);
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
const hanleOnPanResponderEnd = (
|
|
389
|
+
state: State,
|
|
390
|
+
contextValues: ContextValues,
|
|
391
|
+
event: GestureResponderEvent,
|
|
392
|
+
gestureState: PanResponderGestureState
|
|
393
|
+
) => {
|
|
394
|
+
const {
|
|
395
|
+
totalLeftButtonsWidth,
|
|
396
|
+
totalRightButtonsWidth,
|
|
397
|
+
unmountedRef,
|
|
398
|
+
onSwipeLeftEnd,
|
|
399
|
+
onSwipeRightEnd,
|
|
400
|
+
onStateChange,
|
|
401
|
+
} = contextValues;
|
|
402
|
+
const { leftActionActivated, rightActionActivated, pan, swipeState } = state;
|
|
403
|
+
const animationConfig = getReleaseAnimationConfig(swipeState, {
|
|
404
|
+
totalLeftButtonsWidth,
|
|
405
|
+
totalRightButtonsWidth,
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
animationToNewState(
|
|
409
|
+
swipeState,
|
|
410
|
+
pan,
|
|
411
|
+
totalLeftButtonsWidth,
|
|
412
|
+
totalRightButtonsWidth
|
|
413
|
+
).start(() => {
|
|
414
|
+
if (unmountedRef.current) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (leftActionActivated && onSwipeLeftEnd) {
|
|
419
|
+
onSwipeLeftEnd(event, gestureState);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (rightActionActivated && onSwipeRightEnd) {
|
|
423
|
+
onSwipeRightEnd(event, gestureState);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
onStateChange(swipeState);
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
return {
|
|
430
|
+
...state,
|
|
431
|
+
lastOffset: {
|
|
432
|
+
x: animationConfig.toValue.x,
|
|
433
|
+
y: animationConfig.toValue.y,
|
|
434
|
+
},
|
|
435
|
+
leftActionActivated: false,
|
|
436
|
+
rightActionActivated: false,
|
|
437
|
+
};
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
const hanleOnPanResponderMove = (
|
|
441
|
+
state: State,
|
|
442
|
+
contextValues: ContextValues,
|
|
443
|
+
event: GestureResponderEvent,
|
|
444
|
+
gestureState: PanResponderGestureState
|
|
445
|
+
) => {
|
|
446
|
+
const {
|
|
447
|
+
leftActionsWidth,
|
|
448
|
+
rightActionsWidth,
|
|
449
|
+
onSwipeLeftStart,
|
|
450
|
+
onSwipeRightStart,
|
|
451
|
+
canSwipeRight,
|
|
452
|
+
canSwipeLeft,
|
|
453
|
+
hasLeftButtons,
|
|
454
|
+
hasRightButtons,
|
|
455
|
+
} = contextValues;
|
|
456
|
+
const { lastOffset, leftActionActivated, swipeState, rightActionActivated } =
|
|
457
|
+
state;
|
|
458
|
+
|
|
459
|
+
const { dx, vx } = gestureState;
|
|
460
|
+
const x = dx + lastOffset.x;
|
|
461
|
+
const isSwipingLeft = vx < 0;
|
|
462
|
+
const isSwipingRight = vx > 0;
|
|
463
|
+
|
|
464
|
+
const leftActionsActivated = swipeState === 'leftOpen';
|
|
465
|
+
const rightActionsActivated = swipeState === 'rightOpen';
|
|
466
|
+
let nextLeftActionActivated = leftActionActivated;
|
|
467
|
+
let nextLeftButtonsActivated = leftActionsActivated;
|
|
468
|
+
let nextRightActionActivated = rightActionActivated;
|
|
469
|
+
let nextRightButtonsActivated = rightActionsActivated;
|
|
470
|
+
|
|
471
|
+
Animated.event(
|
|
472
|
+
[
|
|
473
|
+
null,
|
|
474
|
+
{
|
|
475
|
+
dx: state.pan.x,
|
|
476
|
+
dy: state.pan.y,
|
|
477
|
+
},
|
|
478
|
+
],
|
|
479
|
+
{ useNativeDriver: false }
|
|
480
|
+
)(event, gestureState);
|
|
481
|
+
|
|
482
|
+
if (!leftActionActivated && canSwipeRight && x >= leftActionsWidth) {
|
|
483
|
+
nextLeftActionActivated = true;
|
|
484
|
+
onSwipeRightStart(event, gestureState);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (leftActionActivated && canSwipeRight && x < leftActionsWidth) {
|
|
488
|
+
nextLeftActionActivated = false;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (!rightActionActivated && canSwipeLeft && x <= -rightActionsWidth) {
|
|
492
|
+
nextRightActionActivated = true;
|
|
493
|
+
onSwipeLeftStart(event, gestureState);
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (rightActionActivated && canSwipeLeft && x > -rightActionsWidth) {
|
|
497
|
+
nextRightActionActivated = false;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (
|
|
501
|
+
!leftActionsActivated &&
|
|
502
|
+
hasLeftButtons &&
|
|
503
|
+
!isSwipingLeft &&
|
|
504
|
+
x >= leftActionsWidth
|
|
505
|
+
) {
|
|
506
|
+
nextLeftButtonsActivated = true;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (leftActionsActivated && hasLeftButtons && isSwipingLeft) {
|
|
510
|
+
nextLeftButtonsActivated = false;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (
|
|
514
|
+
!rightActionsActivated &&
|
|
515
|
+
hasRightButtons &&
|
|
516
|
+
!isSwipingRight &&
|
|
517
|
+
x <= -rightActionsWidth
|
|
518
|
+
) {
|
|
519
|
+
nextRightButtonsActivated = true;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (rightActionsActivated && hasRightButtons && isSwipingRight) {
|
|
523
|
+
nextRightButtonsActivated = false;
|
|
524
|
+
}
|
|
525
|
+
const needsUpdate =
|
|
526
|
+
nextLeftActionActivated !== leftActionActivated ||
|
|
527
|
+
nextLeftButtonsActivated !== leftActionsActivated ||
|
|
528
|
+
nextRightActionActivated !== rightActionActivated ||
|
|
529
|
+
nextRightButtonsActivated !== rightActionsActivated;
|
|
530
|
+
|
|
531
|
+
if (needsUpdate) {
|
|
532
|
+
const nextSwipeState = ((): SwipeState => {
|
|
533
|
+
if (nextLeftButtonsActivated) {
|
|
534
|
+
return 'leftOpen';
|
|
535
|
+
} else if (nextRightButtonsActivated) {
|
|
536
|
+
return 'rightOpen';
|
|
537
|
+
} else {
|
|
538
|
+
return 'closed';
|
|
539
|
+
}
|
|
540
|
+
})();
|
|
541
|
+
|
|
542
|
+
return {
|
|
543
|
+
...state,
|
|
544
|
+
leftActionActivated: nextLeftActionActivated,
|
|
545
|
+
rightActionActivated: nextRightActionActivated,
|
|
546
|
+
swipeState: nextSwipeState,
|
|
547
|
+
};
|
|
548
|
+
} else {
|
|
549
|
+
return state;
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
export default Object.assign(React.memo(Swipeable), {
|
|
158
554
|
Action: SwipeableAction,
|
|
159
|
-
Content: RectButton,
|
|
160
555
|
});
|