1
1
"use client"
2
2
3
3
import {
4
- Badge ,
5
4
Box ,
6
5
Combobox ,
7
6
Heading ,
8
7
Stack ,
9
8
Text ,
10
- createListCollection ,
9
+ useFilter ,
10
+ useListCollection ,
11
11
} from "@chakra-ui/react"
12
- import { useState } from "react"
13
12
14
- const companies = [ "Apple" , "Amazon" , "Meta" , "Netflix" , "Google" ]
15
-
16
- export const ComboboxSelectionBehavior = ( ) => {
13
+ export const ComboboxWithSelectionBehavior = ( ) => {
17
14
return (
18
- < Stack gap = { 8 } p = { 4 } >
15
+ < Stack gap = "8" p = "4" >
19
16
< ComboboxDemo selectionBehavior = "replace" />
20
17
< ComboboxDemo selectionBehavior = "clear" />
21
18
< ComboboxDemo selectionBehavior = "preserve" />
@@ -29,12 +26,15 @@ const descriptions = {
29
26
preserve : "Input value is preserved after selection" ,
30
27
}
31
28
32
- function ComboboxDemo ( props : Partial < Combobox . RootProps > ) {
29
+ const ComboboxDemo = ( props : Partial < Combobox . RootProps > ) => {
33
30
const { selectionBehavior = "replace" } = props
34
- const [ value , setValue ] = useState < string [ ] > ( [ ] )
35
- const [ inputValue , setInputValue ] = useState ( "" )
36
31
37
- const collection = createListCollection ( { items : companies } )
32
+ const { contains } = useFilter ( { sensitivity : "base" } )
33
+
34
+ const { collection, filter } = useListCollection ( {
35
+ initialItems : companies ,
36
+ filter : contains ,
37
+ } )
38
38
39
39
return (
40
40
< Box >
@@ -46,48 +46,33 @@ function ComboboxDemo(props: Partial<Combobox.RootProps>) {
46
46
</ Stack >
47
47
48
48
< Combobox . Root
49
- multiple
50
- value = { value }
51
- inputValue = { inputValue }
52
49
collection = { collection }
53
50
selectionBehavior = { selectionBehavior }
54
- onValueChange = { ( details ) => setValue ( details . value ) }
55
- onInputValueChange = { ( details ) => setInputValue ( details . inputValue ) }
51
+ onInputValueChange = { ( details ) => filter ( details . inputValue ) }
56
52
>
57
53
< Combobox . Label > Select Companies</ Combobox . Label >
58
54
59
- { selectionBehavior !== "replace" && (
60
- < Stack direction = "row" >
61
- { value . map ( ( company ) => (
62
- < Badge key = { company } > { company } </ Badge >
63
- ) ) }
64
- </ Stack >
65
- ) }
66
-
67
55
< Combobox . Control >
68
56
< Combobox . Input />
69
- < Combobox . IndicatorGroup >
70
- < Combobox . ClearTrigger />
71
- < Combobox . Trigger />
72
- </ Combobox . IndicatorGroup >
73
57
</ Combobox . Control >
74
58
75
59
< Combobox . Content >
76
60
{ collection . items . map ( ( item ) => (
77
- < Combobox . Item key = { item } item = { item } >
78
- { item }
61
+ < Combobox . Item key = { item . value } item = { item } >
62
+ { item . label }
79
63
< Combobox . ItemIndicator />
80
64
</ Combobox . Item >
81
65
) ) }
82
66
</ Combobox . Content >
83
67
</ Combobox . Root >
84
-
85
- < Box mt = { 2 } p = { 2 } bg = "gray.50" rounded = "sm" >
86
- < Box textStyle = "sm" >
87
- < Box > Selected: { value . join ( ", " ) || "none" } </ Box >
88
- < Box > Input Value: { inputValue || "empty" } </ Box >
89
- </ Box >
90
- </ Box >
91
68
</ Box >
92
69
)
93
70
}
71
+
72
+ const companies = [
73
+ { label : "Apple" , value : "apple" } ,
74
+ { label : "Amazon" , value : "amazon" } ,
75
+ { label : "Meta" , value : "meta" } ,
76
+ { label : "Netflix" , value : "netflix" } ,
77
+ { label : "Google" , value : "google" } ,
78
+ ]
0 commit comments