@dax-ui/async-dropdown
v0.8.0
Published
dax-ui AsyncDropdown: server-backed select with debounced search, infinite-scroll pagination, caching, error retry and multi-select chips
Maintainers
Readme
@dax-ui/async-dropdown
TODO: one or two sentences on what this component is for.
Installation
pnpm add @dax-ui/async-dropdownMUI and Emotion are included; the app only needs React 18 or 19.
One-time setup per app (skip if the app already uses dax-ui): install
@dax-ui/theme and wrap the app in its UIProvider.
pnpm add @dax-ui/themeUsage
import { AsyncDropdown } from '@dax-ui/async-dropdown';
<AsyncDropdown />Props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| loadOptions (required) | AsyncLoadOptions<T> | | Loads one page of options: ({ search, page, pageSize, signal }) => Promise<{ options, hasMore, total? }>. Pages start at 1. Pass signal to fetch so stale requests are cancelled. |
| autocompleteProps | Partial<AutocompleteProps<T, boolean, boolean, false, "div", undefined>> | | Extra props for the MUI Autocomplete underneath, for anything not covered above (slotProps, filterOptions, autoHighlight, openOnFocus, ...). Applied last. |
| cacheOptions | boolean | false | Keeps loaded pages per search term, so returning to a search is instant. |
| className | string | | Class name for the field's root element. |
| clearable | boolean | true | Shows a clear (×) button when there is a value. |
| clearIcon | ReactNode | | Replaces the × of the clear button. |
| colors | Partial<DropdownColors> | | Overrides any of the input, label, menu or chip colors, e.g. { chipBackground: '#4f46e5', optionSelected: '#eef2ff' }. |
| debounceMs | number | 300 | Waits this long after the last keystroke before searching, in ms. |
| defaultValue | T[] \| DropdownOption<DropdownValue> \| null | | Initial selected option object(s), for an uncontrolled dropdown. |
| disableCloseOnSelect | boolean | true when multiple, else false | Keeps the menu open after choosing an option. |
| disabled | boolean | false | Disables the field. |
| emptyText | ReactNode \| ((search: string) => ReactNode) | (search) => search ? No results for “${search}” : 'No options' | Shown when nothing matches. A function receives the search. |
| error | ReactNode | false | Error state: true turns the field red and marks the control aria-invalid; a message (string or node) does that and shows the message in place of helperText. |
| errorText | ReactNode \| ((error: unknown) => ReactNode) | Couldn't load options. | Message shown when a request fails, before the Retry button. A function receives the error. |
| fieldSlotProps | FormFieldSlotProps | | Style overrides for the label row, label, helper text and counter. |
| fullWidth | boolean | true | Stretches the field to the full width of its container. |
| getOptionKey | ((option: T) => string \| number) | (option) => option.value | Unique key of an option, used to match options with the value and to drop duplicates across pages. |
| helperIcon | ReactNode | an info icon (ⓘ) | Icon before the helper or error text. Pass null to show the text without one. |
| helperText | ReactNode | | Text under the control: a hint normally. Replaced by the error message when error is a message. |
| hideLabel | boolean | false | Hides the label visually; it stays the control's accessible name. |
| id | string | | Id of the control, which the label points at. Generated when not set. |
| label | ReactNode | | Label shown above the control. Also the control's accessible name, so always set it (use hideLabel to hide it visually). |
| labelEnd | ReactNode | | Content at the far end of the label row, e.g. a "Forgot password?" link or an info tooltip. |
| labelIcon | ReactNode | | Decorative icon before the label text, e.g. <CalendarIcon />. Sized to 16px. |
| labelPlacement | "top" \| "start" | top | Where the label sits: top (above the control) or start (beside it, labelWidth wide). |
| labelWidth | string \| number | 160 | Width of the label column when labelPlacement is start. A number is px. |
| limitTags | number | -1 | Maximum number of chips shown while the field isn't focused; the rest become "+N". -1 shows all. |
| loadingMoreText | ReactNode | Loading more… | Text of the row shown while the next page loads. |
| loadMoreOnScroll | boolean | true | Loads the next page when the list is scrolled near its end, or the keyboard highlight nears the last option. Off, only the "Load more" row loads more. |
| loadMoreText | ReactNode | Load more | Text of the row that loads the next page. |
| loadOnOpen | boolean | true | Loads the first page as soon as the menu opens. Off, nothing loads until the user types. |
| locked | boolean | false | Read-only with the disabled look (grey background). The value can't change, but the field stays focusable and readable. For values fixed after creation, e.g. "Site". |
| maxMenuHeight | number | 320 | Maximum height of the menu's list before it scrolls, in px. |
| minSearchLength | number | 0 | Searches shorter than this aren't sent; the menu asks for more characters instead. |
| minSearchText | ((minSearchLength: number) => ReactNode) | (min) => Type at least ${min} characters to search`` | Shown when the search is shorter than minSearchLength. |
| multiple | boolean | false | Allows selecting several options, shown as removable chips. The value becomes an array. |
| onChange | ((value: Multiple extends true ? T[] : T \| null) => void) | | Called with the new selected option(s). |
| onClose | ((event: SyntheticEvent<Element, Event>) => void) | | Called when the menu closes. |
| onOpen | ((event: SyntheticEvent<Element, Event>) => void) | | Called when the menu opens. |
| onSearchChange | ((search: string) => void) | | Called when the user types in the input. |
| optional | boolean | false | Adds "(optional)" after the label, for forms where most fields are required. |
| optionalText | ReactNode | (optional) | Text shown after the label when optional is set. |
| pageSize | number | 20 | Options per page, passed to loadOptions. |
| placeholder | string | | Hint shown in the empty input, e.g. "Select a department" or "Search users". |
| popupIcon | ReactNode | | Replaces the chevron that opens the menu. |
| radius | number | 12 | Corner radius of the input, in px. |
| readOnly | boolean | false | Read-only with the normal look. |
| ref | Ref<HTMLDivElement> | | Forwarded to the root element. |
| reloadKey | unknown | | Change it to discard loaded options and the cache, e.g. when a filter the request depends on changes. |
| renderOption | ((option: T, state: DropdownRenderOptionState) => ReactNode) | | Renders the content of each option in the menu (the <li> around it is kept). Defaults to DropdownOptionContent: icon, label, description and a check mark. Return undefined to use the default for an option. |
| renderValue | ((value: T \| T[], getItemProps: (args: { index: number; }) => DropdownChipItemProps) => ReactNode) | | Renders the selected value(s) inside the input. With multiple, return one element per value (spread getItemProps({ index }) on each, e.g. on a DropdownChip) so limitTags works. |
| required | boolean | false | Marks the field as required: a red asterisk after the label, and aria-required on the control. |
| requiredIndicator | ReactNode | * | What marks a required label. Hidden from screen readers (the control is announced as required instead). |
| retryText | ReactNode | Retry | Label of the retry button. |
| searchable | boolean | true | Lets the user type to filter the options. Off, the dropdown behaves like a <select>: click or press Enter/arrows to open, and the input can't be typed into. |
| showCheckmarks | boolean | true | Shows a check mark after selected options in the menu. |
| showCount | boolean | true | Shows "20 of 214" under the options when loadOptions reports a total. |
| size | "small" \| "medium" \| "large" | medium | Input height: small (40px), medium (52px) or large (60px). Grows to fit chips. |
| skeletonRows | number | 4 | Number of placeholder rows shown while the first page loads. |
| startIcon | ReactNode | | Decorative icon inside the input, before the value, e.g. <SearchIcon />. |
| style | CSSProperties | | Inline styles for the field's root element. |
| sx | SxProps<Theme> | | Style overrides for the field's root element. |
| value | T[] \| DropdownOption<DropdownValue> \| null | | The selected option object(s), for a controlled dropdown. Options are objects (not just values) because the selected option may not be on the loaded page. |
| variant | "outline" \| "filled" | outline | Input style: outline (surface background with a border) or filled (tinted background, no border until focused or in error). Set it for a whole app with <FieldProvider variant="filled">. |
Run pnpm docs to fill in the table above from the JSDoc in src/AsyncDropdown.types.ts.
