@@ -26,7 +26,6 @@ type: example
2626 const [isShowingOptions , setIsShowingOptions ] = useState (false )
2727 const [highlightedOptionId , setHighlightedOptionId ] = useState (null )
2828 const [selectedOptionId , setSelectedOptionId ] = useState (options[0 ].id )
29- const [announcement , setAnnouncement ] = useState (null )
3029 const inputRef = useRef ()
3130
3231 const focusInput = () => {
@@ -61,7 +60,6 @@ type: example
6160 setIsShowingOptions (false )
6261 setHighlightedOptionId (null )
6362 setSelectedOptionId (selectedOptionId ? option : ' ' )
64- setAnnouncement (' List collapsed.' )
6563 }
6664
6765 const handleBlur = (event ) => {
@@ -70,14 +68,8 @@ type: example
7068
7169 const handleHighlightOption = (event , { id }) => {
7270 event .persist ()
73- const optionsAvailable = ` ${ options .length } options available.`
74- const nowOpen = ! isShowingOptions
75- ? ` List expanded. ${ optionsAvailable} `
76- : ' '
77- const option = getOptionById (id)? .label
7871 setHighlightedOptionId (id)
7972 setInputValue (inputValue)
80- setAnnouncement (` ${ option} ${ nowOpen} ` )
8173 }
8274
8375 const handleSelectOption = (event , { id }) => {
@@ -86,7 +78,6 @@ type: example
8678 setSelectedOptionId (id)
8779 setInputValue (option)
8880 setIsShowingOptions (false )
89- setAnnouncement (` "${ option} " selected. List collapsed.` )
9081 }
9182
9283 return (
@@ -177,22 +168,6 @@ type: example
177168 return options .find (({ id }) => id === queryId)
178169 }
179170
180- const getOptionsChangedMessage = (newOptions ) => {
181- let message =
182- newOptions .length !== filteredOptions .length
183- ? ` ${ newOptions .length } options available.` // options changed, announce new total
184- : null // options haven't changed, don't announce
185- if (message && newOptions .length > 0 ) {
186- // options still available
187- if (highlightedOptionId !== newOptions[0 ].id ) {
188- // highlighted option hasn't been announced
189- const option = getOptionById (newOptions[0 ].id ).label
190- message = ` ${ option} . ${ message} `
191- }
192- }
193- return message
194- }
195-
196171 const filterOptions = (value ) => {
197172 return options .filter ((option ) =>
198173 option .label .toLowerCase ().startsWith (value .toLowerCase ())
@@ -231,9 +206,6 @@ type: example
231206
232207 const handleShowOptions = (event ) => {
233208 setIsShowingOptions (true )
234- setAnnouncement (
235- ` List expanded. ${ filteredOptions .length } options available.`
236- )
237209 if (inputValue || selectedOptionId || options .length === 0 ) return
238210
239211 if (' key' in event ) {
@@ -251,7 +223,6 @@ type: example
251223 const handleHideOptions = (event ) => {
252224 setIsShowingOptions (false )
253225 setHighlightedOptionId (false )
254- setAnnouncement (' List collapsed.' )
255226 matchValue ()
256227 }
257228
@@ -265,7 +236,6 @@ type: example
265236 if (! option) return // prevent highlighting of empty option
266237 setHighlightedOptionId (id)
267238 setInputValue (inputValue)
268- setAnnouncement (option .label )
269239 }
270240
271241 const handleSelectOption = (event , { id }) => {
@@ -276,7 +246,6 @@ type: example
276246 setInputValue (option .label )
277247 setIsShowingOptions (false )
278248 setFilteredOptions (options)
279- setAnnouncement (` ${ option .label } selected. List collapsed.` )
280249 }
281250
282251 const handleInputChange = (event ) => {
@@ -287,7 +256,7 @@ type: example
287256 setHighlightedOptionId (newOptions .length > 0 ? newOptions[0 ].id : null )
288257 setIsShowingOptions (true )
289258 setSelectedOptionId (value === ' ' ? null : selectedOptionId)
290- setAnnouncement (getOptionsChangedMessage ( newOptions) )
259+ setAnnouncement (` ${ newOptions . length } options available. ` )
291260 }
292261
293262 return (
@@ -335,6 +304,13 @@ type: example
335304 < / Select .Option >
336305 )}
337306 < / Select>
307+ < Alert
308+ liveRegion= {() => document .getElementById (' flash-messages' )}
309+ liveRegionPoliteness= " assertive"
310+ screenReaderOnly
311+ >
312+ {announcement}
313+ < / Alert>
338314 < / div>
339315 )
340316 }
@@ -378,7 +354,6 @@ type: example
378354 const [highlightedOptionId , setHighlightedOptionId ] = useState (null )
379355 const [selectedOptionId , setSelectedOptionId ] = useState ([' opt1' , ' opt6' ])
380356 const [filteredOptions , setFilteredOptions ] = useState (options)
381- const [announcement , setAnnouncement ] = useState (null )
382357 const inputRef = useRef ()
383358
384359 const focusInput = () => {
@@ -392,22 +367,6 @@ type: example
392367 return options .find (({ id }) => id === queryId)
393368 }
394369
395- const getOptionsChangedMessage = (newOptions ) => {
396- let message =
397- newOptions .length !== filteredOptions .length
398- ? ` ${ newOptions .length } options available.` // options changed, announce new total
399- : null // options haven't changed, don't announce
400- if (message && newOptions .length > 0 ) {
401- // options still available
402- if (highlightedOptionId !== newOptions[0 ].id ) {
403- // highlighted option hasn't been announced
404- const option = getOptionById (newOptions[0 ].id ).label
405- message = ` ${ option} . ${ message} `
406- }
407- }
408- return message
409- }
410-
411370 const filterOptions = (value ) => {
412371 return options .filter ((option ) =>
413372 option .label .toLowerCase ().startsWith (value .toLowerCase ())
@@ -475,7 +434,6 @@ type: example
475434 if (! option) return // prevent highlighting empty option
476435 setHighlightedOptionId (id)
477436 setInputValue (inputValue)
478- setAnnouncement (option .label )
479437 }
480438
481439 const handleSelectOption = (event , { id }) => {
@@ -487,7 +445,6 @@ type: example
487445 setFilteredOptions (filterOptions (' ' ))
488446 setInputValue (' ' )
489447 setIsShowingOptions (false )
490- setAnnouncement (` ${ option .label } selected. List collapsed.` )
491448 }
492449
493450 const handleInputChange = (event ) => {
@@ -497,7 +454,6 @@ type: example
497454 setFilteredOptions (newOptions)
498455 setHighlightedOptionId (newOptions .length > 0 ? newOptions[0 ].id : null )
499456 setIsShowingOptions (true )
500- setAnnouncement (getOptionsChangedMessage (newOptions))
501457 }
502458
503459 const handleKeyDown = (event ) => {
@@ -521,7 +477,6 @@ type: example
521477
522478 setSelectedOptionId (newSelection)
523479 setHighlightedOptionId (null )
524- setAnnouncement (` ${ getOptionById (tag).label } removed` )
525480
526481 inputRef .current .focus ()
527482 }
@@ -628,7 +583,6 @@ const GroupSelectExample = ({ options }) => {
628583 const [selectedOptionId , setSelectedOptionId ] = useState (
629584 options[' Western' ][0 ].id
630585 )
631- const [announcement , setAnnouncement ] = useState (null )
632586 const inputRef = useRef ()
633587
634588 const focusInput = () => {
@@ -654,15 +608,6 @@ const GroupSelectExample = ({ options }) => {
654608 return match
655609 }
656610
657- const getGroupChangedMessage = (newOption ) => {
658- const currentOption = getOptionById (highlightedOptionId)
659- const isNewGroup =
660- ! currentOption || currentOption .group !== newOption .group
661- let message = isNewGroup ? ` Group ${ newOption .group } entered. ` : ' '
662- message += newOption .label
663- return message
664- }
665-
666611 const handleShowOptions = (event ) => {
667612 setIsShowingOptions (true )
668613 setHighlightedOptionId (null )
@@ -694,18 +639,15 @@ const GroupSelectExample = ({ options }) => {
694639
695640 const handleHighlightOption = (event , { id }) => {
696641 event .persist ()
697- const newOption = getOptionById (id)
698642 setHighlightedOptionId (id)
699643 setInputValue (inputValue)
700- setAnnouncement (getGroupChangedMessage (newOption))
701644 }
702645
703646 const handleSelectOption = (event , { id }) => {
704647 focusInput ()
705648 setSelectedOptionId (id)
706649 setInputValue (getOptionById (id).label )
707650 setIsShowingOptions (false )
708- setAnnouncement (` ${ getOptionById (id).label } selected.` )
709651 }
710652
711653 const renderLabel = (text , variant ) => {
@@ -997,7 +939,6 @@ const AsyncExample = ({ options }) => {
997939 const [selectedOptionId , setSelectedOptionId ] = useState (null )
998940 const [selectedOptionLabel , setSelectedOptionLabel ] = useState (' ' )
999941 const [filteredOptions , setFilteredOptions ] = useState ([])
1000- const [announcement , setAnnouncement ] = useState (null )
1001942 const inputRef = useRef ()
1002943
1003944 const focusInput = () => {
@@ -1050,7 +991,6 @@ const AsyncExample = ({ options }) => {
1050991 const handleHideOptions = (event ) => {
1051992 setIsShowingOptions (false )
1052993 setHighlightedOptionId (null )
1053- setAnnouncement (' List collapsed.' )
1054994 matchValue ()
1055995 }
1056996
@@ -1065,7 +1005,6 @@ const AsyncExample = ({ options }) => {
10651005
10661006 setHighlightedOptionId (id)
10671007 setInputValue (inputValue)
1068- setAnnouncement (option .label )
10691008 }
10701009
10711010 const handleSelectOption = (event , { id }) => {
@@ -1076,7 +1015,6 @@ const AsyncExample = ({ options }) => {
10761015 setSelectedOptionLabel (option .label )
10771016 setInputValue (option .label )
10781017 setIsShowingOptions (false )
1079- setAnnouncement (` ${ option .label } selected. List collapsed.` )
10801018 setFilteredOptions ([getOptionById (id)])
10811019 }
10821020
@@ -1097,13 +1035,11 @@ const AsyncExample = ({ options }) => {
10971035 setIsShowingOptions (true )
10981036 setFilteredOptions ([])
10991037 setHighlightedOptionId (null )
1100- setAnnouncement (' Loading options.' )
11011038
11021039 timeoutId = setTimeout (() => {
11031040 const newOptions = filterOptions (value)
11041041 setFilteredOptions (newOptions)
11051042 setIsLoading (false )
1106- setAnnouncement (` ${ newOptions .length } options available.` )
11071043 }, 1500 )
11081044 }
11091045 }
@@ -1196,7 +1132,6 @@ const SingleSelectExample = ({ options }) => {
11961132 const [isShowingOptions , setIsShowingOptions ] = useState (false )
11971133 const [highlightedOptionId , setHighlightedOptionId ] = useState (null )
11981134 const [selectedOptionId , setSelectedOptionId ] = useState (options[0 ].id )
1199- const [announcement , setAnnouncement ] = useState (null )
12001135 const inputRef = useRef ()
12011136
12021137 const focusInput = () => {
@@ -1232,7 +1167,6 @@ const SingleSelectExample = ({ options }) => {
12321167 setIsShowingOptions (false )
12331168 setHighlightedOptionId (null )
12341169 setInputValue (selectedOptionId ? option : ' ' )
1235- setAnnouncement (' List collapsed.' )
12361170 }
12371171
12381172 const handleBlur = (event ) => {
@@ -1241,14 +1175,8 @@ const SingleSelectExample = ({ options }) => {
12411175
12421176 const handleHighlightOption = (event , { id }) => {
12431177 event .persist ()
1244- const optionsAvailable = ` ${ options .length } options available.`
1245- const nowOpen = ! isShowingOptions
1246- ? ` List expanded. ${ optionsAvailable} `
1247- : ' '
1248- const option = getOptionById (id).label
12491178 setHighlightedOptionId (id)
12501179 setInputValue (inputValue)
1251- setAnnouncement (` ${ option} ${ nowOpen} ` )
12521180 }
12531181
12541182 const handleSelectOption = (event , { id }) => {
@@ -1257,7 +1185,6 @@ const SingleSelectExample = ({ options }) => {
12571185 setSelectedOptionId (id)
12581186 setInputValue (option)
12591187 setIsShowingOptions (false )
1260- setAnnouncement (` "${ option} " selected. List collapsed.` )
12611188 }
12621189
12631190 return (
0 commit comments