Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions src/hooks/useSelect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function useSelect(userProps = {}) {
itemToString,
getA11ySelectionMessage,
getA11yStatusMessage,
inputCleanupDebounceWait,
} = props
// Initial state depending on controlled props.
const initialState = getInitialState(props)
Expand Down Expand Up @@ -108,27 +109,31 @@ function useSelect(userProps = {}) {

// Sets cleanup for the keysSoFar callback, debounded after 500ms.
useEffect(() => {
// init the clean function here as we need access to dispatch.
clearTimeoutRef.current = debounce(outerDispatch => {
outerDispatch({
type: stateChangeTypes.FunctionSetInputValue,
inputValue: '',
})
}, 500)
if (inputCleanupDebounceWait > 0) {
// init the clean function here as we need access to dispatch.
clearTimeoutRef.current = debounce(outerDispatch => {
outerDispatch({
type: stateChangeTypes.FunctionSetInputValue,
inputValue: '',
})
}, inputCleanupDebounceWait)
} else {
clearTimeoutRef.current = null;
}

// Cancel any pending debounced calls on mount
return () => {
clearTimeoutRef.current.cancel()
clearTimeoutRef?.current.cancel()
}
}, [])
}, [inputCleanupDebounceWait])

// Invokes the keysSoFar callback set up above.
useEffect(() => {
if (!inputValue) {
return
}

clearTimeoutRef.current(dispatch)
clearTimeoutRef.current?.(dispatch)
}, [dispatch, inputValue])

useControlPropsValidator({
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useSelect/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const propTypes = {
items: PropTypes.array.isRequired,
isItemDisabled: PropTypes.func,
getA11ySelectionMessage: PropTypes.func,
inputCleanupDebounceWait: PropTypes.number,
}

/**
Expand Down Expand Up @@ -79,6 +80,7 @@ export const defaultProps = {
isItemDisabled() {
return false
},
inputCleanupDebounceWait: 500,
}

// eslint-disable-next-line import/no-mutable-exports
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ export interface UseSelectProps<Item> {
onHighlightedIndexChange?: (changes: UseSelectStateChange<Item>) => void
onStateChange?: (changes: UseSelectStateChange<Item>) => void
environment?: Environment
inputCleanupDebounceWait?: number;
}

export interface UseSelectStateChangeOptions<Item>
Expand Down