Skip to content

Commit 0f1cdfe

Browse files
troZeeferrannp
authored andcommitted
fix: disable touch event once user is dragging (#124)
1 parent bd41245 commit 0f1cdfe

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

js/ViewPager.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ function getViewManagerConfig(viewManagerName) {
8383
*/
8484

8585
class ViewPager extends React.Component<ViewPagerProps> {
86+
isScrolling = false;
87+
8688
componentDidMount() {
8789
// On iOS we do it directly on the native side
8890
if (Platform.OS === 'android') {
@@ -114,6 +116,7 @@ class ViewPager extends React.Component<ViewPagerProps> {
114116
if (this.props.onPageScrollStateChanged) {
115117
this.props.onPageScrollStateChanged(e);
116118
}
119+
this.isScrolling = e.nativeEvent.pageScrollState === 'dragging';
117120
};
118121

119122
_onPageSelected = (e: PageSelectedEvent) => {
@@ -159,6 +162,13 @@ class ViewPager extends React.Component<ViewPagerProps> {
159162
);
160163
};
161164

165+
_onMoveShouldSetResponderCapture = () => {
166+
if (Platform.OS === 'ios') {
167+
return this.isScrolling;
168+
}
169+
return false;
170+
};
171+
162172
render() {
163173
return (
164174
<NativeViewPager
@@ -168,6 +178,7 @@ class ViewPager extends React.Component<ViewPagerProps> {
168178
onPageScroll={this._onPageScroll}
169179
onPageScrollStateChanged={this._onPageScrollStateChanged}
170180
onPageSelected={this._onPageSelected}
181+
onMoveShouldSetResponderCapture={this._onMoveShouldSetResponderCapture}
171182
children={childrenWithOverriddenStyle(this.props.children)}
172183
/>
173184
);

js/types.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
* @format
88
* @flow strict-local
99
*/
10-
10+
import * as React from 'react';
11+
import {View} from 'react-native';
1112
import type {Node} from 'react';
1213
import type {SyntheticEvent} from 'react-native/Libraries/Types/CoreEventTypes';
1314
import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
1415

16+
type ViewProps = React.ElementProps<typeof View>;
17+
type ResponderCaptureType = $PropertyType<
18+
ViewProps,
19+
'onMoveShouldSetResponderCapture',
20+
>;
21+
1522
export type PageScrollState = 'idle' | 'dragging' | 'settling';
1623

1724
export type PageScrollEvent = SyntheticEvent<
@@ -95,6 +102,17 @@ export type ViewPagerProps = $ReadOnly<{|
95102

96103
style?: ?ViewStyleProp,
97104

105+
/**
106+
* If a parent `View` wants to prevent a child `View` from becoming responder
107+
* on a move, it should have this handler which returns `true`.
108+
*
109+
* `View.props.onMoveShouldSetResponderCapture: (event) => [true | false]`,
110+
* where `event` is a synthetic touch event as described above.
111+
*
112+
* See http://facebook.github.io/react-native/docs/view.html#onMoveShouldsetrespondercapture
113+
*/
114+
onMoveShouldSetResponderCapture?: ?ResponderCaptureType,
115+
98116
/**
99117
* iOS only
100118
*/

typings/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export interface ViewPagerOnPageSelectedEventData {
1010
position: number;
1111
}
1212

13+
export interface PageScrollStateChangedEvent {
14+
pageScrollState: 'idle' | 'dragging' | 'settling';
15+
}
16+
1317
export interface ViewPagerProps extends ReactNative.ViewProps {
1418
/**
1519
* Index of initial page that should be selected. Use `setPage` method to
@@ -50,7 +54,7 @@ export interface ViewPagerProps extends ReactNative.ViewProps {
5054
* - settling, meaning that there was an interaction with the page scroller, and the
5155
* page scroller is now finishing it's closing or opening animation
5256
*/
53-
onPageScrollStateChanged?: (state: "Idle" | "Dragging" | "Settling") => void;
57+
onPageScrollStateChanged?: (event: ReactNative.NativeSyntheticEvent<PageScrollStateChangedEvent>) => void;
5458

5559
/**
5660
* Determines whether the keyboard gets dismissed in response to a drag.
@@ -64,6 +68,8 @@ export interface ViewPagerProps extends ReactNative.ViewProps {
6468
* edge-to-edge.
6569
*/
6670
pageMargin?: number;
71+
72+
onMoveShouldSetResponderCapture?: (event: any) => boolean;
6773

6874
/**
6975
* iOS only

0 commit comments

Comments
 (0)