-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
486 lines (414 loc) · 13.2 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import Btn from './components/btn';
import React, {
Component,
} from 'react';
import {
Animated,
PanResponder,
StyleSheet,
View,
} from 'react-native';
import PropTypes from 'prop-types';
const styles = StyleSheet.create({
slideOutContainer: {
position: 'absolute',
top: 0, bottom: 0,
left: 0, right: 0,
overflow: 'hidden',
},
btnsContainer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
overflow: 'hidden',
},
container: {
flex: 1,
flexDirection: 'row',
},
});
// Position of the left of the swipable item when closed
const CLOSED_LEFT_POSITION = 0;
// Minimum swipe distance before we recognize it as such
const HORIZONTAL_SWIPE_DISTANCE_THRESHOLD = 10;
// Minimum swipe speed before we fully animate the user's action (open/close)
const HORIZONTAL_FULL_SWIPE_SPEED_THRESHOLD = 0.3;
// Factor to divide by to get slow speed; i.e. 4 means 1/4 of full speed
const SLOW_SPEED_SWIPE_FACTOR = 4;
// Time, in milliseconds, of how long the animated swipe should be
const SWIPE_DURATION = 300;
/**
* On SwipeableListView mount, the 1st item will bounce to show users it's
* possible to swipe
*/
const ON_MOUNT_BOUNCE_DELAY = 700;
const ON_MOUNT_BOUNCE_DURATION = 400;
// Distance left of closed position to bounce back when right-swiping from closed
const RIGHT_SWIPE_BOUNCE_BACK_DISTANCE = 30;
const RIGHT_SWIPE_BOUNCE_BACK_DURATION = 300;
/**
* Max distance of right swipe to allow (right swipes do functionally nothing).
* Must be multiplied by SLOW_SPEED_SWIPE_FACTOR because gestureState.dx tracks
* how far the finger swipes, and not the actual animation distance.
*/
const RIGHT_SWIPE_THRESHOLD = 30 * SLOW_SPEED_SWIPE_FACTOR;
class SwipeableView extends Component {
static propTypes = {
children: PropTypes.any,
isOpen: PropTypes.bool,
onOpen: PropTypes.func,
onClose: PropTypes.func,
onSwipeEnd: PropTypes.func,
onSwipeStart: PropTypes.func,
autoClose: PropTypes.bool,
// Should bounce the row on mount
shouldBounceOnMount: PropTypes.bool,
/**
* A ReactElement that is unveiled when the user swipes
*/
btnsArray: PropTypes.array.isRequired,
/**
* The minimum swipe distance required before fully animating the swipe. If
* the user swipes less than this distance, the item will return to its
* previous (open/close) position.
*/
swipeThreshold: PropTypes.number,
/**
* True/false if the current language is right to left
*/
isRTL: PropTypes.bool,
};
static defaultProps = {
isOpen: false,
onOpen: () => {},
onClose: () => {},
onSwipeEnd: () => {},
onSwipeStart: () => {},
swipeThreshold: 30,
shouldBounceOnMount: false,
autoClose: false,
isRTL: false,
};
constructor(props) {
super(props);
this._panResponder = {};
this._previousLeft = CLOSED_LEFT_POSITION;
this.state = {
btnWidthDefault: 0,
btnWidths: [],
width: 0,
height: 0,
currentLeft: new Animated.Value(this._previousLeft),
/**
* In order to render component A beneath component B, A must be rendered
* before B. However, this will cause "flickering", aka we see A briefly
* then B. To counter this, _isSwipeableViewRendered flag is used to set
* component A to be transparent until component B is loaded.
*/
isSwipeableViewRendered: false,
rowHeight: null,
maxSwipeDistance: 0,
};
this._swipeoutRef = null;
}
componentWillMount() {
this._panResponder = PanResponder.create({
onMoveShouldSetPanResponderCapture: this._handleMoveShouldSetPanResponderCapture.bind(this),
onPanResponderGrant: this._handlePanResponderGrant.bind(this),
onPanResponderMove: this._handlePanResponderMove.bind(this),
onPanResponderRelease: this._handlePanResponderEnd.bind(this),
onPanResponderTerminationRequest: this._onPanResponderTerminationRequest.bind(this),
onPanResponderTerminate: this._handlePanResponderEnd.bind(this),
onShouldBlockNativeResponder: () => false,
});
}
componentDidMount() {
const { shouldBounceOnMount } = this.props;
if (shouldBounceOnMount) {
/**
* Do the on mount bounce after a delay because if we animate when other
* components are loading, the animation will be laggy
*/
this._timeout = setTimeout(() => {
this._animateBounceBack(ON_MOUNT_BOUNCE_DURATION);
}, ON_MOUNT_BOUNCE_DELAY);
}
setTimeout(this._measureSwipeout.bind(this));
}
componentWillUnmount() {
if (this._timeout) {
clearTimeout(this._timeout);
}
}
componentWillReceiveProps(nextProps) {
const { isOpen } = this.props;
/**
* We do not need an "animateOpen(noCallback)" because this animation is
* handled internally by this component.
*/
if (isOpen && !nextProps.isOpen) {
this._animateToClosedPosition();
}
}
shouldComponentUpdate(nextProps) {
const { shouldBounceOnMount } = this.props;
if (shouldBounceOnMount && !nextProps.shouldBounceOnMount) {
// No need to rerender if SwipeableListView is disabling the bounce flag
return false;
}
return true;
}
_onPanResponderTerminationRequest() {
return false;
}
_animateTo(toValue, duration = SWIPE_DURATION, callback = () => {}) {
Animated.timing(
this.state.currentLeft,
{
duration,
toValue,
},
).start(() => {
this._previousLeft = toValue;
callback();
});
}
_animateToClosedPosition(duration = SWIPE_DURATION) {
this._animateTo(CLOSED_LEFT_POSITION, duration);
}
_animateToClosedPositionDuringBounce() {
this._animateToClosedPosition(RIGHT_SWIPE_BOUNCE_BACK_DURATION);
}
_animateToOpenPosition() {
const { isRTL } = this.props;
const { maxSwipeDistance } = this.state;
this._animateTo(isRTL ? maxSwipeDistance : -maxSwipeDistance);
}
_animateToOpenPositionWith(speed, distMoved) {
const { isRTL } = this.props;
const { maxSwipeDistance } = this.state;
/**
* Ensure the speed is at least the set speed threshold to prevent a slow
* swiping animation
*/
speed = (
speed > HORIZONTAL_FULL_SWIPE_SPEED_THRESHOLD ?
speed :
HORIZONTAL_FULL_SWIPE_SPEED_THRESHOLD
);
/**
* Calculate the duration the row should take to swipe the remaining distance
* at the same speed the user swiped (or the speed threshold)
*/
const duration = Math.abs((maxSwipeDistance - Math.abs(distMoved)) / speed);
this._animateTo(isRTL ? maxSwipeDistance : -maxSwipeDistance, duration);
}
_animateBounceBack(duration) {
/**
* When swiping right, we want to bounce back past closed position on release
* so users know they should swipe right to get content.
*/
const { isRTL } = this.props;
const swipeBounceBackDistance = isRTL ?
-RIGHT_SWIPE_BOUNCE_BACK_DISTANCE :
RIGHT_SWIPE_BOUNCE_BACK_DISTANCE;
this._animateTo(
-swipeBounceBackDistance,
duration,
this._animateToClosedPositionDuringBounce.bind(this),
);
}
_shouldAnimateRemainder(gestureState) {
/**
* If user has swiped past a certain distance, animate the rest of the way
* if they let go
*/
return (
Math.abs(gestureState.dx) > this.props.swipeThreshold ||
gestureState.vx > HORIZONTAL_FULL_SWIPE_SPEED_THRESHOLD
);
}
_handlePanResponderEnd(event, gestureState) {
const { onOpen, onClose, onSwipeEnd, isRTL } = this.props;
const horizontalDistance = isRTL ? -gestureState.dx : gestureState.dx;
if (this._isSwipingRightFromClosed(gestureState)) {
onOpen && onOpen();
this._animateBounceBack(RIGHT_SWIPE_BOUNCE_BACK_DURATION);
} else if (this._shouldAnimateRemainder(gestureState)) {
if (horizontalDistance < 0) {
// Swiped left
onOpen && onOpen();
this._animateToOpenPositionWith(gestureState.vx, horizontalDistance);
} else {
// Swiped right
onClose && onClose();
this._animateToClosedPosition();
}
} else {
if (this._previousLeft === CLOSED_LEFT_POSITION) {
this._animateToClosedPosition();
} else {
this._animateToOpenPosition();
}
}
onSwipeEnd && onSwipeEnd();
}
_swipeFullSpeed(gestureState) {
this.state.currentLeft.setValue(this._previousLeft + gestureState.dx);
}
_swipeSlowSpeed(gestureState) {
this.state.currentLeft.setValue(
this._previousLeft + gestureState.dx / SLOW_SPEED_SWIPE_FACTOR
);
}
_isSwipingRightFromClosed(gestureState) {
const { isRTL } = this.props;
const gestureStateDx = isRTL ? -gestureState.dx : gestureState.dx;
return this._previousLeft === CLOSED_LEFT_POSITION && gestureStateDx > 0;
}
_isSwipingExcessivelyRightFromClosedPosition(gestureState) {
/**
* We want to allow a BIT of right swipe, to allow users to know that
* swiping is available, but swiping right does not do anything
* functionally.
*/
const { isRTL } = this.props;
const gestureStateDx = isRTL ? -gestureState.dx : gestureState.dx;
return (
this._isSwipingRightFromClosed(gestureState) &&
gestureStateDx > RIGHT_SWIPE_THRESHOLD
);
}
_handlePanResponderMove(event, gestureState) {
const { onSwipeStart } = this.props;
if (this._isSwipingExcessivelyRightFromClosedPosition(gestureState)) {
return;
}
onSwipeStart && onSwipeStart();
if (this._isSwipingRightFromClosed(gestureState)) {
this._swipeSlowSpeed(gestureState);
} else {
this._swipeFullSpeed(gestureState);
}
}
_handlePanResponderGrant() {
}
_handleMoveShouldSetPanResponderCapture(event, gestureState) {
// Decides whether a swipe is responded to by this component or its child
return gestureState.dy < 10 && this._isValidSwipe(gestureState);
}
_isValidSwipe(gestureState) {
return Math.abs(gestureState.dx) > HORIZONTAL_SWIPE_DISTANCE_THRESHOLD;
}
_btnWidth(btn) {
const hasCustomWidth = btn.props && btn.props.style && btn.props.style.width;
return hasCustomWidth ? btn.props.style.width : false;
}
_btnsWidthTotal(width, group) {
const customWidths = [];
group && group.forEach(btn => {
this._btnWidth(btn) ? customWidths.push(this._btnWidth(btn)) : null;
});
const customWidthTotal = customWidths.reduce((a, b) => a + b, 0);
const defaultWidth = (width - customWidthTotal) / (5 - customWidths.length);
const defaultWidthsTotal = ((group ? group.length : 0) - customWidths.length) * defaultWidth;
this.setState({
btnWidthDefault: defaultWidth,
});
return customWidthTotal + defaultWidthsTotal;
}
_setBtnsWidth(btns) {
const { btnWidthDefault } = this.state;
const btnWidths = [];
btns && btns.forEach(btn => {
btnWidths.push(this._btnWidth(btn) ? this._btnWidth(btn) : btnWidthDefault);
});
this.setState({
btnWidths,
});
}
_handleBtnPress(btn) {
const { autoClose } = this.props;
if (btn) {
btn.onPress && btn.onPress();
(btn.autoClose || autoClose) && this._animateToClosedPosition();
}
}
_measureSwipeout() {
if (this._swipeoutRef) {
this._swipeoutRef.measure((a, b, width, height) => {
const { btnsArray } = this.props;
this.setState({
height: height,
width: width,
maxSwipeDistance: this._btnsWidthTotal(width, btnsArray),
});
this._setBtnsWidth(btnsArray);
});
}
}
_returnBtnDimensions() {
const { height, width } = this.state;
return {
height: height,
width: width,
};
}
_onSwipeableViewLayout(event) {
this.setState({
isSwipeableViewRendered: true,
rowHeight: event.nativeEvent.layout.height,
});
}
_renderSlideoutBtns() {
const { btnWidths } = this.state;
if (btnWidths.length <= 0) {
return false;
}
return this.props.btnsArray.map((btn, i) => {
const btnProps = btn.props ? btn.props : [];
return (
<Btn
key={i}
panDimensions={{ width: btnWidths[i], height: this.state.rowHeight }}
text={btn.text}
type={btn.type}
component={btn.component}
{...btnProps}
onPress={() => this._handleBtnPress(btn)}/>
);
});
}
render() {
const { isRTL } = this.props;
// The view hidden behind the main view
let btnsArray;
if (this.state.isSwipeableViewRendered && this.state.rowHeight) {
btnsArray = (
<View style={[styles.slideOutContainer, { height: this.state.rowHeight }]}>
<View style={[ styles.btnsContainer, isRTL ? { justifyContent: 'flex-start' } : { justifyContent: 'flex-end' } ]}>
{ this._renderSlideoutBtns() }
</View>
</View>
);
}
// The swipeable item
const swipeableView = (
<Animated.View
onLayout={this._onSwipeableViewLayout.bind(this)}
style={{transform: [{translateX: this.state.currentLeft}], backgroundColor: 'white'}}>
{this.props.children}
</Animated.View>
);
return (
<View
ref={ (ref) => (this._swipeoutRef = ref) }
{...this._panResponder.panHandlers}
collapsable={false}>
{btnsArray}
{swipeableView}
</View>
);
}
}
module.exports = SwipeableView;