Skip to content

Commit

Permalink
[fix] Image does not set persistent click handler
Browse files Browse the repository at this point in the history
Only set the click handler if it is needed. This was previously always set,
which caused screen readers to announce the element as "clickable".

Close necolas#2061
  • Loading branch information
Andrew Hayward authored and necolas committed Jun 23, 2021
1 parent b2eb3ca commit ba3c728
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
41 changes: 41 additions & 0 deletions packages/react-native-web/src/exports/Text/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,34 @@ describe('components/Text', () => {
});
});

describe('prop "onClick"', () => {
test('is called', () => {
const onClick = jest.fn();
const ref = React.createRef();
act(() => {
render(<Text onClick={onClick} ref={ref} />);
});
const target = createEventTarget(ref.current);
act(() => {
target.click();
});
expect(onClick).toBeCalled();
});

test('is still called if "onPress" is provided', () => {
const onClick = jest.fn();
const ref = React.createRef();
act(() => {
render(<Text onClick={onClick} onPress={() => {}} ref={ref} />);
});
const target = createEventTarget(ref.current);
act(() => {
target.click();
});
expect(onClick).toBeCalled();
});
});

describe('prop "onFocus"', () => {
test('is called', () => {
const onFocus = jest.fn();
Expand Down Expand Up @@ -184,6 +212,19 @@ describe('components/Text', () => {
});
expect(onPress).toBeCalled();
});

test('is not called if "onClick" is provided', () => {
const onPress = jest.fn();
const ref = React.createRef();
act(() => {
render(<Text onClick={() => {}} onPress={onPress} ref={ref} />);
});
const target = createEventTarget(ref.current);
act(() => {
target.click();
});
expect(onPress).not.toBeCalled();
});
});

describe('prop "ref"', () => {
Expand Down
26 changes: 16 additions & 10 deletions packages/react-native-web/src/exports/Text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,17 @@ const Text: React.AbstractComponent<TextProps, HTMLElement & PlatformMethods> =
onStartShouldSetResponderCapture
});

function handleClick(e) {
if (onClick != null) {
onClick(e);
}
if (onClick == null && onPress != null) {
e.stopPropagation();
onPress(e);
}
}
const handleClick = React.useCallback(
(e) => {
if (onClick != null) {
onClick(e);
} else if (onPress != null) {
e.stopPropagation();
onPress(e);
}
},
[onClick, onPress]
);

let component = hasTextAncestor ? 'span' : 'div';
const supportedProps = pickProps(props);
Expand All @@ -122,7 +124,11 @@ const Text: React.AbstractComponent<TextProps, HTMLElement & PlatformMethods> =
if (!hasTextAncestor) {
supportedProps.dir = dir != null ? dir : 'auto';
}
supportedProps.onClick = handleClick;

if (onClick || onPress) {
supportedProps.onClick = handleClick;
}

supportedProps.style = style;
if (props.href != null) {
component = 'a';
Expand Down

0 comments on commit ba3c728

Please sign in to comment.