File size: 1,977 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import React, { useEffect, useRef, useState } from 'react';
import { StyleSheet, View, TextInput } from 'react-native';
import { useSearchBox, UseSearchBoxProps } from 'react-instantsearch-hooks';
type SearchBoxProps = UseSearchBoxProps & {
onChange: (newValue: string) => void;
};
export function SearchBox({ onChange, ...props }: SearchBoxProps) {
const { query, refine } = useSearchBox(props);
const [value, setValue] = useState(query);
const inputRef = useRef<TextInput>(null);
// Track when the value coming from the React state changes to synchronize
// it with InstantSearch.
useEffect(() => {
if (query !== value) {
refine(value);
}
// We don't want to track when the InstantSearch query changes.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value, refine]);
// Track when the InstantSearch query changes to synchronize it with
// the React state.
useEffect(() => {
// We bypass the state update if the input is focused to avoid concurrent
// updates when typing.
if (!inputRef.current?.isFocused() && query !== value) {
setValue(query);
}
// We don't want to track when the React state value changes.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [query]);
return (
<View style={styles.container}>
<TextInput
ref={inputRef}
style={styles.input}
value={value}
onChangeText={(newValue) => {
setValue(newValue);
onChange(newValue);
}}
clearButtonMode="while-editing"
autoCapitalize="none"
autoCorrect={false}
spellCheck={false}
autoCompleteType="off"
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
backgroundColor: '#252b33',
padding: 18,
},
input: {
height: 48,
padding: 12,
fontSize: 16,
backgroundColor: '#fff',
borderRadius: 4,
borderWidth: 1,
borderColor: '#ddd',
},
});
|