mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-03 02:42:05 +00:00
[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
This commit is contained in:
@@ -14,6 +14,13 @@ export default function TextInputPage() {
|
||||
return (
|
||||
<Example title="TextInput">
|
||||
<View style={styles.container}>
|
||||
<TextInput
|
||||
autoFocus
|
||||
onFocus={() => {
|
||||
console.log('focused');
|
||||
}}
|
||||
style={styles.textinput}
|
||||
/>
|
||||
<TextInput
|
||||
blurOnSubmit={true}
|
||||
onSubmitEditing={() => focusNextField()}
|
||||
|
||||
@@ -95,7 +95,7 @@ describe('components/TextInput', () => {
|
||||
});
|
||||
|
||||
test('value "true"', () => {
|
||||
const { container } = render(<input autoFocus />);
|
||||
const { container } = render(<TextInput autoFocus />);
|
||||
const input = findInput(container);
|
||||
expect(document.activeElement).toBe(input);
|
||||
});
|
||||
|
||||
@@ -229,12 +229,12 @@ const TextInput: React.AbstractComponent<
|
||||
|
||||
function handleFocus(e) {
|
||||
const node = hostRef.current;
|
||||
if (onFocus) {
|
||||
e.nativeEvent.text = e.target.value;
|
||||
onFocus(e);
|
||||
}
|
||||
if (node != null) {
|
||||
TextInputState._currentlyFocusedNode = node;
|
||||
if (onFocus) {
|
||||
e.nativeEvent.text = e.target.value;
|
||||
onFocus(e);
|
||||
}
|
||||
if (clearTextOnFocus) {
|
||||
node.value = '';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user