[fix] Don't propagate click or contextmenu events on disabled elements

This patch fixes the PressResponder to avoid propagating click and contextmenu
events in all circumstances. It also prevents click propagating for focusable
elements that are disabled, mirroring the behavior of native buttons when
disabled.

Fix #1757
This commit is contained in:
Nicolas Gallagher
2020-10-05 15:33:50 -07:00
parent ba5e9e3079
commit 6d04e7243e
2 changed files with 6 additions and 2 deletions

View File

@@ -388,8 +388,8 @@ export default class PressResponder {
} else if (onPress != null && event.ctrlKey === false && event.altKey === false) {
onPress(event);
}
event.stopPropagation();
}
event.stopPropagation();
},
// If `onLongPress` is provided and a touch pointer is being used, prevent the
@@ -399,6 +399,7 @@ export default class PressResponder {
if (!disabled && onLongPress != null && this._isPointerTouch && !event.defaultPrevented) {
event.preventDefault();
}
event.stopPropagation();
}
};
}

View File

@@ -229,7 +229,10 @@ const createDOMProps = (component, props) => {
const onClick = domProps.onClick;
if (onClick != null) {
if (disabled) {
domProps.onClick = undefined;
// Prevent click propagating if the element is disabled. See #1757
domProps.onClick = function(e) {
e.stopPropagation();
};
} else if (!isNativeInteractiveElement) {
// For native elements that are focusable but don't dispatch 'click' events
// for keyboards.