Skip to content

Fix handling of onClick event for accessibilityRole="link" #1353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,45 @@ describe('modules/createElement', () => {
});
});

test('gracefully handles onClick for links', () => {
let wasCalled = false;
const component = shallow(
createElement('div', {
accessibilityRole: 'link',
onClick: () => {
wasCalled = true;
}
})
);
component.find('a').simulate('click', {
isDefaultPrevented() {},
nativeEvent: {},
preventDefault() {}
});

expect(wasCalled).toBe(true);
});

test('gracefully handles onClick for touchable links', () => {
let wasCalled = false;
const component = shallow(
createElement('div', {
accessibilityRole: 'link',
onResponderRelease: () => {}, // fake touchable
onClick: () => {
wasCalled = true;
}
})
);
component.find('a').simulate('click', {
isDefaultPrevented() {},
nativeEvent: {},
preventDefault() {}
});

expect(wasCalled).toBe(true);
});

describe('prop "accessibilityRole"', () => {
test('and string component type', () => {
const component = shallow(createElement('span', { accessibilityRole: 'link' }));
Expand Down
14 changes: 9 additions & 5 deletions packages/react-native-web/src/exports/createElement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ const adjustProps = domProps => {
// element. Click events are not an expected part of the React Native API,
// and browsers dispatch click events that cannot otherwise be cancelled from
// preceding mouse events in the responder system.
// Handle overrides gracefully if the user provides an onClick method.
// If this is provided, the user must make sure to handle the edge cases.
if (isLinkRole && onResponderRelease) {
domProps.onClick = function(e) {
if (!e.isDefaultPrevented() && !isModifiedEvent(e.nativeEvent) && !domProps.target) {
e.preventDefault();
}
};
domProps.onClick =
onClick ||
function(e) {
if (!e.isDefaultPrevented() && !isModifiedEvent(e.nativeEvent) && !domProps.target) {
e.preventDefault();
}
};
}

// Button-like roles should trigger 'onClick' if SPACE or ENTER keys are pressed.
Expand Down