diff --git a/src/components/forms/SearchInput.tsx b/src/components/forms/SearchInput.tsx index 97540d0bca..b0083104ca 100644 --- a/src/components/forms/SearchInput.tsx +++ b/src/components/forms/SearchInput.tsx @@ -1,9 +1,10 @@ -import React from 'react' +import React, {useRef, useState} from 'react' import {TextInput, View} from 'react-native' import {msg} from '@lingui/macro' import {useLingui} from '@lingui/react' import {HITSLOP_10} from '#/lib/constants' +import {mergeRefs} from '#/lib/merge-refs' import {isNative} from '#/platform/detection' import {atoms as a, useTheme} from '#/alf' import {Button, ButtonIcon} from '#/components/Button' @@ -13,26 +14,35 @@ import {TimesLarge_Stroke2_Corner0_Rounded as X} from '#/components/icons/Times' type SearchInputProps = Omit & { label?: TextField.InputProps['label'] - /** - * Called when the user presses the (X) button - */ onClearText?: () => void } export const SearchInput = React.forwardRef( - function SearchInput({value, label, onClearText, ...rest}, ref) { + function SearchInput({label, onChangeText: onChangeTextProp, ...rest}, ref) { const t = useTheme() const {_} = useLingui() - const showClear = value && value.length > 0 + const [showClear, setShowClear] = useState(false) + const inputRef = useRef() + + const handleChangeText = (text: string) => { + setShowClear(text.length > 0) + onChangeTextProp?.(text) + } + + const handleClear = () => { + inputRef.current?.clear() + setShowClear(false) + onChangeTextProp?.('') + } return ( ( autoCorrect={false} autoComplete="off" autoCapitalize="none" - style={[ - showClear - ? { - paddingRight: 24, - } - : {}, - ]} + style={[showClear && {paddingRight: 24}]} {...rest} /> @@ -66,7 +70,7 @@ export const SearchInput = React.forwardRef( ]}>