@react-aria/combobox 3.14.2 → 3.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/dist/import.mjs +2 -2
  2. package/dist/main.js +2 -2
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +2 -2
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +2 -0
  7. package/package.json +15 -26
  8. package/src/index.ts +3 -3
  9. package/dist/ar-AE.main.js +0 -22
  10. package/dist/ar-AE.main.js.map +0 -1
  11. package/dist/ar-AE.mjs +0 -24
  12. package/dist/ar-AE.module.js +0 -24
  13. package/dist/ar-AE.module.js.map +0 -1
  14. package/dist/bg-BG.main.js +0 -22
  15. package/dist/bg-BG.main.js.map +0 -1
  16. package/dist/bg-BG.mjs +0 -24
  17. package/dist/bg-BG.module.js +0 -24
  18. package/dist/bg-BG.module.js.map +0 -1
  19. package/dist/cs-CZ.main.js +0 -22
  20. package/dist/cs-CZ.main.js.map +0 -1
  21. package/dist/cs-CZ.mjs +0 -24
  22. package/dist/cs-CZ.module.js +0 -24
  23. package/dist/cs-CZ.module.js.map +0 -1
  24. package/dist/da-DK.main.js +0 -22
  25. package/dist/da-DK.main.js.map +0 -1
  26. package/dist/da-DK.mjs +0 -24
  27. package/dist/da-DK.module.js +0 -24
  28. package/dist/da-DK.module.js.map +0 -1
  29. package/dist/de-DE.main.js +0 -22
  30. package/dist/de-DE.main.js.map +0 -1
  31. package/dist/de-DE.mjs +0 -24
  32. package/dist/de-DE.module.js +0 -24
  33. package/dist/de-DE.module.js.map +0 -1
  34. package/dist/el-GR.main.js +0 -22
  35. package/dist/el-GR.main.js.map +0 -1
  36. package/dist/el-GR.mjs +0 -24
  37. package/dist/el-GR.module.js +0 -24
  38. package/dist/el-GR.module.js.map +0 -1
  39. package/dist/en-US.main.js +0 -22
  40. package/dist/en-US.main.js.map +0 -1
  41. package/dist/en-US.mjs +0 -24
  42. package/dist/en-US.module.js +0 -24
  43. package/dist/en-US.module.js.map +0 -1
  44. package/dist/es-ES.main.js +0 -22
  45. package/dist/es-ES.main.js.map +0 -1
  46. package/dist/es-ES.mjs +0 -24
  47. package/dist/es-ES.module.js +0 -24
  48. package/dist/es-ES.module.js.map +0 -1
  49. package/dist/et-EE.main.js +0 -22
  50. package/dist/et-EE.main.js.map +0 -1
  51. package/dist/et-EE.mjs +0 -24
  52. package/dist/et-EE.module.js +0 -24
  53. package/dist/et-EE.module.js.map +0 -1
  54. package/dist/fi-FI.main.js +0 -22
  55. package/dist/fi-FI.main.js.map +0 -1
  56. package/dist/fi-FI.mjs +0 -24
  57. package/dist/fi-FI.module.js +0 -24
  58. package/dist/fi-FI.module.js.map +0 -1
  59. package/dist/fr-FR.main.js +0 -22
  60. package/dist/fr-FR.main.js.map +0 -1
  61. package/dist/fr-FR.mjs +0 -24
  62. package/dist/fr-FR.module.js +0 -24
  63. package/dist/fr-FR.module.js.map +0 -1
  64. package/dist/he-IL.main.js +0 -22
  65. package/dist/he-IL.main.js.map +0 -1
  66. package/dist/he-IL.mjs +0 -24
  67. package/dist/he-IL.module.js +0 -24
  68. package/dist/he-IL.module.js.map +0 -1
  69. package/dist/hr-HR.main.js +0 -22
  70. package/dist/hr-HR.main.js.map +0 -1
  71. package/dist/hr-HR.mjs +0 -24
  72. package/dist/hr-HR.module.js +0 -24
  73. package/dist/hr-HR.module.js.map +0 -1
  74. package/dist/hu-HU.main.js +0 -22
  75. package/dist/hu-HU.main.js.map +0 -1
  76. package/dist/hu-HU.mjs +0 -24
  77. package/dist/hu-HU.module.js +0 -24
  78. package/dist/hu-HU.module.js.map +0 -1
  79. package/dist/intlStrings.main.js +0 -108
  80. package/dist/intlStrings.main.js.map +0 -1
  81. package/dist/intlStrings.mjs +0 -110
  82. package/dist/intlStrings.module.js +0 -110
  83. package/dist/intlStrings.module.js.map +0 -1
  84. package/dist/it-IT.main.js +0 -22
  85. package/dist/it-IT.main.js.map +0 -1
  86. package/dist/it-IT.mjs +0 -24
  87. package/dist/it-IT.module.js +0 -24
  88. package/dist/it-IT.module.js.map +0 -1
  89. package/dist/ja-JP.main.js +0 -22
  90. package/dist/ja-JP.main.js.map +0 -1
  91. package/dist/ja-JP.mjs +0 -24
  92. package/dist/ja-JP.module.js +0 -24
  93. package/dist/ja-JP.module.js.map +0 -1
  94. package/dist/ko-KR.main.js +0 -22
  95. package/dist/ko-KR.main.js.map +0 -1
  96. package/dist/ko-KR.mjs +0 -24
  97. package/dist/ko-KR.module.js +0 -24
  98. package/dist/ko-KR.module.js.map +0 -1
  99. package/dist/lt-LT.main.js +0 -22
  100. package/dist/lt-LT.main.js.map +0 -1
  101. package/dist/lt-LT.mjs +0 -24
  102. package/dist/lt-LT.module.js +0 -24
  103. package/dist/lt-LT.module.js.map +0 -1
  104. package/dist/lv-LV.main.js +0 -22
  105. package/dist/lv-LV.main.js.map +0 -1
  106. package/dist/lv-LV.mjs +0 -24
  107. package/dist/lv-LV.module.js +0 -24
  108. package/dist/lv-LV.module.js.map +0 -1
  109. package/dist/nb-NO.main.js +0 -22
  110. package/dist/nb-NO.main.js.map +0 -1
  111. package/dist/nb-NO.mjs +0 -24
  112. package/dist/nb-NO.module.js +0 -24
  113. package/dist/nb-NO.module.js.map +0 -1
  114. package/dist/nl-NL.main.js +0 -22
  115. package/dist/nl-NL.main.js.map +0 -1
  116. package/dist/nl-NL.mjs +0 -24
  117. package/dist/nl-NL.module.js +0 -24
  118. package/dist/nl-NL.module.js.map +0 -1
  119. package/dist/pl-PL.main.js +0 -22
  120. package/dist/pl-PL.main.js.map +0 -1
  121. package/dist/pl-PL.mjs +0 -24
  122. package/dist/pl-PL.module.js +0 -24
  123. package/dist/pl-PL.module.js.map +0 -1
  124. package/dist/pt-BR.main.js +0 -22
  125. package/dist/pt-BR.main.js.map +0 -1
  126. package/dist/pt-BR.mjs +0 -24
  127. package/dist/pt-BR.module.js +0 -24
  128. package/dist/pt-BR.module.js.map +0 -1
  129. package/dist/pt-PT.main.js +0 -22
  130. package/dist/pt-PT.main.js.map +0 -1
  131. package/dist/pt-PT.mjs +0 -24
  132. package/dist/pt-PT.module.js +0 -24
  133. package/dist/pt-PT.module.js.map +0 -1
  134. package/dist/ro-RO.main.js +0 -22
  135. package/dist/ro-RO.main.js.map +0 -1
  136. package/dist/ro-RO.mjs +0 -24
  137. package/dist/ro-RO.module.js +0 -24
  138. package/dist/ro-RO.module.js.map +0 -1
  139. package/dist/ru-RU.main.js +0 -22
  140. package/dist/ru-RU.main.js.map +0 -1
  141. package/dist/ru-RU.mjs +0 -24
  142. package/dist/ru-RU.module.js +0 -24
  143. package/dist/ru-RU.module.js.map +0 -1
  144. package/dist/sk-SK.main.js +0 -22
  145. package/dist/sk-SK.main.js.map +0 -1
  146. package/dist/sk-SK.mjs +0 -24
  147. package/dist/sk-SK.module.js +0 -24
  148. package/dist/sk-SK.module.js.map +0 -1
  149. package/dist/sl-SI.main.js +0 -22
  150. package/dist/sl-SI.main.js.map +0 -1
  151. package/dist/sl-SI.mjs +0 -24
  152. package/dist/sl-SI.module.js +0 -24
  153. package/dist/sl-SI.module.js.map +0 -1
  154. package/dist/sr-SP.main.js +0 -22
  155. package/dist/sr-SP.main.js.map +0 -1
  156. package/dist/sr-SP.mjs +0 -24
  157. package/dist/sr-SP.module.js +0 -24
  158. package/dist/sr-SP.module.js.map +0 -1
  159. package/dist/sv-SE.main.js +0 -22
  160. package/dist/sv-SE.main.js.map +0 -1
  161. package/dist/sv-SE.mjs +0 -24
  162. package/dist/sv-SE.module.js +0 -24
  163. package/dist/sv-SE.module.js.map +0 -1
  164. package/dist/tr-TR.main.js +0 -22
  165. package/dist/tr-TR.main.js.map +0 -1
  166. package/dist/tr-TR.mjs +0 -24
  167. package/dist/tr-TR.module.js +0 -24
  168. package/dist/tr-TR.module.js.map +0 -1
  169. package/dist/types.d.ts +0 -48
  170. package/dist/types.d.ts.map +0 -1
  171. package/dist/uk-UA.main.js +0 -22
  172. package/dist/uk-UA.main.js.map +0 -1
  173. package/dist/uk-UA.mjs +0 -24
  174. package/dist/uk-UA.module.js +0 -24
  175. package/dist/uk-UA.module.js.map +0 -1
  176. package/dist/useComboBox.main.js +0 -326
  177. package/dist/useComboBox.main.js.map +0 -1
  178. package/dist/useComboBox.mjs +0 -321
  179. package/dist/useComboBox.module.js +0 -321
  180. package/dist/useComboBox.module.js.map +0 -1
  181. package/dist/zh-CN.main.js +0 -22
  182. package/dist/zh-CN.main.js.map +0 -1
  183. package/dist/zh-CN.mjs +0 -24
  184. package/dist/zh-CN.module.js +0 -24
  185. package/dist/zh-CN.module.js.map +0 -1
  186. package/dist/zh-TW.main.js +0 -22
  187. package/dist/zh-TW.main.js.map +0 -1
  188. package/dist/zh-TW.mjs +0 -24
  189. package/dist/zh-TW.module.js +0 -24
  190. package/dist/zh-TW.module.js.map +0 -1
  191. package/src/useComboBox.ts +0 -401
@@ -1,401 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {announce} from '@react-aria/live-announcer';
14
- import {AriaButtonProps} from '@react-types/button';
15
- import {AriaComboBoxProps} from '@react-types/combobox';
16
- import {ariaHideOutside} from '@react-aria/overlays';
17
- import {AriaListBoxOptions, getItemId, listData} from '@react-aria/listbox';
18
- import {BaseEvent, DOMAttributes, KeyboardDelegate, LayoutDelegate, PressEvent, RefObject, RouterOptions, ValidationResult} from '@react-types/shared';
19
- import {chain, getActiveElement, getOwnerDocument, isAppleDevice, mergeProps, nodeContains, useEvent, useFormReset, useLabels, useRouter, useUpdateEffect} from '@react-aria/utils';
20
- import {ComboBoxState} from '@react-stately/combobox';
21
- import {dispatchVirtualFocus} from '@react-aria/focus';
22
- import {FocusEvent, InputHTMLAttributes, KeyboardEvent, TouchEvent, useEffect, useMemo, useRef} from 'react';
23
- import {getChildNodes, getItemCount} from '@react-stately/collections';
24
- // @ts-ignore
25
- import intlMessages from '../intl/*.json';
26
- import {ListKeyboardDelegate, useSelectableCollection} from '@react-aria/selection';
27
- import {privateValidationStateProp} from '@react-stately/form';
28
- import {useLocalizedStringFormatter} from '@react-aria/i18n';
29
- import {useMenuTrigger} from '@react-aria/menu';
30
- import {useTextField} from '@react-aria/textfield';
31
-
32
- export interface AriaComboBoxOptions<T> extends Omit<AriaComboBoxProps<T>, 'children'> {
33
- /** The ref for the input element. */
34
- inputRef: RefObject<HTMLInputElement | null>,
35
- /** The ref for the list box popover. */
36
- popoverRef: RefObject<Element | null>,
37
- /** The ref for the list box. */
38
- listBoxRef: RefObject<HTMLElement | null>,
39
- /** The ref for the optional list box popup trigger button. */
40
- buttonRef?: RefObject<Element | null>,
41
- /** An optional keyboard delegate implementation, to override the default. */
42
- keyboardDelegate?: KeyboardDelegate,
43
- /**
44
- * A delegate object that provides layout information for items in the collection.
45
- * By default this uses the DOM, but this can be overridden to implement things like
46
- * virtualized scrolling.
47
- */
48
- layoutDelegate?: LayoutDelegate
49
- }
50
-
51
- export interface ComboBoxAria<T> extends ValidationResult {
52
- /** Props for the label element. */
53
- labelProps: DOMAttributes,
54
- /** Props for the combo box input element. */
55
- inputProps: InputHTMLAttributes<HTMLInputElement>,
56
- /** Props for the list box, to be passed to `useListBox`. */
57
- listBoxProps: AriaListBoxOptions<T>,
58
- /** Props for the optional trigger button, to be passed to `useButton`. */
59
- buttonProps: AriaButtonProps,
60
- /** Props for the combo box description element, if any. */
61
- descriptionProps: DOMAttributes,
62
- /** Props for the combo box error message element, if any. */
63
- errorMessageProps: DOMAttributes
64
- }
65
-
66
- /**
67
- * Provides the behavior and accessibility implementation for a combo box component.
68
- * A combo box combines a text input with a listbox, allowing users to filter a list of options to items matching a query.
69
- * @param props - Props for the combo box.
70
- * @param state - State for the select, as returned by `useComboBoxState`.
71
- */
72
- export function useComboBox<T>(props: AriaComboBoxOptions<T>, state: ComboBoxState<T>): ComboBoxAria<T> {
73
- let {
74
- buttonRef,
75
- popoverRef,
76
- inputRef,
77
- listBoxRef,
78
- keyboardDelegate,
79
- layoutDelegate,
80
- // completionMode = 'suggest',
81
- shouldFocusWrap,
82
- isReadOnly,
83
- isDisabled
84
- } = props;
85
- let backupBtnRef = useRef(null);
86
- buttonRef = buttonRef ?? backupBtnRef;
87
-
88
- let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/combobox');
89
- let {menuTriggerProps, menuProps} = useMenuTrigger<T>(
90
- {
91
- type: 'listbox',
92
- isDisabled: isDisabled || isReadOnly
93
- },
94
- state,
95
- buttonRef
96
- );
97
-
98
- // Set listbox id so it can be used when calling getItemId later
99
- listData.set(state, {id: menuProps.id});
100
-
101
- // By default, a KeyboardDelegate is provided which uses the DOM to query layout information (e.g. for page up/page down).
102
- // When virtualized, the layout object will be passed in as a prop and override this.
103
- let {collection} = state;
104
- let {disabledKeys} = state.selectionManager;
105
- let delegate = useMemo(() => (
106
- keyboardDelegate || new ListKeyboardDelegate({
107
- collection,
108
- disabledKeys,
109
- ref: listBoxRef,
110
- layoutDelegate
111
- })
112
- ), [keyboardDelegate, layoutDelegate, collection, disabledKeys, listBoxRef]);
113
-
114
- // Use useSelectableCollection to get the keyboard handlers to apply to the textfield
115
- let {collectionProps} = useSelectableCollection({
116
- selectionManager: state.selectionManager,
117
- keyboardDelegate: delegate,
118
- disallowTypeAhead: true,
119
- disallowEmptySelection: true,
120
- shouldFocusWrap,
121
- ref: inputRef,
122
- // Prevent item scroll behavior from being applied here, should be handled in the user's Popover + ListBox component
123
- isVirtualized: true
124
- });
125
-
126
- let router = useRouter();
127
-
128
- // For textfield specific keydown operations
129
- let onKeyDown = (e: BaseEvent<KeyboardEvent<any>>) => {
130
- if (e.nativeEvent.isComposing) {
131
- return;
132
- }
133
- switch (e.key) {
134
- case 'Enter':
135
- case 'Tab':
136
- // Prevent form submission if menu is open since we may be selecting a option
137
- if (state.isOpen && e.key === 'Enter') {
138
- e.preventDefault();
139
- }
140
-
141
- // If the focused item is a link, trigger opening it. Items that are links are not selectable.
142
- if (state.isOpen && listBoxRef.current && state.selectionManager.focusedKey != null) {
143
- let collectionItem = state.collection.getItem(state.selectionManager.focusedKey);
144
- if (collectionItem?.props.href) {
145
- let item = listBoxRef.current.querySelector(`[data-key="${CSS.escape(state.selectionManager.focusedKey.toString())}"]`);
146
- if (e.key === 'Enter' && item instanceof HTMLAnchorElement) {
147
- router.open(item, e, collectionItem.props.href, collectionItem.props.routerOptions as RouterOptions);
148
- }
149
- state.close();
150
- break;
151
- } else if (collectionItem?.props.onAction) {
152
- collectionItem.props.onAction();
153
- state.close();
154
- break;
155
- }
156
- }
157
- state.commit();
158
- break;
159
- case 'Escape':
160
- if (
161
- state.selectedKey !== null ||
162
- state.inputValue === '' ||
163
- props.allowsCustomValue
164
- ) {
165
- e.continuePropagation();
166
- }
167
- state.revert();
168
- break;
169
- case 'ArrowDown':
170
- state.open('first', 'manual');
171
- break;
172
- case 'ArrowUp':
173
- state.open('last', 'manual');
174
- break;
175
- case 'ArrowLeft':
176
- case 'ArrowRight':
177
- state.selectionManager.setFocusedKey(null);
178
- break;
179
- }
180
- };
181
-
182
- let onBlur = (e: FocusEvent<HTMLInputElement>) => {
183
- let blurFromButton = buttonRef?.current && buttonRef.current === e.relatedTarget;
184
- let blurIntoPopover = nodeContains(popoverRef.current, e.relatedTarget);
185
- // Ignore blur if focused moved to the button(if exists) or into the popover.
186
- if (blurFromButton || blurIntoPopover) {
187
- return;
188
- }
189
-
190
- if (props.onBlur) {
191
- props.onBlur(e);
192
- }
193
-
194
- state.setFocused(false);
195
- };
196
-
197
- let onFocus = (e: FocusEvent<HTMLInputElement>) => {
198
- if (state.isFocused) {
199
- return;
200
- }
201
-
202
- if (props.onFocus) {
203
- props.onFocus(e);
204
- }
205
-
206
- state.setFocused(true);
207
- };
208
-
209
- let {isInvalid, validationErrors, validationDetails} = state.displayValidation;
210
- let {labelProps, inputProps, descriptionProps, errorMessageProps} = useTextField({
211
- ...props,
212
- onChange: state.setInputValue,
213
- onKeyDown: !isReadOnly ? chain(state.isOpen && collectionProps.onKeyDown, onKeyDown, props.onKeyDown) : props.onKeyDown,
214
- onBlur,
215
- value: state.inputValue,
216
- defaultValue: state.defaultInputValue,
217
- onFocus,
218
- autoComplete: 'off',
219
- validate: undefined,
220
- [privateValidationStateProp]: state
221
- }, inputRef);
222
-
223
- useFormReset(inputRef, state.defaultSelectedKey, state.setSelectedKey);
224
-
225
- // Press handlers for the ComboBox button
226
- let onPress = (e: PressEvent) => {
227
- if (e.pointerType === 'touch') {
228
- // Focus the input field in case it isn't focused yet
229
- inputRef.current?.focus();
230
- state.toggle(null, 'manual');
231
- }
232
- };
233
-
234
- let onPressStart = (e: PressEvent) => {
235
- if (e.pointerType !== 'touch') {
236
- inputRef.current?.focus();
237
- state.toggle((e.pointerType === 'keyboard' || e.pointerType === 'virtual') ? 'first' : null, 'manual');
238
- }
239
- };
240
-
241
- let triggerLabelProps = useLabels({
242
- id: menuTriggerProps.id,
243
- 'aria-label': stringFormatter.format('buttonLabel'),
244
- 'aria-labelledby': props['aria-labelledby'] || labelProps.id
245
- });
246
-
247
- let listBoxProps = useLabels({
248
- id: menuProps.id,
249
- 'aria-label': stringFormatter.format('listboxLabel'),
250
- 'aria-labelledby': props['aria-labelledby'] || labelProps.id
251
- });
252
-
253
- // If a touch happens on direct center of ComboBox input, might be virtual click from iPad so open ComboBox menu
254
- let lastEventTime = useRef(0);
255
- let onTouchEnd = (e: TouchEvent) => {
256
- if (isDisabled || isReadOnly) {
257
- return;
258
- }
259
-
260
- // Sometimes VoiceOver on iOS fires two touchend events in quick succession. Ignore the second one.
261
- if (e.timeStamp - lastEventTime.current < 500) {
262
- e.preventDefault();
263
- inputRef.current?.focus();
264
- return;
265
- }
266
-
267
- let rect = (e.target as Element).getBoundingClientRect();
268
- let touch = e.changedTouches[0];
269
-
270
- let centerX = Math.ceil(rect.left + .5 * rect.width);
271
- let centerY = Math.ceil(rect.top + .5 * rect.height);
272
-
273
- if (touch.clientX === centerX && touch.clientY === centerY) {
274
- e.preventDefault();
275
- inputRef.current?.focus();
276
- state.toggle(null, 'manual');
277
-
278
- lastEventTime.current = e.timeStamp;
279
- }
280
- };
281
-
282
- // VoiceOver has issues with announcing aria-activedescendant properly on change
283
- // (especially on iOS). We use a live region announcer to announce focus changes
284
- // manually. In addition, section titles are announced when navigating into a new section.
285
- let focusedItem = state.selectionManager.focusedKey != null && state.isOpen
286
- ? state.collection.getItem(state.selectionManager.focusedKey)
287
- : undefined;
288
- let sectionKey = focusedItem?.parentKey ?? null;
289
- let itemKey = state.selectionManager.focusedKey ?? null;
290
- let lastSection = useRef(sectionKey);
291
- let lastItem = useRef(itemKey);
292
- useEffect(() => {
293
- if (isAppleDevice() && focusedItem != null && itemKey != null && itemKey !== lastItem.current) {
294
- let isSelected = state.selectionManager.isSelected(itemKey);
295
- let section = sectionKey != null ? state.collection.getItem(sectionKey) : null;
296
- let sectionTitle = section?.['aria-label'] || (typeof section?.rendered === 'string' ? section.rendered : '') || '';
297
-
298
- let announcement = stringFormatter.format('focusAnnouncement', {
299
- isGroupChange: (section && sectionKey !== lastSection.current) ?? false,
300
- groupTitle: sectionTitle,
301
- groupCount: section ? [...getChildNodes(section, state.collection)].length : 0,
302
- optionText: focusedItem['aria-label'] || focusedItem.textValue || '',
303
- isSelected
304
- });
305
-
306
- announce(announcement);
307
- }
308
-
309
- lastSection.current = sectionKey;
310
- lastItem.current = itemKey;
311
- });
312
-
313
- // Announce the number of available suggestions when it changes
314
- let optionCount = getItemCount(state.collection);
315
- let lastSize = useRef(optionCount);
316
- let lastOpen = useRef(state.isOpen);
317
- useEffect(() => {
318
- // Only announce the number of options available when the menu opens if there is no
319
- // focused item, otherwise screen readers will typically read e.g. "1 of 6".
320
- // The exception is VoiceOver since this isn't included in the message above.
321
- let didOpenWithoutFocusedItem =
322
- state.isOpen !== lastOpen.current &&
323
- (state.selectionManager.focusedKey == null || isAppleDevice());
324
-
325
- if (state.isOpen && (didOpenWithoutFocusedItem || optionCount !== lastSize.current)) {
326
- let announcement = stringFormatter.format('countAnnouncement', {optionCount});
327
- announce(announcement);
328
- }
329
-
330
- lastSize.current = optionCount;
331
- lastOpen.current = state.isOpen;
332
- });
333
-
334
- // Announce when a selection occurs for VoiceOver. Other screen readers typically do this automatically.
335
- let lastSelectedKey = useRef(state.selectedKey);
336
- useEffect(() => {
337
- if (isAppleDevice() && state.isFocused && state.selectedItem && state.selectedKey !== lastSelectedKey.current) {
338
- let optionText = state.selectedItem['aria-label'] || state.selectedItem.textValue || '';
339
- let announcement = stringFormatter.format('selectedAnnouncement', {optionText});
340
- announce(announcement);
341
- }
342
-
343
- lastSelectedKey.current = state.selectedKey;
344
- });
345
-
346
- useEffect(() => {
347
- if (state.isOpen) {
348
- return ariaHideOutside([inputRef.current, popoverRef.current].filter(element => element != null));
349
- }
350
- }, [state.isOpen, inputRef, popoverRef]);
351
-
352
- useUpdateEffect(() => {
353
- // Re-show focus ring when there is no virtually focused item.
354
- if (!focusedItem && inputRef.current && getActiveElement(getOwnerDocument(inputRef.current)) === inputRef.current) {
355
- dispatchVirtualFocus(inputRef.current, null);
356
- }
357
- }, [focusedItem]);
358
-
359
- useEvent(listBoxRef, 'react-aria-item-action', state.isOpen ? () => {
360
- state.close();
361
- } : undefined);
362
-
363
- return {
364
- labelProps,
365
- buttonProps: {
366
- ...menuTriggerProps,
367
- ...triggerLabelProps,
368
- excludeFromTabOrder: true,
369
- preventFocusOnPress: true,
370
- onPress,
371
- onPressStart,
372
- isDisabled: isDisabled || isReadOnly
373
- },
374
- inputProps: mergeProps(inputProps, {
375
- role: 'combobox',
376
- 'aria-expanded': menuTriggerProps['aria-expanded'],
377
- 'aria-controls': state.isOpen ? menuProps.id : undefined,
378
- // TODO: readd proper logic for completionMode = complete (aria-autocomplete: both)
379
- 'aria-autocomplete': 'list',
380
- 'aria-activedescendant': focusedItem ? getItemId(state, focusedItem.key) : undefined,
381
- onTouchEnd,
382
- // This disable's iOS's autocorrect suggestions, since the combo box provides its own suggestions.
383
- autoCorrect: 'off',
384
- // This disable's the macOS Safari spell check auto corrections.
385
- spellCheck: 'false'
386
- }),
387
- listBoxProps: mergeProps(menuProps, listBoxProps, {
388
- autoFocus: state.focusStrategy || true,
389
- shouldUseVirtualFocus: true,
390
- shouldSelectOnPressUp: true,
391
- shouldFocusOnHover: true,
392
- linkBehavior: 'selection' as const,
393
- ['UNSTABLE_itemBehavior']: 'action'
394
- }),
395
- descriptionProps,
396
- errorMessageProps,
397
- isInvalid,
398
- validationErrors,
399
- validationDetails
400
- };
401
- }