Skip to content

Commit f62b4a8

Browse files
committed
chore(SwipeAction):Update Code and README
1 parent 06de79d commit f62b4a8

File tree

3 files changed

+70
-131
lines changed

3 files changed

+70
-131
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,20 @@ export default class SwipeActionView extends Component<SwipeActionProps> {
1818
{
1919
text: '查看备注',
2020
color: 'orange',
21-
x: 250,
2221
onPress: () => {
2322
this.newRef?.swipeable?.close();
2423
},
2524
},
2625
{
2726
text: '删除',
2827
color: 'red',
29-
x: 400,
3028
onPress: () => {
3129
this.newRef?.swipeable?.close();
3230
},
3331
},
3432
{
3533
text: '不显示',
3634
color: 'green',
37-
x: 400,
38-
onPress: () => {
39-
this.newRef?.swipeable?.close();
40-
},
41-
},
42-
];
43-
const left = [
44-
{
45-
text: '左侧查看',
46-
color: 'pink',
4735
onPress: () => {
4836
this.newRef?.swipeable?.close();
4937
},
@@ -56,9 +44,9 @@ export default class SwipeActionView extends Component<SwipeActionProps> {
5644
<Body>
5745
<Card title="左右滑动,显示按钮" style={styles.card}>
5846
<SwipeAction
47+
buttonWidth={80}
5948
ref={ref => (this.newRef = ref)}
6049
right={right}
61-
left={left}
6250
onSwipeableRightOpen={() => console.log('right')}
6351
onSwipeableLeftOpen={() => () => console.log('left')}>
6452
<View style={[styles.view]}>

packages/core/src/SwipeAction/README.md

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,17 @@ SwipeAction 滑动操作组件。
66

77
### 基础示例
88

9-
```jsx
9+
```tsx
1010
import React, { Component } from 'react';
11-
import { View, Text, StyleSheet } from 'react-native';
11+
import { StyleSheet } from 'react-native';
1212
import { SwipeAction } from '@uiw/react-native';
13-
import Layout, { Container } from '../../Layout';
14-
import { ComProps } from '../../routes';
15-
16-
const { Header, Body, Card, Footer } = Layout;
1713

18-
export interface SwipeActionProps extends ComProps {
14+
export interface SwipeActionProps {
1915
}
2016

2117
export default class SwipeActionView extends Component<SwipeActionProps> {
2218
newRef: any;
2319
render() {
24-
const { route } = this.props;
25-
const description = route.params.description;
26-
const title = route.params.title;
2720
const right = [
2821
{
2922
text: '查看备注',
@@ -50,37 +43,18 @@ export default class SwipeActionView extends Component<SwipeActionProps> {
5043
},
5144
},
5245
];
53-
const left = [
54-
{
55-
text: '左侧查看',
56-
color: 'pink',
57-
onPress: () => {
58-
this.newRef?.swipeable?.close()
59-
},
60-
},
61-
];
6246
return (
63-
<Container>
64-
<Layout>
65-
<Header title={title} description={description} />
66-
<Body>
67-
<Card title="左右滑动,显示按钮" style={styles.card}>
68-
<SwipeAction
69-
ref={ref => this.newRef = ref}
70-
right={right}
71-
left={left}
72-
onSwipeableRightOpen={() => console.log('right')}
73-
onSwipeableLeftOpen={() => () => console.log('left')}
74-
>
75-
<View style={[styles.view]}>
76-
<Text>滑动</Text>
77-
</View>
78-
</SwipeAction>
79-
</Card>
80-
</Body>
81-
<Footer />
82-
</Layout>
83-
</Container>
47+
<SwipeAction
48+
buttonWidth={60}
49+
ref={ref => this.newRef = ref}
50+
right={right}
51+
onSwipeableRightOpen={() => console.log('right')}
52+
onSwipeableLeftOpen={() => () => console.log('left')}
53+
>
54+
<View style={[styles.view]}>
55+
<Text>滑动</Text>
56+
</View>
57+
</SwipeAction>
8458
);
8559
}
8660
}
@@ -108,21 +82,21 @@ export interface Column {
10882
text: string;
10983
/** 背景色 */
11084
color: string;
111-
/** 滑动距离多少出现 */
112-
x?: number;
11385
/** 点击元素触发 */
11486
onPress?: () => void;
87+
/** 是否禁用 */
88+
disabled?: boolean;
11589
/** 自定义元素 */
116-
render?:(text:string,record: Column,index: number)=>React.ReactNode
90+
render?: (text: string, record: Column, index: number) => React.ReactNode;
11791
}
11892

11993
export interface SwipeActionProps {
12094
/** 右边滑动出来的元素 */
12195
right?: Array<Column>;
12296
/** 左边滑动出来的元素 */
12397
left?: Array<Column>;
124-
/** 滑动条宽度 默认20% */
125-
swipeWidth?:string | number
98+
/** 按钮宽度 默认60 */
99+
buttonWidth?:string | number
126100
/** 其余api参考文档 react-native-gesture-handler/Swipeable */
127101
enableTrackpadTwoFingerGesture?: boolean;
128102
friction?: number;

packages/core/src/SwipeAction/index.tsx

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useImperativeHandle, forwardRef, useRef, useCallback } from 'react';
1+
import React, { useImperativeHandle, forwardRef, useRef } from 'react';
22
import { Animated, StyleSheet, View, Text, I18nManager, StyleProp, ViewStyle } from 'react-native';
33
import { RectButton } from 'react-native-gesture-handler';
44
import Swipeable from 'react-native-gesture-handler/Swipeable';
@@ -8,10 +8,10 @@ export interface Column {
88
text: string;
99
/** 背景色 */
1010
color: string;
11-
/** 滑动距离多少出现 */
12-
x?: number;
1311
/** 点击元素触发 */
1412
onPress?: () => void;
13+
/** 是否禁用 */
14+
disabled?: boolean;
1515
/** 自定义元素 */
1616
render?: (text: string, record: Column, index: number) => React.ReactNode;
1717
}
@@ -21,7 +21,8 @@ export interface SwipeActionProps {
2121
right?: Array<Column>;
2222
/** 左边滑动出来的元素 */
2323
left?: Array<Column>;
24-
swipeWidth?: string | number;
24+
/** 按钮宽度 默认60 */
25+
buttonWidth?: number;
2526
enableTrackpadTwoFingerGesture?: boolean;
2627
friction?: number;
2728
leftThreshold?: number;
@@ -53,68 +54,49 @@ export interface SwipeActionProps {
5354
}
5455

5556
const SwipeAction = (props: SwipeActionProps, ref: any) => {
56-
const { children, right = [], left = [], swipeWidth = '20%', ...others } = props;
57+
const { children, right = [], left = [], buttonWidth = 60, ...others } = props;
5758
const swipeableRef: React.MutableRefObject<null> = useRef(null);
5859

59-
const renderRight = useCallback(() => {
60-
return renderRightAction;
61-
}, [right, swipeWidth]);
62-
63-
const renderLeft = useCallback(() => {
64-
return renderLeftAction;
65-
}, [left, swipeWidth]);
66-
67-
// 右侧滑出
68-
const renderRightAction = (progress: Animated.AnimatedInterpolation) => {
69-
return (
70-
right &&
71-
right.map(({ x = 1, text, color, onPress, render }, idx) => {
72-
const trans = progress.interpolate({
73-
inputRange: [0, 1],
74-
outputRange: [x, 0],
75-
});
76-
return (
77-
<View key={idx} style={[styles.viewActions, { width: swipeWidth }]}>
78-
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }}>
79-
<RectButton
80-
style={[styles.rightAction, { backgroundColor: color }]}
81-
onPress={() => {
82-
onPress && onPress();
83-
}}
84-
>
85-
{render ? render(text, right[idx], idx) : <Text style={styles.actionText}>{text}</Text>}
86-
</RectButton>
87-
</Animated.View>
88-
</View>
89-
);
90-
})
91-
);
92-
};
93-
// 左侧滑出
94-
const renderLeftAction = (progress: Animated.AnimatedInterpolation, dragX: any) => {
60+
// 滑出
61+
const renderRightAction = (progress: Animated.AnimatedInterpolation, dragX: any, isLeft = true) => {
62+
const buttons = isLeft ? left : right;
63+
if (!buttons) {
64+
return null;
65+
}
66+
const length = buttons.length;
67+
const width = buttonWidth * length;
9568
return (
96-
left &&
97-
left.map(({ text, color, onPress, render }, idx) => {
98-
const trans = dragX.interpolate({
99-
inputRange: [0, 50, 100, 101],
100-
outputRange: [-20, 0, 0, 1],
101-
extrapolate: 'clamp',
102-
});
103-
return (
104-
<View style={[styles.viewActions, { width: swipeWidth }]} key={idx}>
105-
<Animated.View style={[{ flex: 1, transform: [{ translateX: trans }] }]}>
106-
<RectButton
107-
style={[styles.rightAction, { backgroundColor: color }]}
108-
onPress={() => {
109-
onPress && onPress();
110-
}}
111-
>
112-
{render ? render(text, left[idx], idx) : <Text style={styles.actionText}>{text}</Text>}
113-
</RectButton>
114-
</Animated.View>
115-
</View>
116-
);
117-
})
69+
<View style={[styles.viewActions, { width: width }]}>
70+
{buttons &&
71+
buttons.map(({ text, color, onPress, disabled, render }, idx) => {
72+
const x = isLeft ? -idx * buttonWidth : (length - idx) * buttonWidth;
73+
const trans = progress.interpolate({
74+
inputRange: [0, 1],
75+
outputRange: [x, 0],
76+
extrapolate: 'clamp',
77+
});
78+
return (
79+
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }}>
80+
<RectButton
81+
style={[styles.rightAction, { backgroundColor: color }]}
82+
onPress={() => {
83+
if (disabled && disabled) {
84+
return;
85+
} else {
86+
onPress && onPress();
87+
}
88+
}}
89+
>
90+
{React.isValidElement(render) ? (
91+
render(text, right[idx], idx)
92+
) : (
93+
<Text style={[styles.actionText]}>{text}</Text>
94+
)}
95+
</RectButton>
96+
</Animated.View>
97+
);
98+
})}
99+
</View>
118100
);
119101
};
120102

@@ -128,11 +110,10 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
128110
ref={swipeableRef}
129111
friction={2}
130112
enableTrackpadTwoFingerGesture
131-
rightThreshold={50}
132-
leftThreshold={50}
133-
overshootRight={false}
134-
renderRightActions={renderRight()}
135-
renderLeftActions={renderLeft()}
113+
leftThreshold={30}
114+
rightThreshold={40}
115+
renderRightActions={(progress, dragX) => renderRightAction(progress, dragX, false)}
116+
renderLeftActions={(progress, dragX) => renderRightAction(progress, dragX, true)}
136117
{...others}
137118
>
138119
{children && children}
@@ -141,13 +122,9 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
141122
};
142123

143124
const styles = StyleSheet.create({
144-
leftAction: {
145-
alignItems: 'center',
146-
flex: 1,
147-
justifyContent: 'center',
148-
},
149125
actionText: {
150126
color: 'white',
127+
fontSize: 16,
151128
backgroundColor: 'transparent',
152129
textAlign: 'center',
153130
},

0 commit comments

Comments
 (0)