1
1
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"
3
3
import { useId , useMemo , useState } from "react"
4
+ import { createFilter } from "@zag-js/i18n-utils"
4
5
5
6
interface ComboboxProps {
6
7
controls : {
@@ -60,6 +61,8 @@ const CaretIcon = () => (
60
61
export function Combobox ( props : ComboboxProps ) {
61
62
const [ options , setOptions ] = useState ( comboboxData )
62
63
64
+ const filter = createFilter ( { sensitivity : "base" } )
65
+
63
66
const collection = useMemo (
64
67
( ) =>
65
68
combobox . collection ( {
@@ -73,30 +76,31 @@ export function Combobox(props: ComboboxProps) {
73
76
const service = useMachine ( combobox . machine , {
74
77
id : useId ( ) ,
75
78
collection,
76
- onOpenChange ( ) {
77
- setOptions ( comboboxData )
78
- } ,
79
79
onInputValueChange ( { inputValue } ) {
80
80
const filtered = comboboxData . filter ( ( item ) =>
81
- item . label . toLowerCase ( ) . includes ( inputValue . toLowerCase ( ) ) ,
81
+ filter . contains ( item . label , inputValue ) ,
82
82
)
83
83
setOptions ( filtered . length > 0 ? filtered : comboboxData )
84
84
} ,
85
85
placeholder : "Type or select country" ,
86
86
...props . controls ,
87
87
} )
88
88
89
- // { context: { ...props.controls, collection } },
90
-
91
89
const api = combobox . connect ( service , normalizeProps )
92
90
91
+ const triggerProps = mergeProps ( api . getTriggerProps ( ) , {
92
+ onClick ( ) {
93
+ setOptions ( comboboxData )
94
+ } ,
95
+ } )
96
+
93
97
return (
94
98
< div >
95
99
< div { ...api . getRootProps ( ) } >
96
100
< label { ...api . getLabelProps ( ) } > Nationality</ label >
97
101
< div { ...api . getControlProps ( ) } >
98
102
< input { ...api . getInputProps ( ) } />
99
- < button { ...api . getTriggerProps ( ) } >
103
+ < button { ...triggerProps } >
100
104
< CaretIcon />
101
105
</ button >
102
106
</ div >
0 commit comments