Skip to content

Commit

Permalink
Experimental Event API: preventDefault handling for anchors (#15383)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm committed Apr 11, 2019
1 parent c25c59c commit c984100
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions packages/react-events/src/Press.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ type PressProps = {
bottom: number,
left: number,
},
preventDefault: boolean,
};

type PressState = {
defaultPrevented: boolean,
isActivePressed: boolean,
isActivePressStart: boolean,
isAnchorTouched: boolean,
Expand Down Expand Up @@ -206,14 +206,7 @@ function dispatchPressStartEvents(
state.isLongPressed = true;
state.longPressTimeout = null;
if (props.onLongPress) {
const listener = e => {
props.onLongPress(e);
// TODO address this again at some point
// if (e.nativeEvent.defaultPrevented) {
// state.defaultPrevented = true;
// }
};
dispatchEvent(context, state, 'longpress', listener);
dispatchEvent(context, state, 'longpress', props.onLongPress);
}
if (props.onLongPressChange) {
dispatchLongPressChangeEvent(context, props, state);
Expand Down Expand Up @@ -363,7 +356,6 @@ const PressResponder = {
targetEventTypes,
createInitialState(): PressState {
return {
defaultPrevented: false,
isActivePressed: false,
isActivePressStart: false,
isAnchorTouched: false,
Expand Down Expand Up @@ -477,14 +469,7 @@ const PressResponder = {
props.onLongPressShouldCancelPress()
)
) {
const listener = e => {
props.onPress(e);
// TODO address this again at some point
// if (e.nativeEvent.defaultPrevented) {
// state.defaultPrevented = true;
// }
};
dispatchEvent(context, state, 'press', listener);
dispatchEvent(context, state, 'press', props.onPress);
}
}
}
Expand Down Expand Up @@ -607,9 +592,13 @@ const PressResponder = {
}
case 'click': {
if (state.defaultPrevented) {
(nativeEvent: any).preventDefault();
state.defaultPrevented = false;
if (isAnchorTagElement(target)) {
const {ctrlKey, metaKey, shiftKey} = ((nativeEvent: any): MouseEvent);
const preventDefault = props.preventDefault;
// Check "open in new window/tab" key modifiers
if (preventDefault !== false && !shiftKey && !ctrlKey && !metaKey) {
(nativeEvent: any).preventDefault();
}
}
}
}
Expand Down

0 comments on commit c984100

Please sign in to comment.