Skip to content

Commit 4e2def2

Browse files
committed
chore(SwipeAciton): fix key and Add render width of Column
1 parent e73a55d commit 4e2def2

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

packages/core/src/SwipeAction/README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,26 @@ const styles = StyleSheet.create({
103103
## Props
104104
组件继承react-native-gesture-handler[`Swipeable`](https://docs.swmansion.com/react-native-gesture-handler/docs/api/components/swipeable)
105105
```tsx
106+
export interface Column {
107+
/** 显示文字 */
108+
text: string;
109+
/** 背景色 */
110+
color: string;
111+
/** 滑动距离多少出现 */
112+
x?: number;
113+
/** 点击元素触发 */
114+
onPress?: () => void;
115+
/** 宽度 */
116+
width?: number|string,
117+
/** 自定义元素 */
118+
render?:(text:string,record: Column,index: number)=>React.ReactNode
119+
}
120+
106121
export interface SwipeActionProps {
107-
right?: Array<{
108-
text: string;
109-
color: string;
110-
x?: number;
111-
onPress?: () => void;
112-
}>;
113-
left?: Array<{
114-
text: string;
115-
color: string;
116-
onPress?: () => void;
117-
}>;
122+
/** 右边滑动出来的元素 */
123+
right?: Array<Column>;
124+
/** 左边滑动出来的元素 */
125+
left?: Array<Column>;
118126
// 滑动条宽度 20%
119127
swipeWidth?:string | number
120128
enableTrackpadTwoFingerGesture?: boolean;

packages/core/src/SwipeAction/index.tsx

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ import { Animated, StyleSheet, View, Text, I18nManager, StyleProp, ViewStyle, Di
33
import { RectButton } from 'react-native-gesture-handler';
44
import Swipeable from 'react-native-gesture-handler/Swipeable';
55

6+
export interface Column {
7+
/** 显示文字 */
8+
text: string;
9+
/** 背景色 */
10+
color: string;
11+
/** 滑动距离多少出现 */
12+
x?: number;
13+
/** 点击元素触发 */
14+
onPress?: () => void;
15+
/** 宽度 */
16+
width?: number | string;
17+
/** 自定义元素 */
18+
render?: (text: string, record: Column, index: number) => React.ReactNode;
19+
}
20+
621
export interface SwipeActionProps {
7-
right?: Array<{
8-
text: string;
9-
color: string;
10-
x?: number;
11-
onPress?: () => void;
12-
}>;
13-
left?: Array<{
14-
text: string;
15-
color: string;
16-
onPress?: () => void;
17-
}>;
22+
/** 右边滑动出来的元素 */
23+
right?: Array<Column>;
24+
/** 左边滑动出来的元素 */
25+
left?: Array<Column>;
1826
swipeWidth?: string | number;
1927
enableTrackpadTwoFingerGesture?: boolean;
2028
friction?: number;
@@ -55,26 +63,27 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
5563
return (
5664
right &&
5765
right.length > 0 &&
58-
right.map(({ x = 1, text, color, onPress }, idx) => {
66+
right.map(({ x = 1, text, color, onPress, width = '20%', render }, idx) => {
5967
const trans = progress.interpolate({
6068
inputRange: [0, 1],
6169
outputRange: [x, 0],
6270
});
6371
return (
6472
<View
73+
key={idx}
6574
style={{
66-
width: swipeWidth,
75+
width: width,
6776
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
6877
}}
6978
>
70-
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }} key={idx}>
79+
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }}>
7180
<RectButton
7281
style={[styles.rightAction, { backgroundColor: color }]}
7382
onPress={() => {
7483
onPress && onPress();
7584
}}
7685
>
77-
<Text style={styles.actionText}>{text}</Text>
86+
{render ? render(text, right[idx], idx) : <Text style={styles.actionText}>{text}</Text>}
7887
</RectButton>
7988
</Animated.View>
8089
</View>
@@ -87,7 +96,7 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
8796
return (
8897
left &&
8998
left.length > 0 &&
90-
left.map(({ text, color, onPress }, idx) => {
99+
left.map(({ text, color, onPress, width, render }, idx) => {
91100
const trans = dragX.interpolate({
92101
inputRange: [0, 50, 100, 101],
93102
outputRange: [-20, 0, 0, 1],
@@ -99,15 +108,16 @@ const SwipeAction = (props: SwipeActionProps, ref: any) => {
99108
width: swipeWidth,
100109
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
101110
}}
111+
key={idx}
102112
>
103-
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }} key={idx}>
113+
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }}>
104114
<RectButton
105115
style={[styles.rightAction, { backgroundColor: color }]}
106116
onPress={() => {
107117
onPress && onPress();
108118
}}
109119
>
110-
<Text style={styles.actionText}>{text}</Text>
120+
{render ? render(text, left[idx], idx) : <Text style={styles.actionText}>{text}</Text>}
111121
</RectButton>
112122
</Animated.View>
113123
</View>

0 commit comments

Comments
 (0)