Skip to content

Commit

Permalink
SmartUrlInput [nfc]: Make controlled by caller
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe committed Mar 29, 2022
1 parent acd4ea0 commit 92c79ff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/common/SmartUrlInput.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @flow strict-local */
import React, { useState, useRef, useCallback, useContext } from 'react';
import React, { useRef, useCallback, useContext } from 'react';
import type { Node } from 'react';
import { TextInput, View } from 'react-native';
import { useFocusEffect } from '@react-navigation/native';
Expand Down Expand Up @@ -28,21 +28,20 @@ type Props = $ReadOnly<{|

style?: ViewStyleProp,
onChangeText: (value: string) => void,
value: string,
onSubmitEditing: () => Promise<void>,
enablesReturnKeyAutomatically: boolean,
|}>;

export default function SmartUrlInput(props: Props): Node {
const { style, onChangeText, onSubmitEditing, enablesReturnKeyAutomatically } = props;
const { style, onChangeText, value, onSubmitEditing, enablesReturnKeyAutomatically } = props;

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

const [value, setValue] = useState<string>('');

const themeContext = useContext(ThemeContext);

// When the route is focused in the navigation, focus the input.
Expand All @@ -65,14 +64,6 @@ export default function SmartUrlInput(props: Props): Node {
}, []),
);

const handleChange = useCallback(
(_value: string) => {
setValue(_value);
onChangeText(_value);
},
[defaultDomain, defaultProtocol, onChangeText],
);

return (
<View style={[styles.wrapper, style]}>
<TextInput
Expand All @@ -84,7 +75,7 @@ export default function SmartUrlInput(props: Props): Node {
autoCorrect={false}
autoCapitalize="none"
returnKeyType="go"
onChangeText={handleChange}
onChangeText={onChangeText}
blurOnSubmit={false}
keyboardType="url"
underlineColorAndroid="transparent"
Expand Down
5 changes: 5 additions & 0 deletions src/start/RealmInputScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ export default function RealmInputScreen(props: Props): Node {
const { navigation } = props;

const [progress, setProgress] = useState<boolean>(false);

// Prepopulate with "https://"; not everyone has memorized that sequence
// of characters.
const [realmInputValue, setRealmInputValue] = useState<string>('');

const [error, setError] = useState<string | null>(null);

const tryRealm = useCallback(async () => {
Expand Down Expand Up @@ -83,6 +87,7 @@ export default function RealmInputScreen(props: Props): Node {
style={styles.input}
navigation={navigation}
onChangeText={setRealmInputValue}
value={realmInputValue}
onSubmitEditing={tryRealm}
enablesReturnKeyAutomatically
/>
Expand Down

0 comments on commit 92c79ff

Please sign in to comment.