Skip to content

Commit 08055a6

Browse files
necolastrueadm
authored andcommitted
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 f4625f5
1 parent f4625f5 commit 08055a6

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

packages/react-events/src/Press.js

+40-44
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ type PressState = {
3838

3939
function dispatchPressEvent(
4040
context: EventResponderContext,
41-
name: string,
4241
state: PressState,
42+
name: string,
4343
listener: (e: Object) => void,
4444
): void {
4545
context.dispatchEvent(name, listener, state.pressTarget, true);
@@ -50,44 +50,50 @@ function dispatchPressInEvents(
5050
props: Object,
5151
state: PressState,
5252
): void {
53+
function dispatchPressChangeEvent(bool) {
54+
const pressChangeEventListener = () => {
55+
props.onPressChange(bool);
56+
};
57+
dispatchPressEvent(context, state, 'presschange', pressChangeEventListener);
58+
}
59+
5360
if (props.onPressIn) {
54-
context.dispatchEvent('pressin', props.onPressIn, state.pressTarget, true);
61+
dispatchPressEvent(context, state, 'pressin', props.onPressIn);
5562
}
5663
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);
6665
}
67-
if (!state.isLongPressed && (props.onLongPress || props.onLongPressChange)) {
66+
if ((props.onLongPress || props.onLongPressChange) && !state.isLongPressed) {
6867
const longPressDelay = props.longPressDelay || 1000;
68+
6969
state.longPressTimeout = setTimeout(() => {
7070
state.isLongPressed = true;
7171
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);
8275
}
83-
if (props.onLongPress && !props.longPressCancelsPress) {
76+
77+
if (props.onLongPress) {
8478
const longPressEventListener = e => {
8579
props.onLongPress(e);
8680
if (e.nativeEvent.defaultPrevented) {
8781
state.defaultPrevented = true;
8882
}
8983
};
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+
);
9197
}
9298
}, longPressDelay);
9399
}
@@ -103,33 +109,23 @@ function dispatchPressOutEvents(
103109
state.longPressTimeout = null;
104110
}
105111
if (props.onPressOut) {
106-
context.dispatchEvent(
107-
'pressout',
108-
props.onPressOut,
109-
state.pressTarget,
110-
true,
111-
);
112+
dispatchPressEvent(context, state, 'pressout', props.onPressOut);
112113
}
113-
if (props.onPressChange && !props.longPressCancelsPress) {
114+
if (props.onPressChange) {
114115
const pressChangeEventListener = () => {
115116
props.onPressChange(false);
116117
};
117-
context.dispatchEvent(
118-
'presschange',
119-
pressChangeEventListener,
120-
state.pressTarget,
121-
true,
122-
);
118+
dispatchPressEvent(context, state, 'presschange', pressChangeEventListener);
123119
}
124-
if (state.isLongPressed && props.onLongPressChange) {
120+
if (props.onLongPressChange && state.isLongPressed) {
125121
const longPressChangeEventListener = () => {
126122
props.onLongPressChange(false);
127123
};
128-
context.dispatchEvent(
124+
dispatchPressEvent(
125+
context,
126+
state,
129127
'longpresschange',
130128
longPressChangeEventListener,
131-
state.pressTarget,
132-
true,
133129
);
134130
}
135131
}
@@ -188,7 +184,7 @@ const PressResponder = {
188184
}
189185
};
190186
}
191-
dispatchPressEvent(context, 'press', state, keyPressEventListener);
187+
dispatchPressEvent(context, state, 'press', keyPressEventListener);
192188
break;
193189
}
194190
case 'touchstart':
@@ -233,7 +229,7 @@ const PressResponder = {
233229
props.onPress &&
234230
!(state.isLongPressed && props.longPressCancelsPress)
235231
) {
236-
dispatchPressEvent(context, 'press', state, props.onPress);
232+
dispatchPressEvent(context, state, 'press', props.onPress);
237233
}
238234
}
239235
}
@@ -296,7 +292,7 @@ const PressResponder = {
296292
state.defaultPrevented = true;
297293
}
298294
};
299-
dispatchPressEvent(context, 'press', state, pressEventListener);
295+
dispatchPressEvent(context, state, 'press', pressEventListener);
300296
}
301297
}
302298
}

0 commit comments

Comments
 (0)