@@ -7,7 +7,7 @@ route: /downshift
7
7
import {useState } from ' react'
8
8
import {Playground } from ' docz'
9
9
import Downshift from ' downshift'
10
- import {items , menuStyles , comboboxStyles } from ' ../utils'
10
+ import {itemFruits as items , menuStyles , comboboxStyles } from ' ../utils'
11
11
12
12
# Downshift
13
13
@@ -49,7 +49,7 @@ are used as follows:
49
49
50
50
For a complete documentation on all the returned props, component props and more
51
51
information check out the
52
- [ Github Page] ( https:// github.com/downshift-js/downshift ) .
52
+ [ Github Page] [ github-page ] .
53
53
54
54
## Important
55
55
@@ -87,20 +87,17 @@ It is possible to achieve the correct HTML structure only if you use
87
87
` getRootProps ` getter prop from ` Downshift ` . You apply ` getRootProps ` on the
88
88
wrapper element that contains the ` <input> ` and optionally the trigger button.
89
89
90
- [ CodeSandbox] ( https://codesandbox.io/s/simple-downshift-with-getrootprops-example-24s13 )
90
+ [ CodeSandbox for usage with getRootProps. ] [ code-sandbox-no-get-root-props ]
91
91
92
92
<Playground >
93
93
{ () => {
94
94
const DropdownCombobox = () => {
95
95
return (
96
96
<Downshift
97
- onChange = { selection =>
98
- alert (
99
- selection
100
- ? ` You selected ${selection .value } `
101
- : ' Selection Cleared' ,
102
- )
97
+ onChange = { (selection ) =>
98
+ alert (selection ? ` You selected ${selection .value } ` : ' Selection Cleared' )
103
99
}
100
+ itemToString = { (item ) => (item ? item .value : ' ' )}
104
101
>
105
102
{ ({
106
103
getInputProps ,
@@ -115,7 +112,7 @@ wrapper element that contains the `<input>` and optionally the trigger button.
115
112
getRootProps ,
116
113
}) => (
117
114
<div >
118
- <label { ... getLabelProps ()} >Choose an element :</label >
115
+ <label { ... getLabelProps ()} >Enter a fruit :</label >
119
116
<div
120
117
style = { comboboxStyles }
121
118
{ ... getRootProps ({}, {suppressRefError: true })}
@@ -128,24 +125,21 @@ wrapper element that contains the `<input>` and optionally the trigger button.
128
125
<ul { ... getMenuProps ()} style = { menuStyles } >
129
126
{ isOpen
130
127
? items
131
- .filter (item => ! inputValue || item .includes (inputValue ))
128
+ .filter (( item ) => ! inputValue || item . value .includes (inputValue ))
132
129
.map ((item , index ) => (
133
130
<li
134
131
{ ... getItemProps ({
135
- key: item ,
132
+ key: item . value ,
136
133
index ,
137
134
item ,
138
135
style: {
139
136
backgroundColor:
140
- highlightedIndex === index
141
- ? ' lightgray'
142
- : ' white' ,
143
- fontWeight:
144
- selectedItem === item ? ' bold' : ' normal' ,
137
+ highlightedIndex === index ? ' lightgray' : ' white' ,
138
+ fontWeight: selectedItem === item ? ' bold' : ' normal' ,
145
139
},
146
140
})}
147
141
>
148
- { item }
142
+ { item . value }
149
143
</li >
150
144
))
151
145
: null }
@@ -170,20 +164,17 @@ was advertised in the past, we are still supporting it, but we strongly suggest
170
164
to move either to the structure with the ` getRootProps ` or even better to
171
165
` useCombobox ` .
172
166
173
- [ CodeSandbox] ( https://codesandbox.io/s/usecombobox-usage-evufg )
167
+ [ CodeSandbox for usage without getRootProps. ] [ code-sandbox-get-root-props ]
174
168
175
169
<Playground >
176
170
{ () => {
177
171
const DropdownCombobox = () => {
178
172
return (
179
173
<Downshift
180
- onChange = { selection =>
181
- alert (
182
- selection
183
- ? ` You selected ${selection .value } `
184
- : ' Selection Cleared' ,
185
- )
174
+ onChange = { (selection ) =>
175
+ alert (selection ? ` You selected ${selection .value } ` : ' Selection Cleared' )
186
176
}
177
+ itemToString = { (item ) => (item ? item .value : ' ' )}
187
178
>
188
179
{ ({
189
180
getInputProps ,
@@ -196,27 +187,30 @@ to move either to the structure with the `getRootProps` or even better to
196
187
selectedItem ,
197
188
isOpen ,
198
189
}) => (
199
- <div >
200
- <label { ... getLabelProps ()} >Choose an element :</label >
190
+ <div style = { comboboxStyles } >
191
+ <label { ... getLabelProps ()} >Enter a fruit :</label >
201
192
<input { ... getInputProps ()} />
202
193
<button { ... getToggleButtonProps ()} aria-label = { ' toggle menu' } >
203
194
↓
204
195
</button >
205
196
<ul { ... getMenuProps ()} style = { menuStyles } >
206
197
{ isOpen &&
207
198
items
208
- .filter (item => ! inputValue || item .includes (inputValue ))
199
+ .filter (( item ) => ! inputValue || item . value .includes (inputValue ))
209
200
.map ((item , index ) => (
210
201
<li
211
- style = {
212
- highlightedIndex === index
213
- ? {backgroundColor: ' #bde4ff' }
214
- : {}
215
- }
216
- key = { ` ${item }${index } ` }
217
- { ... getItemProps ({item , index })}
202
+ { ... getItemProps ({
203
+ key: ` ${item .value }${index } ` ,
204
+ item ,
205
+ index ,
206
+ style: {
207
+ backgroundColor:
208
+ highlightedIndex === index ? ' lightgray' : ' white' ,
209
+ fontWeight: selectedItem === item ? ' bold' : ' normal' ,
210
+ },
211
+ })}
218
212
>
219
- { item }
213
+ { item . value }
220
214
</li >
221
215
))}
222
216
</ul >
@@ -229,6 +223,13 @@ to move either to the structure with the `getRootProps` or even better to
229
223
}}
230
224
</Playground >
231
225
226
+ ## Other usage examples
227
+
228
+ To see more cool stuff you can build with ` Downshift ` , explore the [ examples repository] [ examples-code-sandbox ] .
229
+
232
230
[ combobox-aria ] : https://www.w3.org/TR/wai-aria-practices/examples/combobox/aria1.1pattern/listbox-combo.html
233
231
[ use-a-render-prop ] : https://cdb.reacttraining.com/use-a-render-prop-50de598f11ce
234
232
[ github-page ] : https://github.com/downshift-js/downshift
233
+ [ code-sandbox-get-root-props ] : https://codesandbox.io/s/github/kentcdodds/downshift-examples?file=/src/downshift/ordered-examples/01-basic-autocomplete.js
234
+ [ code-sandbox-no-get-root-props ] : https://codesandbox.io/s/github/kentcdodds/downshift-examples?file=/src/downshift/ordered-examples/00-get-root-props-example.js
235
+ [ examples-code-sandbox ] : https://codesandbox.io/s/github/kentcdodds/downshift-examples
0 commit comments