Skip to content

Commit 0e356f8

Browse files
committed
Fix various microusecases
1 parent 9546120 commit 0e356f8

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed

src/Row.js

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,35 @@ export default class Row extends Component {
4040
}
4141

4242
_panResponder = PanResponder.create({
43-
onStartShouldSetPanResponder: () => !this.props.disabled,
44-
onMoveShouldSetPanResponder: () => !this.props.disabled,
43+
onStartShouldSetPanResponder: () => !this._isDisabled(),
44+
onMoveShouldSetPanResponder: () => !this._isDisabled(),
4545

4646
onPanResponderGrant: (e, gestureState) => {
4747
e.persist();
48+
this._wasLongPress = false;
4849

4950
this._longPressTimer = setTimeout(() => {
50-
this._recentlyReleased = false;
51-
this._prevGestureState = {...gestureState};
52-
51+
this._wasLongPress = true;
52+
this._target = e.nativeEvent.target;
53+
this._prevGestureState = {
54+
...gestureState,
55+
moveX: gestureState.x0,
56+
moveY: gestureState.y0,
57+
};
5358
this._toggleActive(e, gestureState);
5459
}, ACTIVATION_DELAY);
5560
},
5661

5762
onPanResponderMove: (e, gestureState) => {
58-
if (!this._active) {
63+
if (
64+
!this._active ||
65+
gestureState.numberActiveTouches > 1 ||
66+
e.nativeEvent.target !== this._target
67+
) {
68+
if (!this._isTouchInsideElement(e)) {
69+
this._cancelLongPress();
70+
}
71+
5972
return;
6073
}
6174

@@ -69,9 +82,16 @@ export default class Row extends Component {
6982
},
7083

7184
onPanResponderRelease: (e, gestureState) => {
72-
this._cancelLongPress();
85+
if (this._wasLongPress) {
86+
this._toggleActive(e, gestureState);
87+
88+
} else if (this._isTouchInsideElement(e)) {
89+
this._cancelLongPress();
7390

74-
this._toggleActive(e, gestureState);
91+
if (this.props.onPress) {
92+
this.props.onPress();
93+
}
94+
}
7595
},
7696

7797
onPanResponderTerminationRequest: () => {
@@ -87,6 +107,16 @@ export default class Row extends Component {
87107

88108
onPanResponderTerminate: () => {
89109
this._cancelLongPress();
110+
111+
// If responder terminated while dragging,
112+
// deactivate the element and move to the initial location.
113+
if (this._active) {
114+
this._toggleActive(e, gestureState);
115+
116+
if (shallowEqual(this.props.location, this._location)) {
117+
this._relocate(this.props.location);
118+
}
119+
}
90120
},
91121
});
92122

@@ -118,7 +148,7 @@ export default class Row extends Component {
118148
<Animated.View
119149
{...this._panResponder.panHandlers}
120150
style={[style, styles.container, this._animatedLocation.getLayout()]}
121-
onLayout={this.props.onLayout}>
151+
onLayout={this._onLayout}>
122152
{children}
123153
</Animated.View>
124154
);
@@ -132,9 +162,12 @@ export default class Row extends Component {
132162
this._location = nextLocation;
133163

134164
if (animated) {
165+
this._isAnimationRunning = true;
135166
Animated.timing(this._animatedLocation, {
136167
toValue: nextLocation,
137-
}).start();
168+
}).start(() => {
169+
this._isAnimationRunning = false;
170+
});
138171
} else {
139172
this._animatedLocation.setValue(nextLocation);
140173
}
@@ -152,13 +185,34 @@ export default class Row extends Component {
152185

153186
_mapGestureToMove(prevGestureState, gestureState) {
154187
return {
155-
dy: gestureState.dy - prevGestureState.dy,
188+
dy: gestureState.moveY - prevGestureState.moveY,
156189
};
157190
}
158191

192+
_isDisabled() {
193+
return this.props.disabled ||
194+
this._isAnimationRunning && this.props.disabledDuringAnimation;
195+
}
196+
197+
_isTouchInsideElement({nativeEvent}) {
198+
return this._layout &&
199+
nativeEvent.locationX >= 0 &&
200+
nativeEvent.locationX <= this._layout.width &&
201+
nativeEvent.locationY >= 0 &&
202+
nativeEvent.locationY <= this._layout.height;
203+
}
204+
159205
_onChangeLocation = (value) => {
160206
this._location = value;
161207
};
208+
209+
_onLayout = (e) => {
210+
this._layout = e.nativeEvent.layout;
211+
212+
if (this.props.onLayout) {
213+
this.props.onLayout(e);
214+
}
215+
};
162216
}
163217

164218
const styles = StyleSheet.create({

0 commit comments

Comments
 (0)