-
Notifications
You must be signed in to change notification settings - Fork 274
/
swipeable-example.js
161 lines (146 loc) · 4.48 KB
/
swipeable-example.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React, {Component} from 'react';
import {ScrollView, StyleSheet, Text, TouchableOpacity, View} from 'react-native';
import Swipeable from 'react-native-swipeable';
export default class SwipeableExample extends Component {
state = {
currentlyOpenSwipeable: null
};
handleScroll = () => {
const {currentlyOpenSwipeable} = this.state;
if (currentlyOpenSwipeable) {
currentlyOpenSwipeable.recenter();
}
};
render() {
const {currentlyOpenSwipeable} = this.state;
const itemProps = {
onOpen: (event, gestureState, swipeable) => {
if (currentlyOpenSwipeable && currentlyOpenSwipeable !== swipeable) {
currentlyOpenSwipeable.recenter();
}
this.setState({currentlyOpenSwipeable: swipeable});
},
onClose: () => this.setState({currentlyOpenSwipeable: null})
};
return (
<ScrollView onScroll={this.handleScroll} style={styles.container}>
<Example1 {...itemProps}/>
<Example2 {...itemProps}/>
<Example3 {...itemProps}/>
</ScrollView>
);
}
}
function Example1({onOpen, onClose}) {
return (
<Swipeable
leftContent={(
<View style={[styles.leftSwipeItem, {backgroundColor: 'lightskyblue'}]}>
<Text>Pull action</Text>
</View>
)}
rightButtons={[
<TouchableOpacity style={[styles.rightSwipeItem, {backgroundColor: 'lightseagreen'}]}>
<Text>1</Text>
</TouchableOpacity>,
<TouchableOpacity style={[styles.rightSwipeItem, {backgroundColor: 'orchid'}]}>
<Text>2</Text>
</TouchableOpacity>
]}
onRightButtonsOpenRelease={onOpen}
onRightButtonsCloseRelease={onClose}
>
<View style={[styles.listItem, {backgroundColor: 'salmon'}]}>
<Text>Example 1</Text>
</View>
</Swipeable>
);
}
function Example2({onOpen, onClose}) {
return (
<Swipeable
leftButtonWidth={45}
leftButtons={[
<TouchableOpacity style={[styles.leftSwipeItem, {backgroundColor: 'papayawhip'}]}>
<Text>1</Text>
</TouchableOpacity>,
<TouchableOpacity style={[styles.leftSwipeItem, {backgroundColor: 'olivedrab'}]}>
<Text>2</Text>
</TouchableOpacity>,
<TouchableOpacity style={[styles.leftSwipeItem, {backgroundColor: 'mistyrose'}]}>
<Text>3</Text>
</TouchableOpacity>,
<TouchableOpacity style={[styles.leftSwipeItem, {backgroundColor: 'mediumaquamarine'}]}>
<Text>4</Text>
</TouchableOpacity>,
<TouchableOpacity style={[styles.leftSwipeItem, {backgroundColor: 'lightslategray'}]}>
<Text>5</Text>
</TouchableOpacity>,
<TouchableOpacity style={[styles.leftSwipeItem, {backgroundColor: 'ivory'}]}>
<Text>6</Text>
</TouchableOpacity>
]}
rightContent={(
<View style={[styles.rightSwipeItem, {backgroundColor: 'linen'}]}>
<Text>Pull action</Text>
</View>
)}
onLeftButtonsOpenRelease={onOpen}
onLeftButtonsCloseRelease={onClose}
>
<View style={[styles.listItem, {backgroundColor: 'paleturquoise'}]}>
<Text>Example 2</Text>
</View>
</Swipeable>
);
}
class Example3 extends Component {
state = {
leftActionActivated: false,
toggle: false
};
render() {
const {leftActionActivated, toggle} = this.state;
return (
<Swipeable
leftActionActivationDistance={200}
leftContent={(
<View style={[styles.leftSwipeItem, {backgroundColor: leftActionActivated ? 'lightgoldenrodyellow' : 'steelblue'}]}>
{leftActionActivated ?
<Text>release!</Text> :
<Text>keep pulling!</Text>}
</View>
)}
onLeftActionActivate={() => this.setState({leftActionActivated: true})}
onLeftActionDeactivate={() => this.setState({leftActionActivated: false})}
onLeftActionComplete={() => this.setState({toggle: !toggle})}
>
<View style={[styles.listItem, {backgroundColor: toggle ? 'thistle' : 'darkseagreen'}]}>
<Text>Example 3</Text>
</View>
</Swipeable>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 20
},
listItem: {
height: 75,
alignItems: 'center',
justifyContent: 'center'
},
leftSwipeItem: {
flex: 1,
alignItems: 'flex-end',
justifyContent: 'center',
paddingRight: 20
},
rightSwipeItem: {
flex: 1,
justifyContent: 'center',
paddingLeft: 20
},
});