|
1
|
--- |
|
2
|
sidebar_label: "useComboboxData" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `useComboboxData - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This hook is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The `useComboboxData` hook loads paginated and searchable options from the Medusa backend for a [Combobox](../../components/combobox/page.mdx) 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. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { useComboboxData } from "@medusajs/dashboard/hooks" |
|
23
|
``` |
|
24
|
|
|
25
|
--- |
|
26
|
|
|
27
|
## Example |
|
28
|
|
|
29
|
Pass the hook's returned properties to the `Combobox` component: |
|
30
|
|
|
31
|
```tsx |
|
32
|
import { Combobox } from "@medusajs/dashboard/components" |
|
33
|
import { useComboboxData } from "@medusajs/dashboard/hooks" |
|
34
|
import { sdk } from "../lib/sdk" |
|
35
|
|
|
36
|
const CollectionSelect = ({ value, onChange }) => { |
|
37
|
const collections = useComboboxData({ |
|
38
|
queryKey: ["product_collections"], |
|
39
|
queryFn: (params) => |
|
40
|
sdk.admin.productCollection.list(params), |
|
41
|
getOptions: (data) => |
|
42
|
data.collections.map((collection) => ({ |
|
43
|
label: collection.title, |
|
44
|
value: collection.id, |
|
45
|
})), |
|
46
|
}) |
|
47
|
|
|
48
|
return ( |
|
49
|
<Combobox |
|
50
|
value={value} |
|
51
|
onChange={onChange} |
|
52
|
options={collections.options} |
|
53
|
searchValue={collections.searchValue} |
|
54
|
onSearchValueChange={ |
|
55
|
collections.onSearchValueChange |
|
56
|
} |
|
57
|
fetchNextPage={collections.fetchNextPage} |
|
58
|
disabled={collections.disabled} |
|
59
|
/> |
|
60
|
) |
|
61
|
} |
|
62
|
``` |
|
63
|
|
|
64
|
The hook debounces the search value before sending the `q` query parameter to the Medusa backend, and it loads the next page of options when `fetchNextPage` is called. |
|
65
|
|
|
66
|
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. |
|
67
|
|
|
68
|
--- |
|
69
|
|
|
70
|
## Parameters |
|
71
|
|
|
72
|
The hook accepts an object with the following properties: |
|
73
|
|
|
74
|
<TypeList |
|
75
|
types={[ |
|
76
|
{ |
|
77
|
name: "queryKey", |
|
78
|
type: "`QueryKey`", |
|
79
|
optional: false, |
|
80
|
description: "Required. The TanStack Query key that the hook uses as the base for its queries. The hook appends the search query and the default or selected value to it.", |
|
81
|
}, |
|
82
|
{ |
|
83
|
name: "queryFn", |
|
84
|
type: "`(params) => Promise<TResponse>`", |
|
85
|
optional: false, |
|
86
|
description: "Required. A function that fetches the options from the Medusa backend. It receives an object that may have the `id`, `q`, `offset`, and `limit` query parameters, and it must resolve to a response that has the `offset`, `limit`, and `count` properties.", |
|
87
|
}, |
|
88
|
{ |
|
89
|
name: "getOptions", |
|
90
|
type: "`(data: TResponse) => TOption[]`", |
|
91
|
optional: false, |
|
92
|
description: "Required. A function that transforms a page of the response into an array of options. Each option has a `label` and a `value`.", |
|
93
|
}, |
|
94
|
{ |
|
95
|
name: "defaultValue", |
|
96
|
type: "`string | string[]`", |
|
97
|
optional: false, |
|
98
|
description: "The value or values that the hook must always include in the returned options. The hook fetches them in a separate query and adds them to the beginning of `options` when the user hasn't typed a search value.", |
|
99
|
}, |
|
100
|
{ |
|
101
|
name: "defaultValueKey", |
|
102
|
type: "`keyof TParams`", |
|
103
|
optional: false, |
|
104
|
description: "The query parameter that the hook uses to fetch the `defaultValue`. The default value is `id`.", |
|
105
|
}, |
|
106
|
{ |
|
107
|
name: "selectedValue", |
|
108
|
type: "`string`", |
|
109
|
optional: false, |
|
110
|
description: "The ID of the currently selected record. The hook fetches it in a separate query and adds it to the beginning of `options` so that the selection stays visible while the user searches or paginates.", |
|
111
|
}, |
|
112
|
{ |
|
113
|
name: "pageSize", |
|
114
|
type: "`number`", |
|
115
|
optional: false, |
|
116
|
description: "The number of options to fetch per page. The default value is `10`.", |
|
117
|
}, |
|
118
|
{ |
|
119
|
name: "enabled", |
|
120
|
type: "`boolean`", |
|
121
|
optional: false, |
|
122
|
description: "Whether the hook sends its queries. Set it to `false` to defer loading, for example until another field has a value. The default value is `true`.", |
|
123
|
}, |
|
124
|
]} |
|
125
|
/> |
|
126
|
|
|
127
|
--- |
|
128
|
|
|
129
|
## Returns |
|
130
|
|
|
131
|
Along with the following, the hook returns the rest of TanStack Query's [`useInfiniteQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery) result. |
|
132
|
|
|
133
|
<TypeList |
|
134
|
types={[ |
|
135
|
{ |
|
136
|
name: "options", |
|
137
|
type: "`TOption[]`", |
|
138
|
description: "The options of all loaded pages, including the default and selected values. Pass it to the `options` prop of the `Combobox` component.", |
|
139
|
}, |
|
140
|
{ |
|
141
|
name: "searchValue", |
|
142
|
type: "`string`", |
|
143
|
description: "The current value of the search input before debouncing.", |
|
144
|
}, |
|
145
|
{ |
|
146
|
name: "onSearchValueChange", |
|
147
|
type: "`(value: string) => void`", |
|
148
|
description: "A function that changes the search value. The hook debounces it for 300 milliseconds before it refetches the options.", |
|
149
|
}, |
|
150
|
{ |
|
151
|
name: "disabled", |
|
152
|
type: "`boolean`", |
|
153
|
description: "Whether the combobox has no data to search in. It's `true` when `enabled` is `false`, or when the first request resolved with no options and the user hasn't typed a search value.", |
|
154
|
}, |
|
155
|
]} |
|
156
|
/> |