useComboboxData - Admin Components
The useComboboxData hook loads paginated and searchable options from the MedusaGo backend for a Combobox component. The Medusa Admin uses it in forms where the user selects a record, such as a product's collection or a region's countries.
Example#
Pass the hook's returned properties to the Combobox component:
import { Combobox } from "@medusajs/dashboard/components"
import { useComboboxData } from "@medusajs/dashboard/hooks"
import { sdk } from "../lib/sdk"
const CollectionSelect = ({ value, onChange }) => {
const collections = useComboboxData({
queryKey: ["product_collections"],
queryFn: (params) =>
sdk.admin.productCollection.list(params),
getOptions: (data) =>
data.collections.map((collection) => ({
label: collection.title,
value: collection.id,
})),
})
return (
<Combobox
value={value}
onChange={onChange}
options={collections.options}
searchValue={collections.searchValue}
onSearchValueChange={
collections.onSearchValueChange
}
fetchNextPage={collections.fetchNextPage}
disabled={collections.disabled}
/>
)
}
The hook debounces the search value before sending the q query parameter to the MedusaGo backend, and it loads the next page of options when fetchNextPage is called.
To keep a record that the form already selected in the list, pass its ID to defaultValue or selectedValue. The hook then fetches that record separately and adds it to the beginning of options, even when the current page doesn't include it.
Parameters#
The hook accepts an object with the following properties:
queryKeyQueryKeyqueryFn(params) => Promise<TResponse>id, q, offset, and limit query parameters, and it must resolve to a response that has the offset, limit, and count properties.getOptions(data: TResponse) => TOption[]label and a value.defaultValuestring | string[]options when the user hasn't typed a search value.defaultValueKeykeyof TParamsdefaultValue. The default value is id.selectedValuestringoptions so that the selection stays visible while the user searches or paginates.pageSizenumber10.enabledbooleanfalse to defer loading, for example until another field has a value. The default value is true.Returns#
Along with the following, the hook returns the rest of TanStack Query's useInfiniteQuery result.
optionsTOption[]options prop of the Combobox component.searchValuestringonSearchValueChange(value: string) => voiddisabledbooleantrue when enabled is false, or when the first request resolved with no options and the user hasn't typed a search value.