forked from summer88123/react-native-swipeout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
367 lines (318 loc) · 10.4 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
357
358
359
360
361
362
363
364
365
366
367
import tweenState from 'react-tween-state';
import NativeButton from './NativeButton';
import styles from './styles';
import React, {
Component,
PropTypes,
} from 'react';
import {
PanResponder,
TouchableHighlight,
StyleSheet,
Text,
View,
} from 'react-native';
const SwipeoutBtn = React.createClass({
propTypes: {
backgroundColor: PropTypes.string,
color: PropTypes.string,
component: PropTypes.node,
onPress: PropTypes.func,
text: PropTypes.string,
type: PropTypes.string,
underlayColor: PropTypes.string,
},
getDefaultProps: function() {
return {
backgroundColor: null,
color: null,
component: null,
underlayColor: null,
height: 0,
onPress: null,
disabled: false,
text: 'Click me',
type: '',
width: 0,
};
},
render: function() {
var btn = this.props;
var styleSwipeoutBtn = [styles.swipeoutBtn];
// apply "type" styles (delete || primary || secondary)
if (btn.type === 'delete') styleSwipeoutBtn.push(styles.colorDelete);
else if (btn.type === 'primary') styleSwipeoutBtn.push(styles.colorPrimary);
else if (btn.type === 'secondary') styleSwipeoutBtn.push(styles.colorSecondary);
// apply background color
if (btn.backgroundColor) styleSwipeoutBtn.push([{ backgroundColor: btn.backgroundColor }]);
styleSwipeoutBtn.push([{
height: btn.height,
width: btn.width,
}]);
var styleSwipeoutBtnComponent = [];
// set button dimensions
styleSwipeoutBtnComponent.push([{
height: btn.height,
width: btn.width,
}]);
var styleSwipeoutBtnText = [styles.swipeoutBtnText];
// apply text color
if (btn.color) styleSwipeoutBtnText.push([{ color: btn.color }]);
return (
<NativeButton
onPress={this.props.onPress}
underlayColor={this.props.underlayColor}
disabled={this.props.disabled}
style={[styles.swipeoutBtnTouchable, styleSwipeoutBtn]}
textStyle={styleSwipeoutBtnText}>
{
(btn.component ?
<View style={styleSwipeoutBtnComponent}>{btn.component}</View>
:
btn.text
)
}
</NativeButton>
);
}
});
const Swipeout = React.createClass({
mixins: [tweenState.Mixin],
propTypes: {
autoClose: PropTypes.bool,
backgroundColor: PropTypes.string,
close: PropTypes.bool,
left: PropTypes.array,
onOpen: PropTypes.func,
onClose: PropTypes.func,
right: PropTypes.array,
scroll: PropTypes.func,
style: View.propTypes.style,
sensitivity: PropTypes.number,
},
getDefaultProps: function() {
return {
rowID: -1,
sectionID: -1,
sensitivity: 0,
};
},
getInitialState: function() {
return {
autoClose: this.props.autoClose || false,
btnWidth: 0,
btnsLeftWidth: 0,
btnsRightWidth: 0,
contentHeight: 0,
contentPos: 0,
contentWidth: 0,
openedRight: false,
swiping: false,
tweenDuration: 160,
timeStart: null,
};
},
componentWillMount: function() {
this._panResponder = PanResponder.create({
onStartShouldSetPanResponder: (event, gestureState) => true,
onMoveShouldSetPanResponderCapture: (event, gestureState) =>
Math.abs(gestureState.dx) > this.props.sensitivity,
onPanResponderGrant: this._handlePanResponderGrant,
onPanResponderMove: this._handlePanResponderMove,
onPanResponderRelease: this._handlePanResponderEnd,
onPanResponderTerminate: this._handlePanResponderEnd,
onShouldBlockNativeResponder: (event, gestureState) => true,
});
},
componentWillReceiveProps: function(nextProps) {
if (nextProps.close) this._close();
},
_handlePanResponderGrant: function(e: Object, gestureState: Object) {
if (!this.state.openedLeft && !this.state.openedRight) {
if (this.props.onOpen) this.props.onOpen(this.props.sectionID, this.props.rowID);
} else {
if (this.props.onClose) this.props.onClose(this.props.sectionID, this.props.rowID);
}
this.refs.swipeoutContent.measure((ox, oy, width, height) => {
this.setState({
btnWidth: (width/5),
btnsLeftWidth: this.props.left ? (width/5)*this.props.left.length : 0,
btnsRightWidth: this.props.right ? (width/5)*this.props.right.length : 0,
swiping: true,
timeStart: (new Date()).getTime(),
});
});
},
_handlePanResponderMove: function(e: Object, gestureState: Object) {
var posX = gestureState.dx;
var posY = gestureState.dy;
var leftWidth = this.state.btnsLeftWidth;
var rightWidth = this.state.btnsRightWidth;
if (this.state.openedRight) var posX = gestureState.dx - rightWidth;
else if (this.state.openedLeft) var posX = gestureState.dx + leftWidth;
// prevent scroll if moveX is true
var moveX = Math.abs(posX) > Math.abs(posY);
if (this.props.scroll) {
if (moveX) this.props.scroll(false);
else this.props.scroll(true);
}
if (this.state.swiping) {
// move content to reveal swipeout
if (posX < 0 && this.props.right) this.setState({ contentPos: Math.min(posX, 0) });
else if (posX > 0 && this.props.left) this.setState({ contentPos: Math.max(posX, 0) });
}
},
_handlePanResponderEnd: function(e: Object, gestureState: Object) {
var posX = gestureState.dx;
var contentPos = this.state.contentPos;
var contentWidth = this.state.contentWidth;
var btnsLeftWidth = this.state.btnsLeftWidth;
var btnsRightWidth = this.state.btnsRightWidth;
// minimum threshold to open swipeout
var openX = contentWidth*0.33;
// should open swipeout
var openLeft = posX > openX || posX > btnsLeftWidth/2;
var openRight = posX < -openX || posX < -btnsRightWidth/2;
// account for open swipeouts
if (this.state.openedRight) var openRight = posX-openX < -openX;
if (this.state.openedLeft) var openLeft = posX+openX > openX;
// reveal swipeout on quick swipe
var timeDiff = (new Date()).getTime() - this.state.timeStart < 200;
if (timeDiff) {
var openRight = posX < -openX/10 && !this.state.openedLeft;
var openLeft = posX > openX/10 && !this.state.openedRight;
}
if (this.state.swiping) {
if (openRight && contentPos < 0 && posX < 0) {
// open swipeout right
this._tweenContent('contentPos', -btnsRightWidth);
this.setState({ contentPos: -btnsRightWidth, openedLeft: false, openedRight: true, swiping: false });
} else if (openLeft && contentPos > 0 && posX > 0) {
// open swipeout left
this._tweenContent('contentPos', btnsLeftWidth);
this.setState({ contentPos: btnsLeftWidth, openedLeft: true, openedRight: false, swiping: false });
}
else {
// close swipeout
this._tweenContent('contentPos', 0);
this.setState({ contentPos: 0, openedLeft: false, openedRight: false, swiping: false });
}
}
// Allow scroll
if (this.props.scroll) this.props.scroll(true);
},
_tweenContent: function(state, endValue) {
this.tweenState(state, {
easing: tweenState.easingTypes.easeInOutQuad,
duration: endValue === 0 ? this.state.tweenDuration*1.5 : this.state.tweenDuration,
endValue: endValue,
});
},
_rubberBandEasing: function(value, limit) {
if (value < 0 && value < limit) return limit - Math.pow(limit - value, 0.85);
else if (value > 0 && value > limit) return limit + Math.pow(value - limit, 0.85);
return value;
},
// close swipeout on button press
_autoClose: function(btn) {
var onPress = btn.onPress;
if (onPress) onPress();
if (this.state.autoClose) this._close();
},
_close: function() {
this._tweenContent('contentPos', 0);
this.setState({
openedRight: false,
openedLeft: false,
});
},
render: function() {
var contentWidth = this.state.contentWidth;
var posX = this.getTweeningValue('contentPos');
var styleSwipeout = [styles.swipeout, this.props.style];
if (this.props.backgroundColor) {
styleSwipeout.push([{ backgroundColor: this.props.backgroundColor }]);
}
var limit = -this.state.btnsRightWidth;
if (posX > 0) var limit = this.state.btnsLeftWidth;
var styleLeftPos = {
left: {
left: 0,
overflow: 'hidden',
width: Math.min(limit*(posX/limit), limit),
},
};
var styleRightPos = {
right: {
left: Math.abs(contentWidth + Math.max(limit, posX)),
right: 0,
},
};
var styleContentPos = {
content: {
left: this._rubberBandEasing(posX, limit),
},
};
var styleContent = [styles.swipeoutContent];
styleContent.push(styleContentPos.content);
var styleRight = [styles.swipeoutBtns];
styleRight.push(styleRightPos.right);
var styleLeft = [styles.swipeoutBtns];
styleLeft.push(styleLeftPos.left);
var isRightVisible = posX < 0;
var isLeftVisible = posX > 0;
return (
<View style={styleSwipeout}>
<View
ref="swipeoutContent"
style={styleContent}
onLayout={this._onLayout}
{...this._panResponder.panHandlers}
onStartShouldSetResponderCapture={() => (this.state.openedLeft || this.state.openedRight)}
>
{this.props.children}
</View>
{ this._renderButtons(this.props.right, isRightVisible, styleRight) }
{ this._renderButtons(this.props.left, isLeftVisible, styleLeft) }
</View>
);
},
_onLayout: function(event) {
var { width, height } = event.nativeEvent.layout;
this.setState({
contentWidth: width,
contentHeight: height,
});
},
_renderButtons: function(buttons, isVisible, style) {
if (buttons && isVisible) {
return( <View style={style}>
{ buttons.map(this._renderButton) }
</View>);
} else {
return (
<View/>
);
}
},
_renderButton: function(btn, i) {
return (
<SwipeoutBtn
backgroundColor={btn.backgroundColor}
color={btn.color}
component={btn.component}
disabled={btn.disabled}
height={this.state.contentHeight}
key={i}
onPress={() => this._autoClose(btn)}
text={btn.text}
type={btn.type}
underlayColor={btn.underlayColor}
width={this.state.btnWidth}
/>
);
}
})
Swipeout.NativeButton = NativeButton;
Swipeout.SwipeoutButton = SwipeoutBtn;
export default Swipeout;