|
1
|
--- |
|
2
|
sidebar_label: "Combobox" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Combobox - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This component is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The `Combobox` component is a select input with search, multi-select, pagination, and the option to create new values. The Medusa Admin uses it in forms where the user chooses records loaded from the Medusa backend, such as a product's collection or a region's payment providers. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { Combobox } from "@medusajs/dashboard/components" |
|
23
|
``` |
|
24
|
|
|
25
|
--- |
|
26
|
|
|
27
|
## Example |
|
28
|
|
|
29
|
Pair `Combobox` with the [useComboboxData](../../hooks/use-combobox-data/page.mdx) hook to load the options from the Medusa backend and paginate them as the user scrolls: |
|
30
|
|
|
31
|
```tsx |
|
32
|
import { Combobox, Form } from "@medusajs/dashboard/components" |
|
33
|
import { useComboboxData } from "@medusajs/dashboard/hooks" |
|
34
|
import { sdk } from "../lib/sdk" |
|
35
|
|
|
36
|
const CollectionField = ({ control }) => { |
|
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
|
<Form.Field |
|
50
|
control={control} |
|
51
|
name="collection_id" |
|
52
|
render={({ field }) => ( |
|
53
|
<Form.Item> |
|
54
|
<Form.Label>Collection</Form.Label> |
|
55
|
<Form.Control> |
|
56
|
<Combobox |
|
57
|
{...field} |
|
58
|
options={collections.options} |
|
59
|
searchValue={collections.searchValue} |
|
60
|
onSearchValueChange={ |
|
61
|
collections.onSearchValueChange |
|
62
|
} |
|
63
|
fetchNextPage={collections.fetchNextPage} |
|
64
|
disabled={collections.disabled} |
|
65
|
/> |
|
66
|
</Form.Control> |
|
67
|
<Form.ErrorMessage /> |
|
68
|
</Form.Item> |
|
69
|
)} |
|
70
|
/> |
|
71
|
) |
|
72
|
} |
|
73
|
``` |
|
74
|
|
|
75
|
To let the user select multiple values, pass an array as the `value` prop. The `onChange` function then receives an array of the selected values. |
|
76
|
|
|
77
|
--- |
|
78
|
|
|
79
|
## Props |
|
80
|
|
|
81
|
Aside from the HTML props of an `input` element, `Combobox` accepts the following props: |
|
82
|
|
|
83
|
<TypeList |
|
84
|
types={[ |
|
85
|
{ |
|
86
|
name: "options", |
|
87
|
type: "`object[]`", |
|
88
|
optional: false, |
|
89
|
description: `The options the user can select from. This is the only required prop.`, |
|
90
|
children: [ |
|
91
|
{ |
|
92
|
name: "value", |
|
93
|
type: "`string`", |
|
94
|
optional: false, |
|
95
|
description: `The option's value, which is set as the component's value when the user selects the option.`, |
|
96
|
}, |
|
97
|
{ |
|
98
|
name: "label", |
|
99
|
type: "`string`", |
|
100
|
optional: false, |
|
101
|
description: `The option's label, which is shown to the user in the options list.`, |
|
102
|
}, |
|
103
|
{ |
|
104
|
name: "disabled", |
|
105
|
type: "`boolean`", |
|
106
|
optional: true, |
|
107
|
description: `Whether the user can't select the option.`, |
|
108
|
}, |
|
109
|
], |
|
110
|
}, |
|
111
|
{ |
|
112
|
name: "value", |
|
113
|
type: "`string` \\| `string[]`", |
|
114
|
optional: true, |
|
115
|
description: `The selected value. Pass an array to allow selecting multiple values. If you don't pass this prop, the component manages the value internally.`, |
|
116
|
}, |
|
117
|
{ |
|
118
|
name: "onChange", |
|
119
|
type: "`(value?: string | string[]) => void`", |
|
120
|
optional: true, |
|
121
|
description: `The function to execute when the user selects or removes a value.`, |
|
122
|
}, |
|
123
|
{ |
|
124
|
name: "searchValue", |
|
125
|
type: "`string`", |
|
126
|
optional: true, |
|
127
|
description: `The search query. Pass it when you filter the options yourself, such as when you load them from the Medusa backend. Otherwise, the component filters and sorts the options for you.`, |
|
128
|
}, |
|
129
|
{ |
|
130
|
name: "onSearchValueChange", |
|
131
|
type: "`(value: string) => void`", |
|
132
|
optional: true, |
|
133
|
description: `The function to execute when the user changes the search query.`, |
|
134
|
}, |
|
135
|
{ |
|
136
|
name: "fetchNextPage", |
|
137
|
type: "`() => void`", |
|
138
|
optional: true, |
|
139
|
description: `The function to execute when the user scrolls to the end of the options list. Use it to load the next page of options.`, |
|
140
|
}, |
|
141
|
{ |
|
142
|
name: "isFetchingNextPage", |
|
143
|
type: "`boolean`", |
|
144
|
optional: true, |
|
145
|
description: `Whether the next page of options is loading. The component shows a loading placeholder at the end of the list.`, |
|
146
|
}, |
|
147
|
{ |
|
148
|
name: "onCreateOption", |
|
149
|
type: "`(value: string) => void`", |
|
150
|
optional: true, |
|
151
|
description: `The function to execute when the user creates an option from the search query. Pass it to show a create option in the list.`, |
|
152
|
}, |
|
153
|
{ |
|
154
|
name: "shouldAlwaysShowCreateOption", |
|
155
|
type: "`boolean`", |
|
156
|
optional: true, |
|
157
|
description: `Whether to show the create option even when the search query matches existing options.`, |
|
158
|
}, |
|
159
|
{ |
|
160
|
name: "noResultsPlaceholder", |
|
161
|
type: "`React.ReactNode`", |
|
162
|
optional: true, |
|
163
|
description: `The content to show when there are no options and the user hasn't typed a search query.`, |
|
164
|
}, |
|
165
|
{ |
|
166
|
name: "allowClear", |
|
167
|
type: "`boolean`", |
|
168
|
optional: true, |
|
169
|
description: `Whether to show a button that clears the selected value.`, |
|
170
|
}, |
|
171
|
{ |
|
172
|
name: "forceHideInput", |
|
173
|
type: "`boolean`", |
|
174
|
optional: true, |
|
175
|
description: `Whether to always hide the search input. Use it for selects whose options the user doesn't need to search.`, |
|
176
|
}, |
|
177
|
{ |
|
178
|
name: "displayMode", |
|
179
|
type: '`"count"` \\| `"chips"`', |
|
180
|
optional: true, |
|
181
|
defaultValue: "count", |
|
182
|
description: `How to display multiple selected values. \`count\` shows the number of selected values, and \`chips\` shows a badge for each value.`, |
|
183
|
}, |
|
184
|
{ |
|
185
|
name: "placeholder", |
|
186
|
type: "`string`", |
|
187
|
optional: true, |
|
188
|
description: `The placeholder to show when the user hasn't selected a value.`, |
|
189
|
}, |
|
190
|
]} |
|
191
|
/> |