@samitouri / QOS-React / commits / 08055a625e

Fix Press module in experimental event API (#15262)

Note: this is for an experimental event API that we're testing out internally at Facebook. Fixes a regression in f4625f51823dd7faa02bba95ae731249ce07246c

Nicolas Gallagher committed Mar 29, 2019 at 14:26 UTC 08055a625e28672f6ab705fd1d063522e337c2c7
1 file changed +40 -44
packages/react-events/src/Press.js
+40 -44
@@ -38,8 +38,8 @@ type PressState = {
38
39 function dispatchPressEvent(
40 context: EventResponderContext,
41 - name: string,
41 state: PressState,
42 + name: string,
43 listener: (e: Object) => void,
44 ): void {
45 context.dispatchEvent(name, listener, state.pressTarget, true);
@@ -50,44 +50,50 @@ function dispatchPressInEvents(
50 props: Object,
51 state: PressState,
52 ): void {
53 + function dispatchPressChangeEvent(bool) {
54 + const pressChangeEventListener = () => {
55 + props.onPressChange(bool);
56 + };
57 + dispatchPressEvent(context, state, 'presschange', pressChangeEventListener);
58 + }
59 +
60 if (props.onPressIn) {
54 - context.dispatchEvent('pressin', props.onPressIn, state.pressTarget, true);
61 + dispatchPressEvent(context, state, 'pressin', props.onPressIn);
62 }
63 if (props.onPressChange) {
57 - const pressChangeEventListener = () => {
58 - props.onPressChange(true);
59 - };
60 - context.dispatchEvent(
61 - 'presschange',
62 - pressChangeEventListener,
63 - state.pressTarget,
64 - true,
65 - );
64 + dispatchPressChangeEvent(true);
65 }
67 - if (!state.isLongPressed && (props.onLongPress || props.onLongPressChange)) {
66 + if ((props.onLongPress || props.onLongPressChange) && !state.isLongPressed) {
67 const longPressDelay = props.longPressDelay || 1000;
68 +
69 state.longPressTimeout = setTimeout(() => {
70 state.isLongPressed = true;
71 state.longPressTimeout = null;
72 - if (props.onLongPressChange) {
73 - const longPressChangeEventListener = () => {
74 - props.onLongPressChange(true);
75 - };
76 - context.dispatchEvent(
77 - 'longpresschange',
78 - longPressChangeEventListener,
79 - state.pressTarget,
80 - true,
81 - );
72 +
73 + if (props.onPressChange && props.longPressCancelsPress) {
74 + dispatchPressChangeEvent(false);
75 }
83 - if (props.onLongPress && !props.longPressCancelsPress) {
76 +
77 + if (props.onLongPress) {
78 const longPressEventListener = e => {
79 props.onLongPress(e);
80 if (e.nativeEvent.defaultPrevented) {
81 state.defaultPrevented = true;
82 }
83 };
90 - dispatchPressEvent(context, 'longpress', state, longPressEventListener);
84 + dispatchPressEvent(context, state, 'longpress', longPressEventListener);
85 + }
86 +
87 + if (props.onLongPressChange) {
88 + const longPressChangeEventListener = () => {
89 + props.onLongPressChange(true);
90 + };
91 + dispatchPressEvent(
92 + context,
93 + state,
94 + 'longpresschange',
95 + longPressChangeEventListener,
96 + );
97 }
98 }, longPressDelay);
99 }
@@ -103,33 +109,23 @@ function dispatchPressOutEvents(
109 state.longPressTimeout = null;
110 }
111 if (props.onPressOut) {
106 - context.dispatchEvent(
107 - 'pressout',
108 - props.onPressOut,
109 - state.pressTarget,
110 - true,
111 - );
112 + dispatchPressEvent(context, state, 'pressout', props.onPressOut);
113 }
113 - if (props.onPressChange && !props.longPressCancelsPress) {
114 + if (props.onPressChange) {
115 const pressChangeEventListener = () => {
116 props.onPressChange(false);
117 };
117 - context.dispatchEvent(
118 - 'presschange',
119 - pressChangeEventListener,
120 - state.pressTarget,
121 - true,
122 - );
118 + dispatchPressEvent(context, state, 'presschange', pressChangeEventListener);
119 }
124 - if (state.isLongPressed && props.onLongPressChange) {
120 + if (props.onLongPressChange && state.isLongPressed) {
121 const longPressChangeEventListener = () => {
122 props.onLongPressChange(false);
123 };
128 - context.dispatchEvent(
124 + dispatchPressEvent(
125 + context,
126 + state,
127 'longpresschange',
128 longPressChangeEventListener,
131 - state.pressTarget,
132 - true,
129 );
130 }
131 }
@@ -188,7 +184,7 @@ const PressResponder = {
184 }
185 };
186 }
191 - dispatchPressEvent(context, 'press', state, keyPressEventListener);
187 + dispatchPressEvent(context, state, 'press', keyPressEventListener);
188 break;
189 }
190 case 'touchstart':
@@ -233,7 +229,7 @@ const PressResponder = {
229 props.onPress &&
230 !(state.isLongPressed && props.longPressCancelsPress)
231 ) {
236 - dispatchPressEvent(context, 'press', state, props.onPress);
232 + dispatchPressEvent(context, state, 'press', props.onPress);
233 }
234 }
235 }
@@ -296,7 +292,7 @@ const PressResponder = {
292 state.defaultPrevented = true;
293 }
294 };
299 - dispatchPressEvent(context, 'press', state, pressEventListener);
295 + dispatchPressEvent(context, state, 'press', pressEventListener);
296 }
297 }
298 }