diff --git a/src/components/Searchbar.tsx b/src/components/Searchbar.tsx index bb73e8f263..11c79d2e9a 100644 --- a/src/components/Searchbar.tsx +++ b/src/components/Searchbar.tsx @@ -149,7 +149,7 @@ export type Props = React.ComponentPropsWithRef & { type TextInputHandles = Pick< TextInput, - 'setNativeProps' | 'isFocused' | 'clear' | 'blur' | 'focus' + 'setNativeProps' | 'isFocused' | 'clear' | 'blur' | 'focus' | 'setSelection' >; /** @@ -210,31 +210,16 @@ const Searchbar = forwardRef( const theme = useInternalTheme(themeOverrides); const root = React.useRef(null); - React.useImperativeHandle(ref, () => { - const input = root.current; - - if (input) { - return { - focus: () => input.focus(), - clear: () => input.clear(), - setNativeProps: (args: TextInputProps) => input.setNativeProps(args), - isFocused: () => input.isFocused(), - blur: () => input.blur(), - }; - } - - const noop = () => { - throw new Error('TextInput is not available'); - }; - - return { - focus: noop, - clear: noop, - setNativeProps: noop, - isFocused: noop, - blur: noop, - }; - }); + React.useImperativeHandle(ref, () => ({ + focus: () => root.current?.focus(), + clear: () => root.current?.clear(), + setNativeProps: (args: TextInputProps) => + root.current?.setNativeProps(args), + isFocused: () => root.current?.isFocused() || false, + blur: () => root.current?.blur(), + setSelection: (start: number, end: number) => + root.current?.setSelection(start, end), + })); const handleClearPress = (e: any) => { root.current?.clear();