forked from yzsolo/react-native-swipe-left
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
356 lines (296 loc) · 9.83 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* a swipe left component. (消息列表左滑解决方案)
*/
'use strict';
import React, { Component } from 'react';
import {
View,
Text,
Image,
StyleSheet,
PanResponder,
Animated,
TouchableHighlight,
} from 'react-native';
export default class Swipes extends Component {
constructor(props) {
super(props);
let _width = _width || this._getBtnBoxWidth();
this.state = {
isOpen: false,
height: 0,
RowTranslateX: new Animated.Value(0),
BtnTranslateX: new Animated.Value(0)
};
}
_closeRow = () => {
let _width = _width || this._getBtnBoxWidth();
this._setIsOpenState(false);
this._setHasIdOpenState(false);
this.moving(this.state.RowTranslateX, 0);
this.moving(this.state.BtnTranslateX, 0);
}
_getBtnBoxWidth() {
let arr = [];
this.props.rightBtn.map(function(item){
return arr.push(item.width);
})
return arr.reduce(function(pre, cur) {
return pre + cur;
});
}
componentWillMount() {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => {
return (this.state.isOpen || this.props.root.state.hasIdOpen)? true : false;
},
onStartShouldSetPanResponderCapture: (evt, gestureState) => {
return (this.state.isOpen || this.props.root.state.hasIdOpen)? true : false;
},
onMoveShouldSetPanResponder: (evt, gestureState) => {return Math.abs(gestureState.dx) > 0;},
onMoveShouldSetPanResponderCapture: (evt, gestureState) => {return Math.abs(gestureState.dx) > 0;},
onPanResponderMove: (evt, gestureState) => {this._onPanResponderMove(evt, gestureState)},
onPanResponderRelease: (evt, gestureState) => {this._onPanResponderRelease(evt, gestureState)},
onPanResponderTerminate: (evt, gestureState) => {this._onPanResponderTerminate(evt, gestureState)},
})
}
_onPanResponderMove(evt, gestureState) {
let dx;
let _width = _width || this._getBtnBoxWidth();
let right = -_width;
if(Math.abs(gestureState.dx)>5) {
this.disallowScroll();
}
this.isRowMove();
if(!this.state.isOpen) {
dx = gestureState.dx;
if(dx < -10) {
dx += 10;
if(dx >= right) {
this.setState({
BtnTranslateX: new Animated.Value(dx)
});
} else {
this.setState({
BtnTranslateX: new Animated.Value(right)
});
}
if(dx < 50) {
this.setState({
RowTranslateX: new Animated.Value(dx)
});
}
}
} else {
dx = right + gestureState.dx;
if(dx>right) {
this.setState({
BtnTranslateX: new Animated.Value(dx)
})
}
if(dx < 50) {
this.setState({
RowTranslateX: new Animated.Value(dx)
})
}
}
}
_onPanResponderRelease(evt, gestureState) {
let toValue;
let isOpen;
let dx;
let _width = _width || this._getBtnBoxWidth();
let right = -_width;
let range;
if(this.state.isOpen || this.props.root.state.hasIdOpen) {
dx = right + gestureState.dx;
range = right + 40
} else {
dx = gestureState.dx;
range = -40;
}
if(dx<range && dx!== right) {
toValue = right;
isOpen = true;
this.disallowScroll();
this.isRowOpen();
this._setHasIdOpenState(true);
} else {
toValue = 0;
isOpen = false;
this.allowScroll();
/* 点击其他row关闭openRow */
this.closeRow();
}
this._setIsOpenState(isOpen);
this.moving(this.state.RowTranslateX, toValue);
this.moving(this.state.BtnTranslateX, toValue);
}
_onPanResponderTerminate(evt, gestureState) {
this.isTerminate();
let _width = _width || this._getBtnBoxWidth();
let right = -_width;
if(this.state.RowTranslateX._value < -2) {
this.disallowScroll();
this.isRowOpen();
this._setIsOpenState(true);
this._setHasIdOpenState(true);
this.moving(this.state.RowTranslateX, -_width);
this.moving(this.state.BtnTranslateX, -_width);
}
return false;
}
_setHasIdOpenState(bool) {
this.props.root.setState({
hasIdOpen: bool
})
}
_setIsOpenState(bool) {
this.setState({
isOpen: bool
});
}
_onPress = (item, rowId) => event => {
item.onPress(event, rowId)
}
moving(k, v) {
let type = this.props.animationType;
let duration = this.props.duration
if(type === 'timing'){
Animated.timing(k, {
toValue: v,
duration: duration
}).start();
} else if(type === 'spring') {
Animated.spring(k, {
toValue: v,
duration: duration
}).start();
}
}
isRowOpen() {
let root = this.props.root;
let id = this.props.id;
if(!root.openRowId)
root.openRowId = id;
}
isRowMove() {
let root = this.props.root;
let id = this.props.id;
if(root.openRowId && root.openRowId !== id && root._dataRow[root.openRowId]) {
root._dataRow[root.openRowId]._closeRow();
root.openRowId = '';
}
}
isTerminate() {
let root = this.props.root;
let id = this.props.id;
if(root.openRowId && root.openRowId !== id && root._dataRow[root.openRowId]) {
root._dataRow[root.openRowId]._closeRow();
}
root.openRowId = id;
}
closeRow() {
let root = this.props.root;
if(root.openRowId && root._dataRow[root.openRowId]) {
root._dataRow[root.openRowId]._closeRow();
}
}
allowScroll() {
let root = this.props.root;
/* 接入原生下拉 */
root.setState({
scrollEnable: true
})
/* 接入第三方下拉 */
/* 指定到像相应的scrollView对象 */
// root.refs.listview.refs.listviewscroll.setState({
// scrollEnabled: true
// });
}
disallowScroll() {
let root = this.props.root;
/* 接入原生下拉 */
root.setState({
scrollEnable: false
})
/* 接入第三方下拉 */
/* 指定到像相应的scrollView对象 */
// root.refs.listview.refs.listviewscroll.setState({
// scrollEnabled: false
// });
}
render() {
let _width = _width || this._getBtnBoxWidth();
const rowId = this.props.id
return (
<View onLayout={(e)=>{this.setState({height:e.nativeEvent.layout.height})}}>
<View ref='view' style={[styles.containerBox, {backgroundColor:this.props.boxbgColor}]}>
<Animated.View style={
[styles.container, {
backgroundColor: this.props.rowbgColor,
left: 0,
transform: [{translateX: this.state.RowTranslateX}]
}]}
{...this._panResponder.panHandlers}>
{this.props.children}
</Animated.View>
<Animated.View style={
[styles.deletebtnbox,{
width: _width,
right: -_width,
transform: [{translateX: this.state.BtnTranslateX}],
height: this.state.height}
]}>
{this.props.rightBtn.map((item)=>{
return <TouchableHighlight key={item.id} style={[styles.deletebtn, {width: item.width, height: this.state.height, backgroundColor: item.bgColor}]} onPress={this._onPress(item, rowId)} underlayColor={item.underlayColor}>
{item.text?
<Text style={{color: item.color||null, fontSize: item.fontSize||null}}>{item.text}</Text>
:
item.image?
<Image style={[styles.deleteBut, {width: 50, height: 50}]} source={{uri: item.image}} />
:
null}
</TouchableHighlight>
})}
</Animated.View>
</View>
</View>
);
}
}
Swipes.defaultProps = {
boxbgColor: '#eeeeee',
rowbgColor: '#ffffff',
animationType: 'timing',
duration: 150,
}
let styles = StyleSheet.create({
containerBox: {
flex: 1,
flexDirection: 'row',
justifyContent:'center',
overflow: 'hidden',
position: 'relative',
},
container: {
flex: 1,
// borderBottomWidth: 1,
// borderBottomColor: '#eeeeee',
overflow: 'hidden',
},
deletebtnbox: {
flex: 1,
position: 'absolute',
top: 0,
bottom: 0,
right: 0,
flexDirection: 'row',
alignItems: 'center',
justifyContent:'center',
},
deletebtn: {
alignItems: 'center',
justifyContent: 'center'
}
})