-
-
Notifications
You must be signed in to change notification settings - Fork 994
/
Copy pathAppleStyleSwipeableRow.tsx
123 lines (115 loc) · 3.17 KB
/
AppleStyleSwipeableRow.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React, { Component, PropsWithChildren } from 'react';
import { Animated, StyleSheet, Text, View, I18nManager } from 'react-native';
import { RectButton, Swipeable } from 'react-native-gesture-handler';
export default class AppleStyleSwipeableRow extends Component<
PropsWithChildren<unknown>
> {
private renderLeftActions = (
_progress: Animated.AnimatedInterpolation<number>,
dragX: Animated.AnimatedInterpolation<number>
) => {
const trans = dragX.interpolate({
inputRange: [0, 50, 100, 101],
outputRange: [-20, 0, 0, 1],
extrapolate: 'clamp',
});
return (
<RectButton style={styles.leftAction} onPress={this.close}>
<Animated.Text
style={[
styles.actionText,
{
transform: [{ translateX: trans }],
},
]}>
Archive
</Animated.Text>
</RectButton>
);
};
private renderRightAction = (
text: string,
color: string,
x: number,
progress: Animated.AnimatedInterpolation<number>
) => {
const trans = progress.interpolate({
inputRange: [0, 1],
outputRange: [x, 0],
});
const pressHandler = () => {
this.close();
// eslint-disable-next-line no-alert
window.alert(text);
};
return (
<Animated.View style={{ flex: 1, transform: [{ translateX: trans }] }}>
<RectButton
style={[styles.rightAction, { backgroundColor: color }]}
onPress={pressHandler}>
<Text style={styles.actionText}>{text}</Text>
</RectButton>
</Animated.View>
);
};
private renderRightActions = (
progress: Animated.AnimatedInterpolation<number>,
_dragAnimatedValue: Animated.AnimatedInterpolation<number>
) => (
<View
style={{
width: 192,
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
}}>
{this.renderRightAction('More', '#C8C7CD', 192, progress)}
{this.renderRightAction('Flag', '#ffab00', 128, progress)}
{this.renderRightAction('More', '#dd2c00', 64, progress)}
</View>
);
private swipeableRow?: Swipeable;
private updateRef = (ref: Swipeable) => {
this.swipeableRow = ref;
};
private close = () => {
this.swipeableRow?.close();
};
render() {
const { children } = this.props;
return (
<Swipeable
ref={this.updateRef}
friction={2}
enableTrackpadTwoFingerGesture
leftThreshold={30}
rightThreshold={40}
renderLeftActions={this.renderLeftActions}
renderRightActions={this.renderRightActions}
onSwipeableOpen={(direction) => {
console.log(`Opening swipeable from the ${direction}`);
}}
onSwipeableClose={(direction) => {
console.log(`Closing swipeable to the ${direction}`);
}}>
{children}
</Swipeable>
);
}
}
const styles = StyleSheet.create({
leftAction: {
flex: 1,
backgroundColor: '#497AFC',
justifyContent: 'center',
},
actionText: {
color: 'white',
fontSize: 16,
backgroundColor: 'transparent',
padding: 10,
},
rightAction: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
});