[Flare] Deeply prevent default on anchor elements (#15750)

This commit is contained in:
Dominic Gannaway
2019-05-28 12:46:07 +01:00
committed by GitHub
parent a97b5c07b0
commit 287ef30bba
4 changed files with 23 additions and 2 deletions

View File

@@ -424,11 +424,12 @@ const eventResponderContext: ReactResponderContext = {
isTargetWithinHostComponent(
target: Element | Document,
elementType: string,
deep: boolean,
): boolean {
validateResponderContext();
let fiber = getClosestInstanceFromNode(target);
while (fiber !== null) {
if (fiber.stateNode === currentInstance) {
if (!deep && fiber.stateNode === currentInstance) {
return false;
}
if (fiber.tag === HostComponent && fiber.type === elementType) {

View File

@@ -724,7 +724,7 @@ const PressResponder = {
}
case 'click': {
if (context.isTargetWithinHostComponent(target, 'a')) {
if (context.isTargetWithinHostComponent(target, 'a', true)) {
const {
altKey,
ctrlKey,

View File

@@ -2137,6 +2137,25 @@ describe('Event responder: Press', () => {
expect(preventDefault).toBeCalled();
});
it('deeply prevents native behaviour by default', () => {
const onPress = jest.fn();
const preventDefault = jest.fn();
const buttonRef = React.createRef();
const element = (
<a href="#">
<Press onPress={onPress}>
<button ref={buttonRef} />
</Press>
</a>
);
ReactDOM.render(element, container);
buttonRef.current.dispatchEvent(createEvent('pointerdown'));
buttonRef.current.dispatchEvent(createEvent('pointerup'));
buttonRef.current.dispatchEvent(createEvent('click', {preventDefault}));
expect(preventDefault).toBeCalled();
});
it('prevents native behaviour by default with nested elements', () => {
const onPress = jest.fn();
const preventDefault = jest.fn();

View File

@@ -194,5 +194,6 @@ export type ReactResponderContext = {
isTargetWithinHostComponent: (
target: Element | Document,
elementType: string,
deep: boolean,
) => boolean,
};