Skip to content

Commit ba3c728

Browse files
Andrew Haywardnecolas
authored andcommitted
[fix] Image does not set persistent click handler
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 #2061
1 parent b2eb3ca commit ba3c728

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

packages/react-native-web/src/exports/Text/__tests__/index-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,34 @@ describe('components/Text', () => {
155155
});
156156
});
157157

158+
describe('prop "onClick"', () => {
159+
test('is called', () => {
160+
const onClick = jest.fn();
161+
const ref = React.createRef();
162+
act(() => {
163+
render(<Text onClick={onClick} ref={ref} />);
164+
});
165+
const target = createEventTarget(ref.current);
166+
act(() => {
167+
target.click();
168+
});
169+
expect(onClick).toBeCalled();
170+
});
171+
172+
test('is still called if "onPress" is provided', () => {
173+
const onClick = jest.fn();
174+
const ref = React.createRef();
175+
act(() => {
176+
render(<Text onClick={onClick} onPress={() => {}} ref={ref} />);
177+
});
178+
const target = createEventTarget(ref.current);
179+
act(() => {
180+
target.click();
181+
});
182+
expect(onClick).toBeCalled();
183+
});
184+
});
185+
158186
describe('prop "onFocus"', () => {
159187
test('is called', () => {
160188
const onFocus = jest.fn();
@@ -184,6 +212,19 @@ describe('components/Text', () => {
184212
});
185213
expect(onPress).toBeCalled();
186214
});
215+
216+
test('is not called if "onClick" is provided', () => {
217+
const onPress = jest.fn();
218+
const ref = React.createRef();
219+
act(() => {
220+
render(<Text onClick={() => {}} onPress={onPress} ref={ref} />);
221+
});
222+
const target = createEventTarget(ref.current);
223+
act(() => {
224+
target.click();
225+
});
226+
expect(onPress).not.toBeCalled();
227+
});
187228
});
188229

189230
describe('prop "ref"', () => {

packages/react-native-web/src/exports/Text/index.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,17 @@ const Text: React.AbstractComponent<TextProps, HTMLElement & PlatformMethods> =
104104
onStartShouldSetResponderCapture
105105
});
106106

107-
function handleClick(e) {
108-
if (onClick != null) {
109-
onClick(e);
110-
}
111-
if (onClick == null && onPress != null) {
112-
e.stopPropagation();
113-
onPress(e);
114-
}
115-
}
107+
const handleClick = React.useCallback(
108+
(e) => {
109+
if (onClick != null) {
110+
onClick(e);
111+
} else if (onPress != null) {
112+
e.stopPropagation();
113+
onPress(e);
114+
}
115+
},
116+
[onClick, onPress]
117+
);
116118

117119
let component = hasTextAncestor ? 'span' : 'div';
118120
const supportedProps = pickProps(props);
@@ -122,7 +124,11 @@ const Text: React.AbstractComponent<TextProps, HTMLElement & PlatformMethods> =
122124
if (!hasTextAncestor) {
123125
supportedProps.dir = dir != null ? dir : 'auto';
124126
}
125-
supportedProps.onClick = handleClick;
127+
128+
if (onClick || onPress) {
129+
supportedProps.onClick = handleClick;
130+
}
131+
126132
supportedProps.style = style;
127133
if (props.href != null) {
128134
component = 'a';

0 commit comments

Comments
 (0)