44 * Select-all item, and the list of options.
55 */
66import { css } from "goober" ;
7- import React , { useMemo , useState } from "react" ;
7+ import React , { useCallback , useEffect , useMemo , useState } from "react" ;
88
99import { filterOptions } from "../lib/fuzzy-match-utils" ;
10+ import { debounce } from "../lib/debounce" ;
1011import getString from "../lib/get-string" ;
1112import { Option } from "../lib/interfaces" ;
1213import Cross from "./cross" ;
@@ -26,6 +27,7 @@ interface ISelectPanelProps {
2627 filterOptions ?: ( options : Option [ ] , filter : string ) => Option [ ] ;
2728 overrideStrings ?: { [ key : string ] : string } ;
2829 ClearIcon ?;
30+ debounceDuration ?: number ;
2931}
3032
3133enum FocusType {
@@ -74,11 +76,17 @@ export const SelectPanel = (props: ISelectPanelProps) => {
7476 hasSelectAll,
7577 overrideStrings,
7678 ClearIcon,
79+ debounceDuration,
7780 } = props ;
7881 const [ searchText , setSearchText ] = useState ( "" ) ;
82+ const [ searchTextForFilter , setSearchTextForFilter ] = useState ( "" ) ;
7983 const [ focusIndex , setFocusIndex ] = useState (
8084 focusSearchOnOpen && ! disableSearch ? FocusType . SEARCH : FocusType . NONE
8185 ) ;
86+ const debouncedSearch = useCallback (
87+ debounce ( ( query ) => setSearchTextForFilter ( query ) , debounceDuration ) ,
88+ [ ]
89+ ) ;
8290
8391 const selectAllOption = {
8492 label : selectAllLabel || getString ( "selectAll" , overrideStrings ) ,
@@ -106,11 +114,15 @@ export const SelectPanel = (props: ISelectPanelProps) => {
106114 } ;
107115
108116 const handleSearchChange = ( e ) => {
117+ debouncedSearch ( e . target . value ) ;
109118 setSearchText ( e . target . value ) ;
110119 setFocusIndex ( FocusType . SEARCH ) ;
111120 } ;
112121
113- const handleClear = ( ) => setSearchText ( "" ) ;
122+ const handleClear = ( ) => {
123+ setSearchTextForFilter ( "" ) ;
124+ setSearchText ( "" ) ;
125+ } ;
114126
115127 const handleItemClicked = ( index : number ) => setFocusIndex ( index ) ;
116128
@@ -141,8 +153,8 @@ export const SelectPanel = (props: ISelectPanelProps) => {
141153
142154 const filteredOptions = ( ) =>
143155 customFilterOptions
144- ? customFilterOptions ( options , searchText )
145- : filterOptions ( options , searchText ) ;
156+ ? customFilterOptions ( options , searchTextForFilter )
157+ : filterOptions ( options , searchTextForFilter ) ;
146158
147159 const updateFocus = ( offset : number ) => {
148160 let newFocus = focusIndex + offset ;
0 commit comments