Skip to main content

Select Hooks

The last part of this documentation covers the hooks of the Select component, which can also be imported directly from the library. So far, we've explored the functionality of the component and the customization of its parts. But what if you want to maintain the logic and functionality of the Select while integrating it into your custom-built component? This is where theuseSelectanduseSelectAsync hooks come into play. These hooks encapsulate all the logic required to build the functionality of the Select component.

  1. useSelect is the backbone of the Select component. It handles all of its core logic, from toggling the dropdown visibility to filtering the selected values and filtering the options by input. Much like the Select component itself, it accepts a handful of props that can modify its functionality for specific use cases.

  2. useSelectAsync manages the state and functionality of data fetching. This hook abstracts the complexities of handling API calls, state changes, and UI interactions such as searching and pagination, enabling seamless integration with a variety of backend systems. Think of this hook as an extension of useSelect. To use it, you must first initialize useSelect and pass its result into useSelectAsync.

Example

Here’s a simple example of a custom, lightweight Select component that can be opened and closed by clicking the dropdown indicator button, as opposed to the container itself. The functionality is entirely managed by the useSelect hook.

Select a value

useSelect

Arguments:

selectProps:

An object containing configuration options for the hook.

selectProps values

Property NameDescription
isMultiValueBoolean that determines if multiple values can be selected inside the select.
customStateAn object that holds custom state values specific to the select component.
customStateSettersFunctions to update customState.
labelKeySpecifies the key in the select options used as the display label.
fetchOnInputChangeBoolean that determines if the fetch function will get called when the input value changes.
clearInputOnIdicatorClickA boolean that determines whether the input field will be cleared when the clear indicator (e.g., "X" button) is clicked.
defaultSelectOptionsDefault options available in the select dropdown.
optionFilterA function that takes an individual option (iteratee) as an argument and filters the list of options based on the provided logic.
closeDropdownOnSelectBoolean that determines if the dropdown should close after an option is selected.
clearInputOnSelectA boolean that determines whether the input field should be cleared when an option is selected.
preventInputUpdateFunction holding custom logic to prevent input updates under certain conditions.
clearValueOnInputChangeBoolean that determines if the value should get cleared after the input value gets updated.
fetchOnScrollBoolean that determines if the fetch function should be called on page change triggered by scroll.
hasInputBoolean that indicates if the component has an input field for searching.
useAsyncBoolean that determines if options should be loaded asynchronously.
categoryKeySpecifies a key used for categorizing options.
removeSelectedOptionsFromListBoolean that determines if the selected options should be removed from the dropdown list.
sortFunctionCustom sorting function for the options list.
debounceInputUpdateEnables debouncing for input updates to optimize performance.
inputUpdateDebounceDurationTime in milliseconds to wait before updating input.
recordsPerPageNumber of records to fetch per page for pagination.
isLoadingBoolean that Indicates if data is currently being loaded.
isCategorizedBoolean that determines if the options should be displayed in categories.
inputFilterFunctionCustom function to filter options based on input.

Returns:

SelectApi

An object that provides access to key functionalities and state management of the select component.

SelectApi properties

Property NameDescription
selectStateObject that holds the current state of the select component.
selectStateUpdatersFunctions to update selectState.
selectFocusStateObject that holds the focus state details of the select options.
selectFocusHandlersFunctions to handle focus-related events like keyboard events and hovering.
handleOptionsInputFilterFilters available options based on the provided input value.
usesInputAsyncBoolean that determines if the select component fetches options asynchronously via input change.
onOptionSelectFunction that handles the selection or deselection of an option.
isLastPageReturns true if there are no more pages of options to load.
preventInputUpdateFunction that holds the resolved logic to prevent updates to the input value under certain conditions.
selectEventHandlersDefault event handlers for managing user interactions with the select component.
displayedOptionsThe list of currently displayed options, either flat or categorized.
getOriginalOptionsReturns the full list of unfiltered options.
setOriginalOptionsUpdates the full list of unfiltered options.
getSelectOptionsMapReturns a Map of option elements for DOM manipulation.
closeDropdownOnSelectBoolean that etermines whether the dropdown should close after selecting an option.
closeDropdownProgrammatically closes the dropdown menu.
focusInputProgrammatically focuses the input field.
selectDomRefsContains references to key DOM elements within the select component.
handlePageResetResets the pagination state to the first page.
clearInputOnSelectA boolean that determines whether the input field should be cleared when an option is selected. Holds the resolved value
loadNextPageIncrements the state of the page.
filterSearchedOptionsFilters the options based on the current input value.

useSelectAsync

Arguments:

selectApi:

The SelectApi

selectProps:

An object containing configuration options for the hook.

selectProps values

Property NameDescription
updateSelectOptionsAfterFetchA boolean that determines whether the select options should be updated after a fetch operation.
fetchFunctionA function used to fetch select options asynchronously.
useAsyncA boolean that indicates if the select options should be fetched asynchronously.
inputUpdateDebounceDurationThe duration in milliseconds to debounce the input update.
isLoadingA boolean that indicates if the select options are currently being loaded.
recordsPerPageThe number of records to fetch per page for pagination.
fetchOnInputChangeA boolean that determines if the fetch function should be called when the input value changes.
isLazyInitA boolean that determines whether the fetch function should be called when the dropdown is expanded for the first time, as opposed to on page load.
fetchOnScrollA boolean that determines if the fetch function should be called on page change triggered by scroll.

Returns:

SelectAsyncApi

An object that provides information based on the async operations of the component.

SelectAsyncApi properties:

Property NameDescription
isLastPageA function that returns a boolean indicating if the current page is the last page of options.
isInitialFetchA function that returns a boolean indicating if the current fetch is the initial fetch.
fetchOnScrollToBottomA boolean that determines if the fetch function should be triggered when scrolling to the bottom (return true if useAsync and fetchOnScroll are set to true ).
loadNextPageAsyncA function that loads the next page of options asynchronously.