Skip to content

Commit 218cc32

Browse files
authored
fix: add set selection to the searchbar (#4708)
1 parent beab66e commit 218cc32

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

src/components/Searchbar.tsx

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export type Props = React.ComponentPropsWithRef<typeof TextInput> & {
149149

150150
type TextInputHandles = Pick<
151151
TextInput,
152-
'setNativeProps' | 'isFocused' | 'clear' | 'blur' | 'focus'
152+
'setNativeProps' | 'isFocused' | 'clear' | 'blur' | 'focus' | 'setSelection'
153153
>;
154154

155155
/**
@@ -210,31 +210,16 @@ const Searchbar = forwardRef<TextInputHandles, Props>(
210210
const theme = useInternalTheme(themeOverrides);
211211
const root = React.useRef<TextInput>(null);
212212

213-
React.useImperativeHandle(ref, () => {
214-
const input = root.current;
215-
216-
if (input) {
217-
return {
218-
focus: () => input.focus(),
219-
clear: () => input.clear(),
220-
setNativeProps: (args: TextInputProps) => input.setNativeProps(args),
221-
isFocused: () => input.isFocused(),
222-
blur: () => input.blur(),
223-
};
224-
}
225-
226-
const noop = () => {
227-
throw new Error('TextInput is not available');
228-
};
229-
230-
return {
231-
focus: noop,
232-
clear: noop,
233-
setNativeProps: noop,
234-
isFocused: noop,
235-
blur: noop,
236-
};
237-
});
213+
React.useImperativeHandle(ref, () => ({
214+
focus: () => root.current?.focus(),
215+
clear: () => root.current?.clear(),
216+
setNativeProps: (args: TextInputProps) =>
217+
root.current?.setNativeProps(args),
218+
isFocused: () => root.current?.isFocused() || false,
219+
blur: () => root.current?.blur(),
220+
setSelection: (start: number, end: number) =>
221+
root.current?.setSelection(start, end),
222+
}));
238223

239224
const handleClearPress = (e: any) => {
240225
root.current?.clear();

0 commit comments

Comments
 (0)