@@ -114,7 +114,7 @@ function Example() {
114114
115115 return (
116116 <Select >
117- <Label >Add tag... </Label >
117+ <Label >Category </Label >
118118 <Button >
119119 <SelectValue />
120120 <ChevronDown size = { 18 } />
@@ -142,6 +142,124 @@ function Example() {
142142}
143143```
144144
145+ ### TagGroup
146+
147+ Use the ` SelectValue ` render prop function to display the selected items as a [ TagGroup] ( TagGroup.html ) .
148+
149+ ``` tsx render files={['packages/dev/s2-docs/pages/react-aria/MultiSelect.css']}
150+ " use client" ;
151+ import {Autocomplete , Select , SelectValue , Group , useFilter } from ' react-aria-components' ;
152+ import {Button } from ' vanilla-starter/Button' ;
153+ import {ListBox , DropdownItem } from ' vanilla-starter/ListBox' ;
154+ import {Label } from ' vanilla-starter/Form' ;
155+ import {Popover } from ' vanilla-starter/Popover' ;
156+ import {Plus } from ' lucide-react' ;
157+ import {SearchField } from ' vanilla-starter/SearchField' ;
158+ import {Tag , TagGroup } from ' vanilla-starter/TagGroup' ;
159+ import {useRef } from ' react' ;
160+ import ' ./MultiSelect.css' ;
161+
162+ /* - begin collapse -*/
163+ const states = [
164+ {id: ' AL' , name: ' Alabama' },
165+ {id: ' AK' , name: ' Alaska' },
166+ {id: ' AZ' , name: ' Arizona' },
167+ {id: ' AR' , name: ' Arkansas' },
168+ {id: ' CA' , name: ' California' },
169+ {id: ' CO' , name: ' Colorado' },
170+ {id: ' CT' , name: ' Connecticut' },
171+ {id: ' DE' , name: ' Delaware' },
172+ {id: ' DC' , name: ' District of Columbia' },
173+ {id: ' FL' , name: ' Florida' },
174+ {id: ' GA' , name: ' Georgia' },
175+ {id: ' HI' , name: ' Hawaii' },
176+ {id: ' ID' , name: ' Idaho' },
177+ {id: ' IL' , name: ' Illinois' },
178+ {id: ' IN' , name: ' Indiana' },
179+ {id: ' IA' , name: ' Iowa' },
180+ {id: ' KS' , name: ' Kansas' },
181+ {id: ' KY' , name: ' Kentucky' },
182+ {id: ' LA' , name: ' Louisiana' },
183+ {id: ' ME' , name: ' Maine' },
184+ {id: ' MD' , name: ' Maryland' },
185+ {id: ' MA' , name: ' Massachusetts' },
186+ {id: ' MI' , name: ' Michigan' },
187+ {id: ' MN' , name: ' Minnesota' },
188+ {id: ' MS' , name: ' Mississippi' },
189+ {id: ' MO' , name: ' Missouri' },
190+ {id: ' MT' , name: ' Montana' },
191+ {id: ' NE' , name: ' Nebraska' },
192+ {id: ' NV' , name: ' Nevada' },
193+ {id: ' NH' , name: ' New Hampshire' },
194+ {id: ' NJ' , name: ' New Jersey' },
195+ {id: ' NM' , name: ' New Mexico' },
196+ {id: ' NY' , name: ' New York' },
197+ {id: ' NC' , name: ' North Carolina' },
198+ {id: ' ND' , name: ' North Dakota' },
199+ {id: ' OH' , name: ' Ohio' },
200+ {id: ' OK' , name: ' Oklahoma' },
201+ {id: ' OR' , name: ' Oregon' },
202+ {id: ' PA' , name: ' Pennsylvania' },
203+ {id: ' RI' , name: ' Rhode Island' },
204+ {id: ' SC' , name: ' South Carolina' },
205+ {id: ' SD' , name: ' South Dakota' },
206+ {id: ' TN' , name: ' Tennessee' },
207+ {id: ' TX' , name: ' Texas' },
208+ {id: ' UT' , name: ' Utah' },
209+ {id: ' VT' , name: ' Vermont' },
210+ {id: ' VA' , name: ' Virginia' },
211+ {id: ' WA' , name: ' Washington' },
212+ {id: ' WV' , name: ' West Virginia' },
213+ {id: ' WI' , name: ' Wisconsin' },
214+ {id: ' WY' , name: ' Wyoming' }
215+ ];
216+ /* - end collapse -*/
217+
218+ function SelectWithTagGroup() {
219+ let triggerRef = useRef <HTMLDivElement | null >(null );
220+ let {contains} = useFilter ({sensitivity: ' base' });
221+
222+ return (
223+ <Select selectionMode = " multiple" className = " multi-select" >
224+ <Label >States</Label >
225+ <Group aria-label = " States" ref = { triggerRef } >
226+ { /* - begin highlight -*/ }
227+ <SelectValue <typeof states [0 ]> style = { {flex: 1 }} >
228+ { ({selectedItems , state }) => (
229+ <TagGroup
230+ aria-label = " Selected states"
231+ items = { selectedItems .filter (item => item != null )}
232+ renderEmptyState = { () => ' No selected items' }
233+ onRemove = { (keys ) => {
234+ // Remove keys from Select state.
235+ if (Array .isArray (state .value )) {
236+ state .setValue (state .value .filter (k => ! keys .has (k )));
237+ }
238+ }} >
239+ { item => <Tag >{ item .name } </Tag >}
240+ </TagGroup >
241+ )}
242+ </SelectValue >
243+ { /* - end highlight -*/ }
244+ <Button variant = " primary" ><Plus /></Button >
245+ </Group >
246+ <Popover
247+ // Position popover relative to the wrapping div instead of the Button
248+ triggerRef = { triggerRef }
249+ hideArrow
250+ style = { {display: ' flex' , flexDirection: ' column' , width: 250 , padding: 4 }} >
251+ <Autocomplete filter = { contains } >
252+ <SearchField aria-label = " Search states" placeholder = " Search states" autoFocus style = { {marginBottom: 4 }} />
253+ <ListBox items = { states } >
254+ { state => <DropdownItem >{ state .name } </DropdownItem >}
255+ </ListBox >
256+ </Autocomplete >
257+ </Popover >
258+ </Select >
259+ );
260+ }
261+ ```
262+
145263## Value
146264
147265Use the ` defaultValue ` or ` value ` prop to set the selected item. The value corresponds to the ` id ` prop of an item. When ` selectionMode="multiple" ` , ` value ` and ` onChange ` accept an array. Items can be disabled with the ` isDisabled ` prop.
0 commit comments