Skip to content

Commit 697479e

Browse files
committed
[fix] TextInput calls onFocus if autoFocus={true}
The host node ref is not a dependency of the focus handling. Previously, the focus callback was not called if the element was auto-focused, because React DOM calls `onFocus` while the object ref is `null`. Fix #2045
1 parent 3fc7634 commit 697479e

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

packages/examples/pages/text-input/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export default function TextInputPage() {
1414
return (
1515
<Example title="TextInput">
1616
<View style={styles.container}>
17+
<TextInput
18+
autoFocus
19+
onFocus={() => {
20+
console.log('focused');
21+
}}
22+
style={styles.textinput}
23+
/>
1724
<TextInput
1825
blurOnSubmit={true}
1926
onSubmitEditing={() => focusNextField()}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('components/TextInput', () => {
9595
});
9696

9797
test('value "true"', () => {
98-
const { container } = render(<input autoFocus />);
98+
const { container } = render(<TextInput autoFocus />);
9999
const input = findInput(container);
100100
expect(document.activeElement).toBe(input);
101101
});

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ const TextInput: React.AbstractComponent<
229229

230230
function handleFocus(e) {
231231
const node = hostRef.current;
232+
if (onFocus) {
233+
e.nativeEvent.text = e.target.value;
234+
onFocus(e);
235+
}
232236
if (node != null) {
233237
TextInputState._currentlyFocusedNode = node;
234-
if (onFocus) {
235-
e.nativeEvent.text = e.target.value;
236-
onFocus(e);
237-
}
238238
if (clearTextOnFocus) {
239239
node.value = '';
240240
}

0 commit comments

Comments
 (0)