Skip to content

Commit aad181a

Browse files
committed
docs: rework combobox demo
1 parent de050d0 commit aad181a

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

website/components/machines/combobox.tsx

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as combobox from "@zag-js/combobox"
2-
import { Portal, normalizeProps, useMachine } from "@zag-js/react"
2+
import { Portal, mergeProps, normalizeProps, useMachine } from "@zag-js/react"
33
import { useId, useMemo, useState } from "react"
4+
import { createFilter } from "@zag-js/i18n-utils"
45

56
interface ComboboxProps {
67
controls: {
@@ -60,6 +61,8 @@ const CaretIcon = () => (
6061
export function Combobox(props: ComboboxProps) {
6162
const [options, setOptions] = useState(comboboxData)
6263

64+
const filter = createFilter({ sensitivity: "base" })
65+
6366
const collection = useMemo(
6467
() =>
6568
combobox.collection({
@@ -73,30 +76,31 @@ export function Combobox(props: ComboboxProps) {
7376
const service = useMachine(combobox.machine, {
7477
id: useId(),
7578
collection,
76-
onOpenChange() {
77-
setOptions(comboboxData)
78-
},
7979
onInputValueChange({ inputValue }) {
8080
const filtered = comboboxData.filter((item) =>
81-
item.label.toLowerCase().includes(inputValue.toLowerCase()),
81+
filter.contains(item.label, inputValue),
8282
)
8383
setOptions(filtered.length > 0 ? filtered : comboboxData)
8484
},
8585
placeholder: "Type or select country",
8686
...props.controls,
8787
})
8888

89-
// { context: { ...props.controls, collection } },
90-
9189
const api = combobox.connect(service, normalizeProps)
9290

91+
const triggerProps = mergeProps(api.getTriggerProps(), {
92+
onClick() {
93+
setOptions(comboboxData)
94+
},
95+
})
96+
9397
return (
9498
<div>
9599
<div {...api.getRootProps()}>
96100
<label {...api.getLabelProps()}>Nationality</label>
97101
<div {...api.getControlProps()}>
98102
<input {...api.getInputProps()} />
99-
<button {...api.getTriggerProps()}>
103+
<button {...triggerProps}>
100104
<CaretIcon />
101105
</button>
102106
</div>

website/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@zag-js/docs": "workspace:*",
4343
"@zag-js/editable": "workspace:*",
4444
"@zag-js/file-upload": "workspace:*",
45+
"@zag-js/i18n-utils": "workspace:*",
4546
"@zag-js/hover-card": "workspace:*",
4647
"@zag-js/menu": "workspace:*",
4748
"@zag-js/number-input": "workspace:*",

0 commit comments

Comments
 (0)