Skip to content

Commit 6e1fd76

Browse files
committed
fix:Swipeable优化
1 parent 6440e56 commit 6e1fd76

File tree

3 files changed

+117
-29
lines changed

3 files changed

+117
-29
lines changed

example/examples/src/routes/SwipeAction/index.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,44 @@ const {Header, Body, Card, Footer} = Layout;
99
export interface SwipeActionProps extends ComProps {}
1010

1111
export default class SwipeActionView extends Component<SwipeActionProps> {
12+
newRef: any;
1213
render() {
1314
const {route} = this.props;
1415
const description = route.params.description;
1516
const title = route.params.title;
1617
const right = [
1718
{
18-
text: 'More',
19+
text: '查看备注',
1920
color: 'orange',
2021
x: 250,
21-
onPress: () => {},
22+
onPress: () => {
23+
this.newRef?.swipeable?.close();
24+
},
2225
},
2326
{
24-
text: 'Delete',
27+
text: '删除',
2528
color: 'red',
2629
x: 400,
27-
onPress: () => {},
30+
onPress: () => {
31+
this.newRef?.swipeable?.close();
32+
},
33+
},
34+
{
35+
text: '不显示',
36+
color: 'green',
37+
x: 400,
38+
onPress: () => {
39+
this.newRef?.swipeable?.close();
40+
},
41+
},
42+
];
43+
const left = [
44+
{
45+
text: '左侧查看',
46+
color: 'pink',
47+
onPress: () => {
48+
this.newRef?.swipeable?.close();
49+
},
2850
},
2951
];
3052
return (
@@ -34,11 +56,12 @@ export default class SwipeActionView extends Component<SwipeActionProps> {
3456
<Body>
3557
<Card title="左右滑动,显示按钮" style={styles.card}>
3658
<SwipeAction
59+
ref={ref => (this.newRef = ref)}
3760
right={right}
38-
onSwipeableRightOpen={() => {
39-
console.log('right');
40-
}}>
41-
<View style={styles.view}>
61+
left={left}
62+
onSwipeableRightOpen={() => console.log('right')}
63+
onSwipeableLeftOpen={() => () => console.log('left')}>
64+
<View style={[styles.view]}>
4265
<Text>滑动</Text>
4366
</View>
4467
</SwipeAction>

packages/core/src/SwipeAction/README.md

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,49 @@ import { ComProps } from '../../routes';
1515

1616
const { Header, Body, Card, Footer } = Layout;
1717

18-
export interface SwipeActionProps extends ComProps { }
18+
export interface SwipeActionProps extends ComProps {
19+
}
1920

2021
export default class SwipeActionView extends Component<SwipeActionProps> {
22+
newRef: any;
2123
render() {
2224
const { route } = this.props;
2325
const description = route.params.description;
2426
const title = route.params.title;
2527
const right = [
2628
{
27-
text: 'More',
28-
color: "orange",
29+
text: '查看备注',
30+
color: 'orange',
2931
x: 250,
30-
onPress: () => { },
32+
onPress: () => {
33+
this.newRef?.swipeable?.close()
34+
},
35+
},
36+
{
37+
text: '删除',
38+
color: 'red',
39+
x: 400,
40+
onPress: () => {
41+
this.newRef?.swipeable?.close()
42+
},
3143
},
3244
{
33-
text: 'Delete',
34-
color: "red",
45+
text: '不显示',
46+
color: 'green',
3547
x: 400,
36-
onPress: () => { },
37-
}
48+
onPress: () => {
49+
this.newRef?.swipeable?.close()
50+
},
51+
},
52+
];
53+
const left = [
54+
{
55+
text: '左侧查看',
56+
color: 'pink',
57+
onPress: () => {
58+
this.newRef?.swipeable?.close()
59+
},
60+
},
3861
];
3962
return (
4063
<Container>
@@ -43,9 +66,13 @@ export default class SwipeActionView extends Component<SwipeActionProps> {
4366
<Body>
4467
<Card title="左右滑动,显示按钮" style={styles.card}>
4568
<SwipeAction
69+
ref={ref => this.newRef = ref}
4670
right={right}
71+
left={left}
72+
onSwipeableRightOpen={() => console.log('right')}
73+
onSwipeableLeftOpen={() => () => console.log('left')}
4774
>
48-
<View style={styles.view}>
75+
<View style={[styles.view]}>
4976
<Text>滑动</Text>
5077
</View>
5178
</SwipeAction>
@@ -66,7 +93,7 @@ const styles = StyleSheet.create({
6693
paddingBottom: 30,
6794
},
6895
view: {
69-
height: 30,
96+
height: 30
7097
},
7198
});
7299

packages/core/src/SwipeAction/index.tsx

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import React, { useImperativeHandle, forwardRef, useRef } from 'react';
2-
import { Animated, StyleSheet, View, Text, I18nManager, StyleProp, ViewStyle } from 'react-native';
2+
import { Animated, StyleSheet, View, Text, I18nManager, StyleProp, ViewStyle, Dimensions } from 'react-native';
33
import { RectButton } from 'react-native-gesture-handler';
44
import Swipeable from 'react-native-gesture-handler/Swipeable';
55

66
export interface SwipeActionProps {
7-
right: Array<{
7+
right?: Array<{
88
text: string;
99
color: string;
1010
x?: number;
1111
onPress?: () => void;
1212
}>;
13+
left?: Array<{
14+
text: string;
15+
color: string;
16+
onPress?: () => void;
17+
}>;
1318
enableTrackpadTwoFingerGesture?: boolean;
1419
friction?: number;
1520
leftThreshold?: number;
@@ -41,11 +46,10 @@ export interface SwipeActionProps {
4146
}
4247

4348
const SwipeAction = (props: SwipeActionProps, ref: any) => {
44-
const { children, right = [], ...others } = props;
45-
const swipeableRef: any = useRef(null);
46-
47-
const close = () => swipeableRef?.current?.close();
49+
const { children, right = [], left = [], ...others } = props;
50+
const swipeableRef: React.MutableRefObject<null> = useRef(null);
4851

52+
// 右侧滑出
4953
const renderRightAction = (progress: Animated.AnimatedInterpolation) => {
5054
return (
5155
right &&
@@ -58,7 +62,40 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
5862
return (
5963
<View
6064
style={{
61-
width: 60,
65+
width: '20%',
66+
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
67+
}}
68+
>
69+
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }} key={idx}>
70+
<RectButton
71+
style={[styles.rightAction, { backgroundColor: color }]}
72+
onPress={() => {
73+
onPress && onPress();
74+
}}
75+
>
76+
<Text style={styles.actionText}>{text}</Text>
77+
</RectButton>
78+
</Animated.View>
79+
</View>
80+
);
81+
})
82+
);
83+
};
84+
// 左侧滑出
85+
const renderLeftAction = (progress: Animated.AnimatedInterpolation, dragX: any) => {
86+
return (
87+
left &&
88+
left.length > 0 &&
89+
left.map(({ text, color, onPress }, idx) => {
90+
const trans = dragX.interpolate({
91+
inputRange: [0, 50, 100, 101],
92+
outputRange: [-20, 0, 0, 1],
93+
extrapolate: 'clamp',
94+
});
95+
return (
96+
<View
97+
style={{
98+
width: '20%',
6299
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
63100
}}
64101
>
@@ -67,7 +104,6 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
67104
style={[styles.rightAction, { backgroundColor: color }]}
68105
onPress={() => {
69106
onPress && onPress();
70-
close();
71107
}}
72108
>
73109
<Text style={styles.actionText}>{text}</Text>
@@ -79,9 +115,9 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
79115
);
80116
};
81117

82-
// 暴露给父组件调用的方法
83-
useImperativeHandle(ref, (): any => ({
84-
close: close,
118+
// 暴露给父组件调用 Swipeable上的方法
119+
useImperativeHandle(ref, () => ({
120+
swipeable: swipeableRef.current,
85121
}));
86122

87123
return (
@@ -90,8 +126,10 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
90126
friction={2}
91127
enableTrackpadTwoFingerGesture
92128
rightThreshold={50}
129+
leftThreshold={50}
93130
overshootRight={false}
94131
renderRightActions={renderRightAction}
132+
renderLeftActions={renderLeftAction}
95133
{...others}
96134
>
97135
{children && children}

0 commit comments

Comments
 (0)