Skip to content

Commit 016acc3

Browse files
committed
feat(Select): add clearSearchOnClose prop to reset search input on dropdown close
1 parent ad7fa7c commit 016acc3

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

packages/components/src/ui/select.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export interface SelectProps<T extends React.Key = string>
4949
// Search behavior
5050
searchable?: boolean;
5151
searchInputProps?: React.ComponentPropsWithoutRef<typeof CommandInput>;
52+
// Clear the search input when the dropdown closes
53+
clearSearchOnClose?: boolean;
5254
// Creatable behavior
5355
creatable?: boolean;
5456
onCreateOption?: (input: string) => SelectOption<T> | Promise<SelectOption<T>>;
@@ -74,6 +76,7 @@ export function Select<T extends React.Key = string>({
7476
components,
7577
searchable = true,
7678
searchInputProps,
79+
clearSearchOnClose = false,
7780
creatable = false,
7881
onCreateOption,
7982
createOptionLabel,
@@ -96,6 +99,13 @@ export function Select<T extends React.Key = string>({
9699
});
97100
}, [popoverState.isOpen]);
98101

102+
// When closing, optionally clear the search query to reset the input value
103+
useEffect(() => {
104+
if (!popoverState.isOpen && clearSearchOnClose) {
105+
setSearchQuery('');
106+
}
107+
}, [popoverState.isOpen, clearSearchOnClose]);
108+
99109
const selectedOption = options.find((o) => o.value === value);
100110

101111
const Trigger =

0 commit comments

Comments
 (0)