@@ -14,6 +14,7 @@ import {Constants} from '../../helpers';
14
14
15
15
16
16
const DRAG_TOSS = 0.05 ;
17
+ const LEFT_TOGGLE_THRESHOLD = 0.6 ;
17
18
18
19
// Math.sign polyfill for iOS 8.x
19
20
if ( ! Math . sign ) {
@@ -43,6 +44,7 @@ export type PropType = {
43
44
onSwipeableWillOpen ?: Function ,
44
45
onSwipeableWillClose ?: Function ,
45
46
onFullSwipeLeft ?: Function ,
47
+ onToggleSwipeLeft ?: Function ,
46
48
onWillFullSwipeLeft ?: Function ,
47
49
onFullSwipeRight ?: Function ,
48
50
onWillFullSwipeRight ?: Function ,
@@ -88,6 +90,7 @@ export default class Swipeable extends Component<PropType, StateType> {
88
90
// 1 -> closing to the left
89
91
// -1 -> closing to the right
90
92
this . rowState = 0 ;
93
+ this . dragFired = false ;
91
94
92
95
this . state = {
93
96
dragX,
@@ -101,10 +104,25 @@ export default class Swipeable extends Component<PropType, StateType> {
101
104
this . _updateAnimatedEvent ( props , this . state ) ;
102
105
103
106
this . _onGestureEvent = Animated . event ( [ { nativeEvent : { translationX : dragX } } ] , {
104
- useNativeDriver : props . useNativeAnimations
107
+ useNativeDriver : props . useNativeAnimations ,
108
+ listener : this . _handleDrag
105
109
} ) ;
106
110
}
107
111
112
+ _handleDrag = ( e ) => {
113
+ const { onToggleSwipeLeft} = this . props ;
114
+
115
+ if ( onToggleSwipeLeft && ! this . dragFired ) {
116
+ const { rowWidth, leftWidth} = this . state ;
117
+ const x = e . nativeEvent . translationX ;
118
+ const threshold = rowWidth * LEFT_TOGGLE_THRESHOLD ;
119
+ if ( x >= threshold && x < threshold + 10 ) {
120
+ this . dragFired = true ;
121
+ onToggleSwipeLeft ( { rowWidth, leftWidth, dragX : x } ) ;
122
+ }
123
+ }
124
+ }
125
+
108
126
// TODO: change to componentDidUpdate
109
127
UNSAFE_componentWillUpdate ( props : PropType , state : StateType ) {
110
128
if (
@@ -197,14 +215,18 @@ export default class Swipeable extends Component<PropType, StateType> {
197
215
const { leftWidth = 0 , rowWidth = 0 } = this . state ;
198
216
const { rightOffset = rowWidth } = this . state ;
199
217
const rightWidth = rowWidth - rightOffset ;
200
- const { fullSwipeLeft, fullSwipeRight, friction, leftThreshold = leftWidth / 2 , rightThreshold = rightWidth / 2 , fullLeftThreshold, fullRightThreshold} = this . props ;
218
+ const { fullSwipeLeft, fullSwipeRight, friction, leftThreshold = leftWidth / 2 , rightThreshold = rightWidth / 2 , fullLeftThreshold, fullRightThreshold, onToggleSwipeLeft } = this . props ;
201
219
202
220
const startOffsetX = this . _currentOffset ( ) + dragX / friction ;
203
221
const translationX = ( dragX + DRAG_TOSS * velocityX ) / friction ;
204
222
205
223
let toValue = 0 ;
206
224
if ( this . rowState === 0 ) {
207
- if ( fullSwipeLeft && translationX > rowWidth * fullLeftThreshold ) {
225
+ if ( onToggleSwipeLeft && translationX > leftWidth ) {
226
+ if ( ! this . dragFired ) {
227
+ toValue = rowWidth * LEFT_TOGGLE_THRESHOLD ;
228
+ }
229
+ } else if ( fullSwipeLeft && translationX > rowWidth * fullLeftThreshold ) {
208
230
toValue = rowWidth ;
209
231
} else if ( fullSwipeRight && translationX < - rowWidth * fullRightThreshold ) {
210
232
toValue = - rowWidth ;
@@ -229,7 +251,7 @@ export default class Swipeable extends Component<PropType, StateType> {
229
251
} ;
230
252
231
253
_animateRow = ( fromValue , toValue , velocityX ) => {
232
- const { dragX, rowTranslation, rowWidth} = this . state ;
254
+ const { dragX, rowTranslation, rowWidth, leftWidth } = this . state ;
233
255
const {
234
256
useNativeAnimations,
235
257
animationOptions,
@@ -242,6 +264,8 @@ export default class Swipeable extends Component<PropType, StateType> {
242
264
onSwipeableWillClose,
243
265
onSwipeableWillOpen,
244
266
onFullSwipeLeft,
267
+ fullSwipeLeft,
268
+ onToggleSwipeLeft,
245
269
onWillFullSwipeLeft,
246
270
onFullSwipeRight,
247
271
onWillFullSwipeRight
@@ -260,7 +284,7 @@ export default class Swipeable extends Component<PropType, StateType> {
260
284
useNativeDriver : useNativeAnimations ,
261
285
...animationOptions
262
286
} ) . start ( ( { finished} ) => {
263
- if ( finished ) {
287
+ if ( finished ) {
264
288
if ( toValue === rowWidth && onFullSwipeLeft ) {
265
289
onFullSwipeLeft ( ) ;
266
290
} else if ( toValue === - rowWidth && onFullSwipeRight ) {
@@ -279,7 +303,9 @@ export default class Swipeable extends Component<PropType, StateType> {
279
303
}
280
304
} ) ;
281
305
282
- if ( toValue === rowWidth && onWillFullSwipeLeft ) {
306
+ if ( toValue === rowWidth * LEFT_TOGGLE_THRESHOLD && onToggleSwipeLeft ) {
307
+ onToggleSwipeLeft ( { rowWidth, leftWidth} ) ;
308
+ } else if ( toValue === rowWidth && onWillFullSwipeLeft ) {
283
309
onWillFullSwipeLeft ( )
284
310
} else if ( toValue === - rowWidth && onWillFullSwipeRight ) {
285
311
onWillFullSwipeRight ( )
@@ -323,6 +349,11 @@ export default class Swipeable extends Component<PropType, StateType> {
323
349
this . _animateRow ( this . _currentOffset ( ) , rowWidth ) ;
324
350
} ;
325
351
352
+ toggleLeft = ( ) => {
353
+ const { rowWidth} = this . state ;
354
+ this . _animateRow ( this . _currentOffset ( ) , rowWidth * LEFT_TOGGLE_THRESHOLD ) ;
355
+ } ;
356
+
326
357
openRight = ( ) => {
327
358
const { rowWidth = 0 } = this . state ;
328
359
const { rightOffset = rowWidth } = this . state ;
0 commit comments