Skip to content

Commit 7bf0c54

Browse files
committed
SmartUrlInput [nfc]: Make controlled by caller
1 parent 8bf7fb7 commit 7bf0c54

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

src/common/SmartUrlInput.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* @flow strict-local */
2-
import React, { useState, useRef, useCallback, useContext } from 'react';
2+
import React, { useRef, useCallback, useContext } from 'react';
33
import type { Node } from 'react';
44
import { Platform, TextInput, View, Keyboard } from 'react-native';
55
import { useFocusEffect } from '@react-navigation/native';
@@ -28,6 +28,7 @@ type Props = $ReadOnly<{|
2828

2929
style?: ViewStyleProp,
3030
onChangeText: (value: string) => void,
31+
value: string,
3132
onSubmitEditing: () => Promise<void>,
3233
enablesReturnKeyAutomatically: boolean,
3334
|}>;
@@ -76,23 +77,14 @@ function useRn19366Workaround(textInputRef) {
7677
}
7778

7879
export default function SmartUrlInput(props: Props): Node {
79-
const { style, onChangeText, onSubmitEditing, enablesReturnKeyAutomatically } = props;
80+
const { style, onChangeText, value, onSubmitEditing, enablesReturnKeyAutomatically } = props;
8081

8182
// We should replace the fixme with
8283
// `React$ElementRef<typeof TextInput>` when we can. Currently, that
8384
// would make `.current` be `any(implicit)`, which we don't want;
8485
// this is probably down to bugs in Flow's special support for React.
8586
const textInputRef = useRef<$FlowFixMe>();
8687

87-
/**
88-
* The actual input string, exactly as entered by the user,
89-
* without modifications by autocomplete.
90-
*
91-
* Prepopulates with "https://", because not everyone has memorized that
92-
* sequence of characters.
93-
*/
94-
const [value, setValue] = useState<string>('https://');
95-
9688
const themeContext = useContext(ThemeContext);
9789

9890
// When the route is focused in the navigation, focus the input.
@@ -107,14 +99,6 @@ export default function SmartUrlInput(props: Props): Node {
10799
}, []),
108100
);
109101

110-
const handleChange = useCallback(
111-
(_value: string) => {
112-
setValue(_value);
113-
onChangeText(_value);
114-
},
115-
[onChangeText],
116-
);
117-
118102
if (Platform.OS === 'android') {
119103
// (This eslint-disable is fine; the relevant rule is not to call Hooks
120104
// conditionally. But this conditional won't vary in its behavior
@@ -132,7 +116,7 @@ export default function SmartUrlInput(props: Props): Node {
132116
autoCorrect={false}
133117
autoCapitalize="none"
134118
returnKeyType="go"
135-
onChangeText={handleChange}
119+
onChangeText={onChangeText}
136120
blurOnSubmit={false}
137121
keyboardType="url"
138122
underlineColorAndroid="transparent"

src/start/RealmInputScreen.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export default function RealmInputScreen(props: Props): Node {
2525
const { navigation } = props;
2626

2727
const [progress, setProgress] = useState<boolean>(false);
28-
const [realmInputValue, setRealmInputValue] = useState<string>('');
28+
29+
// Prepopulate with "https://"; not everyone has memorized that sequence
30+
// of characters.
31+
const [realmInputValue, setRealmInputValue] = useState<string>('https://');
32+
2933
const [error, setError] = useState<string | null>(null);
3034

3135
const tryRealm = useCallback(async () => {
@@ -75,6 +79,7 @@ export default function RealmInputScreen(props: Props): Node {
7579
style={styles.input}
7680
navigation={navigation}
7781
onChangeText={setRealmInputValue}
82+
value={realmInputValue}
7883
onSubmitEditing={tryRealm}
7984
enablesReturnKeyAutomatically
8085
/>

0 commit comments

Comments
 (0)