Skip to content

Commit 0684547

Browse files
committed
SmartUrlInput: Remove workaround for an RN bug that's less bothersome now
Less bothersome because, after the previous commit, the only remaining .focus() call is for convenience (autofocus when back-navigating to the screen), not necessity. With this RN bug, whether we work around it or not, it remains possible to focus a TextInput by tapping on it. So why "necessity"? Before the previous commit, we had a hack where text elements were trying to blend in as part of a TextInput, with the real TextInput being as little as 1px wide when empty. So, pressing the text elements was an important way to give the input focus, and that was achieved with .focus().
1 parent aeeb835 commit 0684547

File tree

1 file changed

+1
-56
lines changed

1 file changed

+1
-56
lines changed

src/common/SmartUrlInput.js

+1-56
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow strict-local */
22
import React, { useState, useRef, useCallback, useContext } from 'react';
33
import type { Node } from 'react';
4-
import { Platform, TextInput, View, Keyboard } from 'react-native';
4+
import { TextInput, View } from 'react-native';
55
import { useFocusEffect } from '@react-navigation/native';
66
import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';
77

@@ -32,59 +32,6 @@ type Props = $ReadOnly<{|
3232
enablesReturnKeyAutomatically: boolean,
3333
|}>;
3434

35-
/**
36-
* Work around https://github.com/facebook/react-native/issues/19366.
37-
*
38-
* The bug: If the keyboard is dismissed only by pressing the built-in
39-
* Android back button, then the next time you call `.focus()` on the
40-
* input, the keyboard won't open again. On the other hand, if you call
41-
* `.blur()`, then the keyboard *will* open the next time you call
42-
* `.focus()`.
43-
*
44-
* This workaround: Call `.blur()` on the input whenever the keyboard is
45-
* closed, because it might have been closed by the built-in Android back
46-
* button. Then when we call `.focus()` the next time, it will open the
47-
* keyboard, as expected. (We only maintain that keyboard-closed listener
48-
* when this SmartUrlInput is on the screen that's focused in the
49-
* navigation.)
50-
*
51-
* Other workarounds that didn't work:
52-
* - When it comes time to do a `.focus()`, do a sneaky `.blur()` first,
53-
* then do the `.focus()` 100ms later. It's janky. This was #2078,
54-
* probably inspired by
55-
* https://github.com/facebook/react-native/issues/19366#issuecomment-400603928.
56-
* - Use RN's `BackHandler` to actually listen for the built-in Android back
57-
* button being used. That didn't work; the event handler wasn't firing
58-
* for either `backPress` or `hardwareBackPress` events. (We never
59-
* committed a version of this workaround.)
60-
*/
61-
function useRn19366Workaround(textInputRef) {
62-
if (Platform.OS !== 'android') {
63-
return;
64-
}
65-
66-
// (Disabling `react-hooks/rules-of-hooks` here is fine; the relevant rule
67-
// is not to call Hooks conditionally. But the platform conditional won't
68-
// vary in its behavior between multiple renders.)
69-
70-
// eslint-disable-next-line react-hooks/rules-of-hooks
71-
useFocusEffect(
72-
// eslint-disable-next-line react-hooks/rules-of-hooks
73-
React.useCallback(() => {
74-
const handleKeyboardDidHide = () => {
75-
if (textInputRef.current) {
76-
// `.current` is not type-checked; see definition.
77-
textInputRef.current.blur();
78-
}
79-
};
80-
81-
Keyboard.addListener('keyboardDidHide', handleKeyboardDidHide);
82-
83-
return () => Keyboard.removeListener('keyboardDidHide', handleKeyboardDidHide);
84-
}, [textInputRef]),
85-
);
86-
}
87-
8835
export default function SmartUrlInput(props: Props): Node {
8936
const { style, onChangeText, onSubmitEditing, enablesReturnKeyAutomatically } = props;
9037

@@ -126,8 +73,6 @@ export default function SmartUrlInput(props: Props): Node {
12673
[onChangeText],
12774
);
12875

129-
useRn19366Workaround(textInputRef);
130-
13176
return (
13277
<View style={[styles.wrapper, style]}>
13378
<TextInput

0 commit comments

Comments
 (0)