@momo-kits/swipe 0.77.8 → 0.78.2
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/index.tsx +127 -113
- package/package.json +1 -1
- package/types.ts +5 -5
package/index.tsx
CHANGED
|
@@ -1,130 +1,144 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, {
|
|
2
|
+
forwardRef,
|
|
3
|
+
ForwardRefRenderFunction,
|
|
4
|
+
useContext,
|
|
5
|
+
useImperativeHandle,
|
|
6
|
+
useRef,
|
|
7
|
+
} from 'react';
|
|
2
8
|
import {Animated, Dimensions, TouchableHighlight, View} from 'react-native';
|
|
3
9
|
import {Swipeable} from 'react-native-gesture-handler';
|
|
4
10
|
import {ApplicationContext, Colors, Icon, Text} from '@momo-kits/foundation';
|
|
5
|
-
import {SwipeAction, SwipeProps} from './types';
|
|
11
|
+
import {SwipeableRef, SwipeAction, SwipeProps} from './types';
|
|
6
12
|
import styles from './styles';
|
|
7
13
|
|
|
8
14
|
const ACTION_WIDTH = 56;
|
|
9
15
|
|
|
10
|
-
const Swipe:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
const Swipe: ForwardRefRenderFunction<SwipeableRef, SwipeProps> = (
|
|
17
|
+
{children, leftActions, rightActions, ...props},
|
|
18
|
+
ref,
|
|
19
|
+
) => {
|
|
20
|
+
const {theme} = useContext(ApplicationContext);
|
|
21
|
+
const screenWidth = Dimensions.get('window').width;
|
|
22
|
+
const swipeableRef = useRef<Swipeable>(null);
|
|
14
23
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
const renderActionItem = (
|
|
25
|
+
action: SwipeAction,
|
|
26
|
+
index: number,
|
|
27
|
+
length: number,
|
|
28
|
+
direction: 'left' | 'right',
|
|
29
|
+
dragX: Animated.AnimatedInterpolation,
|
|
30
|
+
) => {
|
|
31
|
+
const {
|
|
32
|
+
label,
|
|
33
|
+
icon,
|
|
34
|
+
onPress,
|
|
35
|
+
backgroundColor = theme.colors.highlight.primary,
|
|
36
|
+
} = action;
|
|
28
37
|
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
let inputRange = [0, ACTION_WIDTH * length];
|
|
39
|
+
let outputRange = [-ACTION_WIDTH * (index + 1), 0];
|
|
31
40
|
|
|
32
|
-
|
|
41
|
+
let zIndex = -index;
|
|
33
42
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const translateX = dragX.interpolate({
|
|
41
|
-
inputRange,
|
|
42
|
-
outputRange,
|
|
43
|
-
extrapolate: 'clamp',
|
|
44
|
-
});
|
|
43
|
+
if (direction === 'right') {
|
|
44
|
+
inputRange = [-ACTION_WIDTH * length, 0];
|
|
45
|
+
outputRange = [0, ACTION_WIDTH * (length - index)];
|
|
46
|
+
zIndex = index;
|
|
47
|
+
}
|
|
45
48
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
onPress={onPress}>
|
|
52
|
-
<Animated.View
|
|
53
|
-
style={[
|
|
54
|
-
styles.action,
|
|
55
|
-
{
|
|
56
|
-
backgroundColor,
|
|
57
|
-
transform: [{translateX}],
|
|
58
|
-
width: ACTION_WIDTH,
|
|
59
|
-
},
|
|
60
|
-
]}>
|
|
61
|
-
{!!icon && <Icon source={icon} color={Colors.black_01} />}
|
|
62
|
-
<Text
|
|
63
|
-
style={styles.textCenter}
|
|
64
|
-
numberOfLines={1}
|
|
65
|
-
color={Colors.black_01}
|
|
66
|
-
typography={'label_s'}>
|
|
67
|
-
{label}
|
|
68
|
-
</Text>
|
|
69
|
-
</Animated.View>
|
|
70
|
-
</TouchableHighlight>
|
|
71
|
-
);
|
|
72
|
-
};
|
|
49
|
+
const translateX = dragX.interpolate({
|
|
50
|
+
inputRange,
|
|
51
|
+
outputRange,
|
|
52
|
+
extrapolate: 'clamp',
|
|
53
|
+
});
|
|
73
54
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
item,
|
|
102
|
-
index,
|
|
103
|
-
actions?.length,
|
|
104
|
-
direction,
|
|
105
|
-
dragX,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
})}
|
|
109
|
-
</View>
|
|
110
|
-
);
|
|
111
|
-
};
|
|
55
|
+
return (
|
|
56
|
+
<TouchableHighlight
|
|
57
|
+
underlayColor={Colors.black_01}
|
|
58
|
+
activeOpacity={0.8}
|
|
59
|
+
style={{zIndex: zIndex}}
|
|
60
|
+
onPress={onPress}>
|
|
61
|
+
<Animated.View
|
|
62
|
+
style={[
|
|
63
|
+
styles.action,
|
|
64
|
+
{
|
|
65
|
+
backgroundColor,
|
|
66
|
+
transform: [{translateX}],
|
|
67
|
+
width: ACTION_WIDTH,
|
|
68
|
+
},
|
|
69
|
+
]}>
|
|
70
|
+
{!!icon && <Icon source={icon} color={Colors.black_01} />}
|
|
71
|
+
<Text
|
|
72
|
+
style={styles.textCenter}
|
|
73
|
+
numberOfLines={1}
|
|
74
|
+
color={Colors.black_01}
|
|
75
|
+
typography={'label_s'}>
|
|
76
|
+
{label}
|
|
77
|
+
</Text>
|
|
78
|
+
</Animated.View>
|
|
79
|
+
</TouchableHighlight>
|
|
80
|
+
);
|
|
81
|
+
};
|
|
112
82
|
|
|
83
|
+
const renderActions = (
|
|
84
|
+
actions: SwipeAction[] = [],
|
|
85
|
+
direction: 'left' | 'right',
|
|
86
|
+
dragX: Animated.AnimatedInterpolation,
|
|
87
|
+
) => {
|
|
88
|
+
let overlayPosition: {[key: string]: number} = {left: 0};
|
|
89
|
+
if (direction === 'right') {
|
|
90
|
+
overlayPosition = {right: 0};
|
|
91
|
+
}
|
|
113
92
|
return (
|
|
114
|
-
<
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
93
|
+
<View style={styles.row}>
|
|
94
|
+
<View
|
|
95
|
+
style={[
|
|
96
|
+
{
|
|
97
|
+
width: screenWidth,
|
|
98
|
+
backgroundColor: theme.colors.background.surface,
|
|
99
|
+
height: '100%',
|
|
100
|
+
zIndex: -10,
|
|
101
|
+
position: 'absolute',
|
|
102
|
+
},
|
|
103
|
+
overlayPosition,
|
|
104
|
+
]}
|
|
105
|
+
/>
|
|
106
|
+
{actions?.map((item, index) => {
|
|
107
|
+
//maximum 3 actions
|
|
108
|
+
if (index < 3) {
|
|
109
|
+
return renderActionItem(
|
|
110
|
+
item,
|
|
111
|
+
index,
|
|
112
|
+
actions?.length,
|
|
113
|
+
direction,
|
|
114
|
+
dragX,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
})}
|
|
118
|
+
</View>
|
|
126
119
|
);
|
|
127
|
-
}
|
|
128
|
-
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
useImperativeHandle(ref, () => ({
|
|
123
|
+
openLeft: () => swipeableRef?.current?.openLeft(),
|
|
124
|
+
openRight: () => swipeableRef?.current?.openRight(),
|
|
125
|
+
close: () => swipeableRef?.current?.close(),
|
|
126
|
+
}));
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<Swipeable
|
|
130
|
+
{...props}
|
|
131
|
+
containerStyle={styles.container}
|
|
132
|
+
ref={swipeableRef}
|
|
133
|
+
renderRightActions={(progressAnimatedValue, dragAnimatedValue) =>
|
|
134
|
+
renderActions(rightActions, 'right', dragAnimatedValue)
|
|
135
|
+
}
|
|
136
|
+
renderLeftActions={(progressAnimatedValue, dragAnimatedValue) =>
|
|
137
|
+
renderActions(leftActions, 'left', dragAnimatedValue)
|
|
138
|
+
}>
|
|
139
|
+
{children}
|
|
140
|
+
</Swipeable>
|
|
141
|
+
);
|
|
142
|
+
};
|
|
129
143
|
|
|
130
|
-
export default Swipe;
|
|
144
|
+
export default forwardRef(Swipe);
|
package/package.json
CHANGED
package/types.ts
CHANGED
|
@@ -90,10 +90,10 @@ export type SwipeProps = {
|
|
|
90
90
|
* about to close and return to its original position.
|
|
91
91
|
*/
|
|
92
92
|
onSwipeableWillClose?: () => void;
|
|
93
|
+
};
|
|
93
94
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
ref?: React.ForwardedRef<Swipeable>;
|
|
95
|
+
export type SwipeableRef = {
|
|
96
|
+
openLeft: () => void;
|
|
97
|
+
openRight: () => void;
|
|
98
|
+
close: () => void;
|
|
99
99
|
};
|