forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericTouchable.js
More file actions
271 lines (248 loc) · 8.46 KB
/
GenericTouchable.js
File metadata and controls
271 lines (248 loc) · 8.46 KB
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
import React, { Component } from 'react';
import { Animated, Platform } from 'react-native';
import { State, BaseButton } from '../GestureHandler';
import PropTypes from 'prop-types';
/**
* Each touchable is a states' machine which preforms transitions.
* On very beginning (and on the very end or recognition) touchable is
* UNDETERMINED. Then it moves to BEGAN. If touchable recognizes that finger
* travel outside it transits to special MOVED_OUTSIDE state. Gesture recognition
* finishes in UNDETERMINED state.
*/
export const TOUCHABLE_STATE = {
UNDETERMINED: 0,
BEGAN: 1,
MOVED_OUTSIDE: 2,
};
const PublicPropTypes = {
// Decided to drop not used fields from RN's implementation.
// e.g. onBlur and onFocus as well as deprecated props.
accessible: PropTypes.bool,
accessibilityLabel: PropTypes.node,
accessibilityHint: PropTypes.string,
hitSlop: PropTypes.shape({
top: PropTypes.number,
left: PropTypes.number,
bottom: PropTypes.number,
right: PropTypes.number,
}),
disabled: PropTypes.bool,
onPress: PropTypes.func,
onPressIn: PropTypes.func,
onPressOut: PropTypes.func,
onLayout: PropTypes.func,
onLongPress: PropTypes.func,
nativeID: PropTypes.string,
testID: PropTypes.string,
delayPressIn: PropTypes.number,
delayPressOut: PropTypes.number,
delayLongPress: PropTypes.number,
};
const InternalPropTypes = {
extraButtonProps: PropTypes.object,
onStateChange: PropTypes.func,
};
/**
* GenericTouchable is not intented to be used as it.
* Should be treated as a source for the rest of touchables
*/
export default class GenericTouchable extends Component {
static publicPropTypes = PublicPropTypes;
static internalPropTypes = InternalPropTypes;
// The prop type collections have to be outside of the class, as metro
// at this time does not compile `this.foo` correctly if HMR is enabled.
// https://github.com/kmagiera/react-native-gesture-handler/pull/406#issuecomment-453779977
static propTypes = {
...InternalPropTypes,
...PublicPropTypes,
};
static defaultProps = {
delayLongPress: 600,
extraButtonProps: {
rippleColor: 'transparent',
},
};
// timeout handlers
pressInTimeout;
pressOutTimeout;
longPressTimeout;
// This flag is required since recognition of longPress implies not-invoking onPress
longPressDetected = false;
pointerInside = true;
// State of touchable
STATE = TOUCHABLE_STATE.UNDETERMINED;
// handlePressIn in called on first touch on traveling inside component.
// Handles state transition with delay.
handlePressIn = () => {
if (this.props.delayPressIn) {
this.pressInTimeout = setTimeout(() => {
this.moveToState(TOUCHABLE_STATE.BEGAN);
this.pressInTimeout = null;
}, this.props.delayPressIn);
} else {
this.moveToState(TOUCHABLE_STATE.BEGAN);
}
if (this.props.onLongPress) {
const time =
(this.props.delayPressIn || 0) + (this.props.delayLongPress || 0);
this.longPressTimeout = setTimeout(this.onLongPressDetected, time);
}
};
// handleMoveOutside in called on traveling outside component.
// Handles state transition with delay.
handleMoveOutside = () => {
if (this.props.delayPressOut) {
this.pressOutTimeout =
this.pressOutTimeout ||
setTimeout(() => {
this.moveToState(TOUCHABLE_STATE.MOVED_OUTSIDE);
this.pressOutTimeout = null;
}, this.props.delayPressOut);
} else {
this.moveToState(TOUCHABLE_STATE.MOVED_OUTSIDE);
}
};
// handleGoToUndetermined transits to UNDETERMINED state with proper delay
handleGoToUndetermined = () => {
clearTimeout(this.pressOutTimeout);
if (this.props.delayPressOut) {
this.pressOutTimeout = setTimeout(() => {
if (this.STATE === TOUCHABLE_STATE.UNDETERMINED) {
this.moveToState(TOUCHABLE_STATE.BEGAN);
}
this.moveToState(TOUCHABLE_STATE.UNDETERMINED);
this.pressOutTimeout = null;
}, this.props.delayPressOut);
} else {
if (this.STATE === TOUCHABLE_STATE.UNDETERMINED) {
this.moveToState(TOUCHABLE_STATE.BEGAN);
}
this.moveToState(TOUCHABLE_STATE.UNDETERMINED);
}
};
componentDidMount() {
this.reset();
}
// reset timeout to prevent memory leaks.
reset() {
this.longPressDetected = false;
this.pointerInside = true;
clearTimeout(this.pressInTimeout);
clearTimeout(this.pressOutTimeout);
clearTimeout(this.longPressTimeout);
this.pressOutTimeout = null;
this.longPressTimeout = null;
this.pressInTimeout = null;
}
// All states' transitions are defined here.
moveToState = newState => {
if (newState === this.STATE) {
// Ignore dummy transitions
return;
}
if (newState === TOUCHABLE_STATE.BEGAN) {
// First touch and moving inside
this.props.onPressIn && this.props.onPressIn();
} else if (newState === TOUCHABLE_STATE.MOVED_OUTSIDE) {
// Moving outside
this.props.onPressOut && this.props.onPressOut();
} else if (newState === TOUCHABLE_STATE.UNDETERMINED) {
// Need to reset each time on transition to UNDETERMINED
this.reset();
if (this.STATE === TOUCHABLE_STATE.BEGAN) {
// ... and if it happens inside button.
this.props.onPressOut && this.props.onPressOut();
}
}
// Finally call lister (used by subclasses)
this.props.onStateChange && this.props.onStateChange(this.STATE, newState);
// ... and make transition.
this.STATE = newState;
};
onGestureEvent = ({ nativeEvent: { pointerInside } }) => {
if (this.pointerInside !== pointerInside) {
if (pointerInside) {
this.onMoveIn();
} else {
this.onMoveOut();
}
}
this.pointerInside = pointerInside;
};
onHandlerStateChange = ({ nativeEvent }) => {
const { state } = nativeEvent;
if (state === State.CANCELLED || state === State.FAILED) {
// Need to handle case with external cancellation (e.g. by ScrollView)
this.moveToState(TOUCHABLE_STATE.UNDETERMINED);
} else if (
// This platform check is an implication of slightly different behavior of handlers on different platform.
// And Android "Active" state is achieving on first move of a finger, not on press in.
// On iOS event on "Began" is not delivered.
state === (Platform.OS === 'ios' ? State.ACTIVE : State.BEGAN) &&
this.STATE === TOUCHABLE_STATE.UNDETERMINED
) {
// Moving inside requires
this.handlePressIn();
} else if (state === State.END) {
const shouldCallOnPress =
!this.longPressDetected &&
this.STATE !== TOUCHABLE_STATE.MOVED_OUTSIDE &&
this.pressOutTimeout === null;
this.handleGoToUndetermined();
if (shouldCallOnPress) {
// Calls only inside component whether no long press was called previously
this.props.onPress && this.props.onPress();
}
}
};
onLongPressDetected = () => {
this.longPressDetected = true;
this.props.onLongPress();
};
componentWillUnmount() {
// to prevent memory leaks
this.reset();
}
onMoveIn = () => {
if (this.STATE === TOUCHABLE_STATE.MOVED_OUTSIDE) {
// This call is not throttles with delays (like in RN's implementation).
this.moveToState(TOUCHABLE_STATE.BEGAN);
}
};
onMoveOut = () => {
// long press shouldn't be longer detected
clearTimeout(this.longPressTimeout);
this.longPressTimeout = null;
if (this.STATE === TOUCHABLE_STATE.BEGAN) {
this.handleMoveOutside();
}
};
render() {
const coreProps = {
accessible: this.props.accessible !== false,
accessibilityLabel: this.props.accessibilityLabel,
accessibilityHint: this.props.accessibilityHint,
accessibilityComponentType: this.props.accessibilityComponentType,
accessibilityRole: this.props.accessibilityRole,
accessibilityStates: this.props.accessibilityStates,
accessibilityTraits: this.props.accessibilityTraits,
nativeID: this.props.nativeID,
testID: this.props.testID,
onLayout: this.props.onLayout,
hitSlop: this.props.hitSlop,
};
return (
<BaseButton
onHandlerStateChange={
this.props.disabled ? null : this.onHandlerStateChange
}
onGestureEvent={this.onGestureEvent}
hitSlop={this.props.hitSlop}
{...this.props.extraButtonProps}>
<Animated.View {...coreProps} style={this.props.style}>
{this.props.children}
</Animated.View>
</BaseButton>
);
}
}