diff options
author | Hailey <me@haileyok.com> | 2024-02-12 13:47:48 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-12 13:47:48 -0800 |
commit | b936da1c0fa7b8a49e84a1b36ae064db9f8c0e47 (patch) | |
tree | de9fba4a47fb505a5dfb754bc1dd469701cfcece /src/view/com/util/forms | |
parent | ba7463cadf15bd5420e1a8cc46952bde2c81cad9 (diff) | |
download | voidsky-b936da1c0fa7b8a49e84a1b36ae064db9f8c0e47.tar.zst |
Add search button to header on feeds screen (#2848)
* add search bar to header * add button on web
Diffstat (limited to 'src/view/com/util/forms')
-rw-r--r-- | src/view/com/util/forms/SearchInput.tsx | 125 |
1 files changed, 70 insertions, 55 deletions
diff --git a/src/view/com/util/forms/SearchInput.tsx b/src/view/com/util/forms/SearchInput.tsx index a78d23c9b..5a21d8fdd 100644 --- a/src/view/com/util/forms/SearchInput.tsx +++ b/src/view/com/util/forms/SearchInput.tsx @@ -26,64 +26,79 @@ interface Props { onSubmitQuery: () => void style?: StyleProp<ViewStyle> } -export function SearchInput({ - query, - setIsInputFocused, - onChangeQuery, - onPressCancelSearch, - onSubmitQuery, - style, -}: Props) { - const theme = useTheme() - const pal = usePalette('default') - const {_} = useLingui() - const textInput = React.useRef<TextInput>(null) - const onPressCancelSearchInner = React.useCallback(() => { - onPressCancelSearch() - textInput.current?.blur() - }, [onPressCancelSearch, textInput]) +export interface SearchInputRef { + focus?: () => void +} + +export const SearchInput = React.forwardRef<SearchInputRef, Props>( + function SearchInput( + { + query, + setIsInputFocused, + onChangeQuery, + onPressCancelSearch, + onSubmitQuery, + style, + }, + ref, + ) { + const theme = useTheme() + const pal = usePalette('default') + const {_} = useLingui() + const textInput = React.useRef<TextInput>(null) + + const onPressCancelSearchInner = React.useCallback(() => { + onPressCancelSearch() + textInput.current?.blur() + }, [onPressCancelSearch, textInput]) - return ( - <View style={[pal.viewLight, styles.container, style]}> - <MagnifyingGlassIcon style={[pal.icon, styles.icon]} size={21} /> - <TextInput - testID="searchTextInput" - ref={textInput} - placeholder={_(msg`Search`)} - placeholderTextColor={pal.colors.textLight} - selectTextOnFocus - returnKeyType="search" - value={query} - style={[pal.text, styles.input]} - keyboardAppearance={theme.colorScheme} - onFocus={() => setIsInputFocused?.(true)} - onBlur={() => setIsInputFocused?.(false)} - onChangeText={onChangeQuery} - onSubmitEditing={onSubmitQuery} - accessibilityRole="search" - accessibilityLabel={_(msg`Search`)} - accessibilityHint="" - autoCorrect={false} - autoCapitalize="none" - /> - {query ? ( - <TouchableOpacity - onPress={onPressCancelSearchInner} - accessibilityRole="button" - accessibilityLabel={_(msg`Clear search query`)} + React.useImperativeHandle(ref, () => ({ + focus: () => textInput.current?.focus(), + blur: () => textInput.current?.blur(), + })) + + return ( + <View style={[pal.viewLight, styles.container, style]}> + <MagnifyingGlassIcon style={[pal.icon, styles.icon]} size={21} /> + <TextInput + testID="searchTextInput" + ref={textInput} + placeholder={_(msg`Search`)} + placeholderTextColor={pal.colors.textLight} + selectTextOnFocus + returnKeyType="search" + value={query} + style={[pal.text, styles.input]} + keyboardAppearance={theme.colorScheme} + onFocus={() => setIsInputFocused?.(true)} + onBlur={() => setIsInputFocused?.(false)} + onChangeText={onChangeQuery} + onSubmitEditing={onSubmitQuery} + accessibilityRole="search" + accessibilityLabel={_(msg`Search`)} accessibilityHint="" - hitSlop={HITSLOP_10}> - <FontAwesomeIcon - icon="xmark" - size={16} - style={pal.textLight as FontAwesomeIconStyle} - /> - </TouchableOpacity> - ) : undefined} - </View> - ) -} + autoCorrect={false} + autoCapitalize="none" + /> + {query ? ( + <TouchableOpacity + onPress={onPressCancelSearchInner} + accessibilityRole="button" + accessibilityLabel={_(msg`Clear search query`)} + accessibilityHint="" + hitSlop={HITSLOP_10}> + <FontAwesomeIcon + icon="xmark" + size={16} + style={pal.textLight as FontAwesomeIconStyle} + /> + </TouchableOpacity> + ) : undefined} + </View> + ) + }, +) const styles = StyleSheet.create({ container: { |