You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/app/(docs)/react/components/combobox/page.mdx
+87-5Lines changed: 87 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,18 +71,98 @@ import { Combobox } from '@base-ui/react/combobox';
71
71
</Combobox.Root>;
72
72
```
73
73
74
-
## TypeScript
74
+
## Value modes
75
75
76
-
Combobox infers the item type from the `defaultValue` or `value` props passed to `<Combobox.Root>`.
77
-
The type of items held in the `items` array must also match the `value` prop type passed to `<Combobox.Item>`.
76
+
Combobox supports two value modes:
77
+
78
+
-**Item values**: `value` / `defaultValue` has the same shape as the entries in the `items` array.
79
+
-**Keyed values**: `value` / `defaultValue` is a primitive while `items` use the `{ value, label }` object shape.
80
+
81
+
In the keyed mode, the combobox uses `item.label` for display and filtering, and `item.value` as the selected value. Other object shapes such as `{ id, name }` are not supported by the keyed mode; use item-shaped values instead.
82
+
83
+
### Item values
84
+
85
+
Use object-shaped values when the selected value should keep the full item data instead of a primitive key.
86
+
87
+
- Use `itemToStringLabel` when the object does not already have a `label` field, or when you want to override the default `label` text shown in the input or trigger.
88
+
- Use `itemToStringValue` when form submission should serialize the object as a string, such as an `id`.
89
+
- Use `isItemEqualToValue` when items are re-created across renders or loaded asynchronously and object identity is not stable.
| name |`string`| - | Identifies the field when a form is submitted. |
17
-
| defaultValue |`Value[] \| Value \| null`| - | The uncontrolled selected value of the combobox when it's initially rendered. To render a controlled combobox, use the `value` prop instead. |
18
-
| value |`Value[] \| Value \| null`| - | The selected value of the combobox. Use when controlled.|
17
+
| defaultValue |`Value[] \| Value \| null`| - | The uncontrolled selected value of the combobox when it's initially rendered.
Must match the item value shape, except when `items` use the `{ value, label }` shape, in which case a primitive `item.value` is also supported. To render a controlled combobox, use the `value` prop instead.|
18
+
| value |`Value[] \| Value \| null`| - | The selected value of the combobox. Use when controlled.
Must match the item value shape, except when `items` use the `{ value, label }` shape, in which case a primitive `item.value` is also supported.|
19
19
| onValueChange |`((value: Value[] \| Value \| null, eventDetails: Combobox.Root.ChangeEventDetails) => void)`| - | Event handler called when the selected value of the combobox changes. |
20
20
| defaultInputValue |`string \| number \| string[]`| - | The uncontrolled input value when initially rendered. To render a controlled input, use the `inputValue` prop instead. |
21
21
| inputValue |`string \| string[] \| number`| - | The input value of the combobox. Use when controlled. |
@@ -33,9 +33,9 @@ Doesn't render its own HTML element.
33
33
| grid |`boolean`|`false`| Whether list items are presented in a grid layout.
When enabled, arrow keys navigate across rows and columns inferred from DOM rows. |
34
34
| inline |`boolean`|`false`| Whether the list is rendered inline without using the popup. |
35
35
| isItemEqualToValue |`((itemValue: Value, value: Value) => boolean)`| - | Custom comparison logic used to determine if a combobox item value matches the current selected value. Useful when item values are objects without matching referentially.
Defaults to `Object.is` comparison. |
36
-
| itemToStringLabel |`((itemValue: Value) => string)`| - | When the item values are objects (`<Combobox.Item value={object}>`), this function converts the object value to a string representation for display in the input.
If the shape of the object is `{ value, label }`, the label will be used automatically without needing to specify this prop.|
36
+
| itemToStringLabel |`((itemValue: Value) => string)`| - | When the item values are objects (`<Combobox.Item value={object}>`), this function converts the object value to a string representation for display in the input.
If the shape of the object is `{ value, label }`, the label will be used automatically for display and filtering without needing to specify this prop.
In that keyed mode, this prop is bypassed for the `{ value, label }` item array itself and only applies as a fallback when resolving other value shapes.|
37
37
| itemToStringValue |`((itemValue: Value) => string)`| - | When the item values are objects (`<Combobox.Item value={object}>`), this function converts the object value to a string representation for form submission.
If the shape of the object is `{ value, label }`, the value will be used automatically without needing to specify this prop. |
38
-
| items |`any[] \| Group[]`| - | The items to be displayed in the list.
Can be either a flat array of items or an array of groups with items.|
38
+
| items |`any[] \| Group[]`| - | The items to be displayed in the list.
Can be either a flat array of items or an array of groups with items.
Primitive `value`/`defaultValue` is only supported implicitly when each item has the shape `{ value, label }`.|
39
39
| limit |`number`|`-1`| The maximum number of items to display in the list. |
40
40
| locale |`Intl.LocalesArgument`| - | The locale to use for string comparison.
Defaults to the user's runtime locale. |
41
41
| loopFocus |`boolean`|`true`| Whether to loop keyboard focus back to the input when the end of the list is reached while using the arrow keys. The first item can then be reached by pressing ArrowDown again from the input, or the last item can be reached by pressing ArrowUp from the input.
The input is always included in the focus loop per [ARIA Authoring Practices](https://www.w3.org/WAI/ARIA/apg/patterns/combobox/).
When disabled, focus does not move when on the last element and the user presses ArrowDown, or when on the first element and the user presses ArrowUp. |
0 commit comments