@react-aria/autocomplete 3.0.0-nightly.5042 → 3.0.0-rc.1

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 (190) hide show
  1. package/dist/ar-AE.main.js +6 -0
  2. package/dist/ar-AE.main.js.map +1 -0
  3. package/dist/ar-AE.mjs +8 -0
  4. package/dist/ar-AE.module.js +8 -0
  5. package/dist/ar-AE.module.js.map +1 -0
  6. package/dist/bg-BG.main.js +6 -0
  7. package/dist/bg-BG.main.js.map +1 -0
  8. package/dist/bg-BG.mjs +8 -0
  9. package/dist/bg-BG.module.js +8 -0
  10. package/dist/bg-BG.module.js.map +1 -0
  11. package/dist/cs-CZ.main.js +6 -0
  12. package/dist/cs-CZ.main.js.map +1 -0
  13. package/dist/cs-CZ.mjs +8 -0
  14. package/dist/cs-CZ.module.js +8 -0
  15. package/dist/cs-CZ.module.js.map +1 -0
  16. package/dist/da-DK.main.js +6 -0
  17. package/dist/da-DK.main.js.map +1 -0
  18. package/dist/da-DK.mjs +8 -0
  19. package/dist/da-DK.module.js +8 -0
  20. package/dist/da-DK.module.js.map +1 -0
  21. package/dist/de-DE.main.js +6 -0
  22. package/dist/de-DE.main.js.map +1 -0
  23. package/dist/de-DE.mjs +8 -0
  24. package/dist/de-DE.module.js +8 -0
  25. package/dist/de-DE.module.js.map +1 -0
  26. package/dist/el-GR.main.js +6 -0
  27. package/dist/el-GR.main.js.map +1 -0
  28. package/dist/el-GR.mjs +8 -0
  29. package/dist/el-GR.module.js +8 -0
  30. package/dist/el-GR.module.js.map +1 -0
  31. package/dist/en-US.main.js +6 -0
  32. package/dist/en-US.main.js.map +1 -0
  33. package/dist/en-US.mjs +8 -0
  34. package/dist/en-US.module.js +8 -0
  35. package/dist/en-US.module.js.map +1 -0
  36. package/dist/es-ES.main.js +6 -0
  37. package/dist/es-ES.main.js.map +1 -0
  38. package/dist/es-ES.mjs +8 -0
  39. package/dist/es-ES.module.js +8 -0
  40. package/dist/es-ES.module.js.map +1 -0
  41. package/dist/et-EE.main.js +6 -0
  42. package/dist/et-EE.main.js.map +1 -0
  43. package/dist/et-EE.mjs +8 -0
  44. package/dist/et-EE.module.js +8 -0
  45. package/dist/et-EE.module.js.map +1 -0
  46. package/dist/fi-FI.main.js +6 -0
  47. package/dist/fi-FI.main.js.map +1 -0
  48. package/dist/fi-FI.mjs +8 -0
  49. package/dist/fi-FI.module.js +8 -0
  50. package/dist/fi-FI.module.js.map +1 -0
  51. package/dist/fr-FR.main.js +6 -0
  52. package/dist/fr-FR.main.js.map +1 -0
  53. package/dist/fr-FR.mjs +8 -0
  54. package/dist/fr-FR.module.js +8 -0
  55. package/dist/fr-FR.module.js.map +1 -0
  56. package/dist/he-IL.main.js +6 -0
  57. package/dist/he-IL.main.js.map +1 -0
  58. package/dist/he-IL.mjs +8 -0
  59. package/dist/he-IL.module.js +8 -0
  60. package/dist/he-IL.module.js.map +1 -0
  61. package/dist/hr-HR.main.js +6 -0
  62. package/dist/hr-HR.main.js.map +1 -0
  63. package/dist/hr-HR.mjs +8 -0
  64. package/dist/hr-HR.module.js +8 -0
  65. package/dist/hr-HR.module.js.map +1 -0
  66. package/dist/hu-HU.main.js +6 -0
  67. package/dist/hu-HU.main.js.map +1 -0
  68. package/dist/hu-HU.mjs +8 -0
  69. package/dist/hu-HU.module.js +8 -0
  70. package/dist/hu-HU.module.js.map +1 -0
  71. package/dist/import.mjs +3 -1
  72. package/dist/intlStrings.main.js +108 -0
  73. package/dist/intlStrings.main.js.map +1 -0
  74. package/dist/intlStrings.mjs +110 -0
  75. package/dist/intlStrings.module.js +110 -0
  76. package/dist/intlStrings.module.js.map +1 -0
  77. package/dist/it-IT.main.js +6 -0
  78. package/dist/it-IT.main.js.map +1 -0
  79. package/dist/it-IT.mjs +8 -0
  80. package/dist/it-IT.module.js +8 -0
  81. package/dist/it-IT.module.js.map +1 -0
  82. package/dist/ja-JP.main.js +6 -0
  83. package/dist/ja-JP.main.js.map +1 -0
  84. package/dist/ja-JP.mjs +8 -0
  85. package/dist/ja-JP.module.js +8 -0
  86. package/dist/ja-JP.module.js.map +1 -0
  87. package/dist/ko-KR.main.js +6 -0
  88. package/dist/ko-KR.main.js.map +1 -0
  89. package/dist/ko-KR.mjs +8 -0
  90. package/dist/ko-KR.module.js +8 -0
  91. package/dist/ko-KR.module.js.map +1 -0
  92. package/dist/lt-LT.main.js +6 -0
  93. package/dist/lt-LT.main.js.map +1 -0
  94. package/dist/lt-LT.mjs +8 -0
  95. package/dist/lt-LT.module.js +8 -0
  96. package/dist/lt-LT.module.js.map +1 -0
  97. package/dist/lv-LV.main.js +6 -0
  98. package/dist/lv-LV.main.js.map +1 -0
  99. package/dist/lv-LV.mjs +8 -0
  100. package/dist/lv-LV.module.js +8 -0
  101. package/dist/lv-LV.module.js.map +1 -0
  102. package/dist/main.js +3 -0
  103. package/dist/main.js.map +1 -1
  104. package/dist/module.js +3 -1
  105. package/dist/module.js.map +1 -1
  106. package/dist/nb-NO.main.js +6 -0
  107. package/dist/nb-NO.main.js.map +1 -0
  108. package/dist/nb-NO.mjs +8 -0
  109. package/dist/nb-NO.module.js +8 -0
  110. package/dist/nb-NO.module.js.map +1 -0
  111. package/dist/nl-NL.main.js +6 -0
  112. package/dist/nl-NL.main.js.map +1 -0
  113. package/dist/nl-NL.mjs +8 -0
  114. package/dist/nl-NL.module.js +8 -0
  115. package/dist/nl-NL.module.js.map +1 -0
  116. package/dist/pl-PL.main.js +6 -0
  117. package/dist/pl-PL.main.js.map +1 -0
  118. package/dist/pl-PL.mjs +8 -0
  119. package/dist/pl-PL.module.js +8 -0
  120. package/dist/pl-PL.module.js.map +1 -0
  121. package/dist/pt-BR.main.js +6 -0
  122. package/dist/pt-BR.main.js.map +1 -0
  123. package/dist/pt-BR.mjs +8 -0
  124. package/dist/pt-BR.module.js +8 -0
  125. package/dist/pt-BR.module.js.map +1 -0
  126. package/dist/pt-PT.main.js +6 -0
  127. package/dist/pt-PT.main.js.map +1 -0
  128. package/dist/pt-PT.mjs +8 -0
  129. package/dist/pt-PT.module.js +8 -0
  130. package/dist/pt-PT.module.js.map +1 -0
  131. package/dist/ro-RO.main.js +6 -0
  132. package/dist/ro-RO.main.js.map +1 -0
  133. package/dist/ro-RO.mjs +8 -0
  134. package/dist/ro-RO.module.js +8 -0
  135. package/dist/ro-RO.module.js.map +1 -0
  136. package/dist/ru-RU.main.js +6 -0
  137. package/dist/ru-RU.main.js.map +1 -0
  138. package/dist/ru-RU.mjs +8 -0
  139. package/dist/ru-RU.module.js +8 -0
  140. package/dist/ru-RU.module.js.map +1 -0
  141. package/dist/sk-SK.main.js +6 -0
  142. package/dist/sk-SK.main.js.map +1 -0
  143. package/dist/sk-SK.mjs +8 -0
  144. package/dist/sk-SK.module.js +8 -0
  145. package/dist/sk-SK.module.js.map +1 -0
  146. package/dist/sl-SI.main.js +6 -0
  147. package/dist/sl-SI.main.js.map +1 -0
  148. package/dist/sl-SI.mjs +8 -0
  149. package/dist/sl-SI.module.js +8 -0
  150. package/dist/sl-SI.module.js.map +1 -0
  151. package/dist/sr-SP.main.js +6 -0
  152. package/dist/sr-SP.main.js.map +1 -0
  153. package/dist/sr-SP.mjs +8 -0
  154. package/dist/sr-SP.module.js +8 -0
  155. package/dist/sr-SP.module.js.map +1 -0
  156. package/dist/sv-SE.main.js +6 -0
  157. package/dist/sv-SE.main.js.map +1 -0
  158. package/dist/sv-SE.mjs +8 -0
  159. package/dist/sv-SE.module.js +8 -0
  160. package/dist/sv-SE.module.js.map +1 -0
  161. package/dist/tr-TR.main.js +6 -0
  162. package/dist/tr-TR.main.js.map +1 -0
  163. package/dist/tr-TR.mjs +8 -0
  164. package/dist/tr-TR.module.js +8 -0
  165. package/dist/tr-TR.module.js.map +1 -0
  166. package/dist/types.d.ts +52 -1
  167. package/dist/types.d.ts.map +1 -1
  168. package/dist/uk-UA.main.js +6 -0
  169. package/dist/uk-UA.main.js.map +1 -0
  170. package/dist/uk-UA.mjs +8 -0
  171. package/dist/uk-UA.module.js +8 -0
  172. package/dist/uk-UA.module.js.map +1 -0
  173. package/dist/useAutocomplete.main.js +301 -0
  174. package/dist/useAutocomplete.main.js.map +1 -0
  175. package/dist/useAutocomplete.mjs +296 -0
  176. package/dist/useAutocomplete.module.js +296 -0
  177. package/dist/useAutocomplete.module.js.map +1 -0
  178. package/dist/zh-CN.main.js +6 -0
  179. package/dist/zh-CN.main.js.map +1 -0
  180. package/dist/zh-CN.mjs +8 -0
  181. package/dist/zh-CN.module.js +8 -0
  182. package/dist/zh-CN.module.js.map +1 -0
  183. package/dist/zh-TW.main.js +6 -0
  184. package/dist/zh-TW.main.js.map +1 -0
  185. package/dist/zh-TW.mjs +8 -0
  186. package/dist/zh-TW.module.js +8 -0
  187. package/dist/zh-TW.module.js.map +1 -0
  188. package/package.json +22 -13
  189. package/src/index.ts +3 -0
  190. package/src/useAutocomplete.ts +414 -0
@@ -0,0 +1,414 @@
1
+ /*
2
+ * Copyright 2024 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 {AriaLabelingProps, BaseEvent, DOMProps, FocusableElement, FocusEvents, KeyboardEvents, Node, RefObject, ValueBase} from '@react-types/shared';
14
+ import {AriaTextFieldProps} from '@react-aria/textfield';
15
+ import {AutocompleteProps, AutocompleteState} from '@react-stately/autocomplete';
16
+ import {CLEAR_FOCUS_EVENT, FOCUS_EVENT, getActiveElement, getOwnerDocument, isAndroid, isCtrlKeyPressed, isIOS, mergeProps, mergeRefs, useEffectEvent, useEvent, useLabels, useObjectRef, useSlotId} from '@react-aria/utils';
17
+ import {dispatchVirtualBlur, dispatchVirtualFocus, getVirtuallyFocusedElement, moveVirtualFocus} from '@react-aria/focus';
18
+ import {getInteractionModality} from '@react-aria/interactions';
19
+ // @ts-ignore
20
+ import intlMessages from '../intl/*.json';
21
+ import {FocusEvent as ReactFocusEvent, KeyboardEvent as ReactKeyboardEvent, useCallback, useEffect, useMemo, useRef} from 'react';
22
+ import {useLocalizedStringFormatter} from '@react-aria/i18n';
23
+
24
+ export interface CollectionOptions extends DOMProps, AriaLabelingProps {
25
+ /** Whether the collection items should use virtual focus instead of being focused directly. */
26
+ shouldUseVirtualFocus: boolean,
27
+ /** Whether typeahead is disabled. */
28
+ disallowTypeAhead: boolean
29
+ }
30
+
31
+ export interface InputProps<T = FocusableElement> extends DOMProps,
32
+ FocusEvents<T>,
33
+ KeyboardEvents,
34
+ Pick<ValueBase<string>, 'onChange' | 'value'>,
35
+ Pick<AriaTextFieldProps, 'enterKeyHint' | 'aria-controls' | 'aria-autocomplete' | 'aria-activedescendant' | 'spellCheck' | 'autoCorrect' | 'autoComplete'> {}
36
+
37
+ export interface AriaAutocompleteProps<T> extends AutocompleteProps {
38
+ /**
39
+ * An optional filter function used to determine if a option should be included in the autocomplete list.
40
+ * Include this if the items you are providing to your wrapped collection aren't filtered by default.
41
+ */
42
+ filter?: (textValue: string, inputValue: string, node: Node<T>) => boolean,
43
+
44
+ /**
45
+ * Whether or not to focus the first item in the collection after a filter is performed. Note this is only applicable
46
+ * if virtual focus behavior is not turned off via `disableVirtualFocus`.
47
+ * @default false
48
+ */
49
+ disableAutoFocusFirst?: boolean,
50
+
51
+ /**
52
+ * Whether the autocomplete should disable virtual focus, instead making the wrapped collection directly tabbable.
53
+ * @default false
54
+ */
55
+ disableVirtualFocus?: boolean
56
+ }
57
+
58
+ export interface AriaAutocompleteOptions<T> extends Omit<AriaAutocompleteProps<T>, 'children'> {
59
+ /** The ref for the wrapped collection element. */
60
+ inputRef: RefObject<HTMLInputElement | null>,
61
+ /** The ref for the wrapped collection element. */
62
+ collectionRef: RefObject<HTMLElement | null>
63
+ }
64
+
65
+ export interface AutocompleteAria<T> {
66
+ /** Props for the autocomplete input element. These should be passed to the input's aria hooks (e.g. useTextField/useSearchField/etc) respectively. */
67
+ inputProps: InputProps,
68
+ /** Props for the collection, to be passed to collection's respective aria hook (e.g. useMenu). */
69
+ collectionProps: CollectionOptions,
70
+ /** Ref to attach to the wrapped collection. */
71
+ collectionRef: RefObject<HTMLElement | null>,
72
+ /** A filter function that returns if the provided collection node should be filtered out of the collection. */
73
+ filter?: (nodeTextValue: string, node: Node<T>) => boolean
74
+ }
75
+
76
+ /**
77
+ * Provides the behavior and accessibility implementation for an autocomplete component.
78
+ * An autocomplete combines a text input with a collection, allowing users to filter the collection's contents match a query.
79
+ * @param props - Props for the autocomplete.
80
+ * @param state - State for the autocomplete, as returned by `useAutocompleteState`.
81
+ */
82
+ export function useAutocomplete<T>(props: AriaAutocompleteOptions<T>, state: AutocompleteState): AutocompleteAria<T> {
83
+ let {
84
+ inputRef,
85
+ collectionRef,
86
+ filter,
87
+ disableAutoFocusFirst = false,
88
+ disableVirtualFocus = false
89
+ } = props;
90
+
91
+ let collectionId = useSlotId();
92
+ let timeout = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
93
+ let delayNextActiveDescendant = useRef(false);
94
+ let queuedActiveDescendant = useRef<string | null>(null);
95
+ let lastCollectionNode = useRef<HTMLElement>(null);
96
+
97
+ // For mobile screen readers, we don't want virtual focus, instead opting to disable FocusScope's restoreFocus and manually
98
+ // moving focus back to the subtriggers
99
+ let isMobileScreenReader = getInteractionModality() === 'virtual' && (isIOS() || isAndroid());
100
+ let shouldUseVirtualFocus = !isMobileScreenReader && !disableVirtualFocus;
101
+ useEffect(() => {
102
+ return () => clearTimeout(timeout.current);
103
+ }, []);
104
+
105
+ let updateActiveDescendant = useEffectEvent((e: Event) => {
106
+ // Ensure input is focused if the user clicks on the collection directly.
107
+ if (!e.isTrusted && shouldUseVirtualFocus && inputRef.current && getActiveElement(getOwnerDocument(inputRef.current)) !== inputRef.current) {
108
+ inputRef.current.focus();
109
+ }
110
+
111
+ let target = e.target as Element | null;
112
+ if (e.isTrusted || !target || queuedActiveDescendant.current === target.id) {
113
+ return;
114
+ }
115
+
116
+ clearTimeout(timeout.current);
117
+ if (target !== collectionRef.current) {
118
+ if (delayNextActiveDescendant.current) {
119
+ queuedActiveDescendant.current = target.id;
120
+ timeout.current = setTimeout(() => {
121
+ state.setFocusedNodeId(target.id);
122
+ }, 500);
123
+ } else {
124
+ queuedActiveDescendant.current = target.id;
125
+ state.setFocusedNodeId(target.id);
126
+ }
127
+ } else {
128
+ queuedActiveDescendant.current = null;
129
+ state.setFocusedNodeId(null);
130
+ }
131
+
132
+ delayNextActiveDescendant.current = false;
133
+ });
134
+
135
+ let callbackRef = useCallback((collectionNode) => {
136
+ if (collectionNode != null) {
137
+ // When typing forward, we want to delay the setting of active descendant to not interrupt the native screen reader announcement
138
+ // of the letter you just typed. If we recieve another focus event then we clear the queued update
139
+ // We track lastCollectionNode to do proper cleanup since callbackRefs just pass null when unmounting. This also handles
140
+ // React 19's extra call of the callback ref in strict mode
141
+ lastCollectionNode.current?.removeEventListener('focusin', updateActiveDescendant);
142
+ lastCollectionNode.current = collectionNode;
143
+ collectionNode.addEventListener('focusin', updateActiveDescendant);
144
+ } else {
145
+ lastCollectionNode.current?.removeEventListener('focusin', updateActiveDescendant);
146
+ }
147
+ }, [updateActiveDescendant]);
148
+
149
+ // Make sure to memo so that React doesn't keep registering a new event listeners on every rerender of the wrapped collection
150
+ let mergedCollectionRef = useObjectRef(useMemo(() => mergeRefs(collectionRef, callbackRef), [collectionRef, callbackRef]));
151
+
152
+ let focusFirstItem = useEffectEvent(() => {
153
+ delayNextActiveDescendant.current = true;
154
+ collectionRef.current?.dispatchEvent(
155
+ new CustomEvent(FOCUS_EVENT, {
156
+ cancelable: true,
157
+ bubbles: true,
158
+ detail: {
159
+ focusStrategy: 'first'
160
+ }
161
+ })
162
+ );
163
+ });
164
+
165
+ let clearVirtualFocus = useEffectEvent((clearFocusKey?: boolean) => {
166
+ moveVirtualFocus(getActiveElement());
167
+ queuedActiveDescendant.current = null;
168
+ state.setFocusedNodeId(null);
169
+ let clearFocusEvent = new CustomEvent(CLEAR_FOCUS_EVENT, {
170
+ cancelable: true,
171
+ bubbles: true,
172
+ detail: {
173
+ clearFocusKey
174
+ }
175
+ });
176
+ clearTimeout(timeout.current);
177
+ delayNextActiveDescendant.current = false;
178
+ collectionRef.current?.dispatchEvent(clearFocusEvent);
179
+ });
180
+
181
+ let lastInputType = useRef('');
182
+ useEvent(inputRef, 'input', e => {
183
+ let {inputType} = e as InputEvent;
184
+ lastInputType.current = inputType;
185
+ });
186
+
187
+ let onChange = (value: string) => {
188
+ // Tell wrapped collection to focus the first element in the list when typing forward and to clear focused key when modifying the text via
189
+ // copy paste/backspacing/undo/redo for screen reader announcements
190
+ if (lastInputType.current === 'insertText' && !disableAutoFocusFirst) {
191
+ focusFirstItem();
192
+ } else if (lastInputType.current.includes('insert') || lastInputType.current.includes('delete') || lastInputType.current.includes('history')) {
193
+ clearVirtualFocus(true);
194
+
195
+ // If onChange was triggered before the timeout actually updated the activedescendant, we need to fire
196
+ // our own dispatchVirtualFocus so focusVisible gets reapplied on the input
197
+ if (getVirtuallyFocusedElement(document) === inputRef.current) {
198
+ dispatchVirtualFocus(inputRef.current!, null);
199
+ }
200
+ }
201
+
202
+ state.setInputValue(value);
203
+ };
204
+
205
+ let keyDownTarget = useRef<Element | null>(null);
206
+ // For textfield specific keydown operations
207
+ let onKeyDown = (e: BaseEvent<ReactKeyboardEvent<any>>) => {
208
+ keyDownTarget.current = e.target as Element;
209
+ if (e.nativeEvent.isComposing) {
210
+ return;
211
+ }
212
+
213
+ let focusedNodeId = queuedActiveDescendant.current;
214
+ switch (e.key) {
215
+ case 'a':
216
+ if (isCtrlKeyPressed(e)) {
217
+ return;
218
+ }
219
+ break;
220
+ case 'Escape':
221
+ // Early return for Escape here so it doesn't leak the Escape event from the simulated collection event below and
222
+ // close the dialog prematurely. Ideally that should be up to the discretion of the input element hence the check
223
+ // for isPropagationStopped
224
+ if (e.isDefaultPrevented()) {
225
+ return;
226
+ }
227
+ break;
228
+ case ' ':
229
+ // Space shouldn't trigger onAction so early return.
230
+ return;
231
+ case 'Tab':
232
+ // Don't propogate Tab down to the collection, otherwise we will try to focus the collection via useSelectableCollection's Tab handler (aka shift tab logic)
233
+ // We want FocusScope to handle Tab if one exists (aka sub dialog), so special casepropogate
234
+ if ('continuePropagation' in e) {
235
+ e.continuePropagation();
236
+ }
237
+ return;
238
+ case 'Home':
239
+ case 'End':
240
+ case 'PageDown':
241
+ case 'PageUp':
242
+ case 'ArrowUp':
243
+ case 'ArrowDown': {
244
+ if ((e.key === 'Home' || e.key === 'End') && focusedNodeId == null && e.shiftKey) {
245
+ return;
246
+ }
247
+
248
+ // Prevent these keys from moving the text cursor in the input
249
+ e.preventDefault();
250
+ // Move virtual focus into the wrapped collection
251
+ let focusCollection = new CustomEvent(FOCUS_EVENT, {
252
+ cancelable: true,
253
+ bubbles: true
254
+ });
255
+
256
+ collectionRef.current?.dispatchEvent(focusCollection);
257
+ break;
258
+ }
259
+ }
260
+
261
+ // Emulate the keyboard events that happen in the input field in the wrapped collection. This is for triggering things like onAction via Enter
262
+ // or moving focus from one item to another. Stop propagation on the input event if it isn't already stopped so it doesn't leak out. For events
263
+ // like ESC, the dispatched event below will bubble out of the collection and be stopped if handled by useSelectableCollection, otherwise will bubble
264
+ // as expected
265
+ if (!e.isPropagationStopped()) {
266
+ e.stopPropagation();
267
+ }
268
+
269
+ let shouldPerformDefaultAction = true;
270
+ if (collectionRef.current !== null) {
271
+ if (focusedNodeId == null) {
272
+ shouldPerformDefaultAction = collectionRef.current?.dispatchEvent(
273
+ new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
274
+ ) || false;
275
+ } else {
276
+ let item = document.getElementById(focusedNodeId);
277
+ shouldPerformDefaultAction = item?.dispatchEvent(
278
+ new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
279
+ ) || false;
280
+ }
281
+ }
282
+
283
+ if (shouldPerformDefaultAction) {
284
+ switch (e.key) {
285
+ case 'ArrowLeft':
286
+ case 'ArrowRight': {
287
+ // Clear the activedescendant so NVDA announcements aren't interrupted but retain the focused key in the collection so the
288
+ // user's keyboard navigation restarts from where they left off
289
+ clearVirtualFocus();
290
+ break;
291
+ }
292
+ case 'Enter':
293
+ // Trigger click action on item when Enter key was pressed.
294
+ if (focusedNodeId != null) {
295
+ let item = document.getElementById(focusedNodeId);
296
+ item?.click();
297
+ }
298
+ break;
299
+ }
300
+ } else {
301
+ // TODO: check if we can do this, want to stop textArea from using its default Enter behavior so items are properly triggered
302
+ e.preventDefault();
303
+ }
304
+ };
305
+
306
+ let onKeyUpCapture = useEffectEvent((e) => {
307
+ // Dispatch simulated key up events for things like triggering links in listbox
308
+ // Make sure to stop the propagation of the input keyup event so that the simulated keyup/down pair
309
+ // is detected by usePress instead of the original keyup originating from the input
310
+ if (e.target === keyDownTarget.current) {
311
+ e.stopImmediatePropagation();
312
+ let focusedNodeId = queuedActiveDescendant.current;
313
+ if (focusedNodeId == null) {
314
+ collectionRef.current?.dispatchEvent(
315
+ new KeyboardEvent(e.type, e)
316
+ );
317
+ } else {
318
+ let item = document.getElementById(focusedNodeId);
319
+ item?.dispatchEvent(
320
+ new KeyboardEvent(e.type, e)
321
+ );
322
+ }
323
+ }
324
+ });
325
+
326
+ useEffect(() => {
327
+ document.addEventListener('keyup', onKeyUpCapture, true);
328
+ return () => {
329
+ document.removeEventListener('keyup', onKeyUpCapture, true);
330
+ };
331
+ }, [onKeyUpCapture]);
332
+
333
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/autocomplete');
334
+ let collectionProps = useLabels({
335
+ id: collectionId,
336
+ 'aria-label': stringFormatter.format('collectionLabel')
337
+ });
338
+
339
+ let filterFn = useCallback((nodeTextValue: string, node: Node<T>) => {
340
+ if (filter) {
341
+ return filter(nodeTextValue, state.inputValue, node);
342
+ }
343
+
344
+ return true;
345
+ }, [state.inputValue, filter]);
346
+
347
+ // Be sure to clear/restore the virtual + collection focus when blurring/refocusing the field so we only show the
348
+ // focus ring on the virtually focused collection when are actually interacting with the Autocomplete
349
+ let onBlur = (e: ReactFocusEvent) => {
350
+ if (!e.isTrusted) {
351
+ return;
352
+ }
353
+
354
+ let lastFocusedNode = queuedActiveDescendant.current ? document.getElementById(queuedActiveDescendant.current) : null;
355
+ if (lastFocusedNode) {
356
+ dispatchVirtualBlur(lastFocusedNode, e.relatedTarget);
357
+ }
358
+ };
359
+
360
+ let onFocus = (e: ReactFocusEvent) => {
361
+ if (!e.isTrusted) {
362
+ return;
363
+ }
364
+
365
+ let curFocusedNode = queuedActiveDescendant.current ? document.getElementById(queuedActiveDescendant.current) : null;
366
+ if (curFocusedNode) {
367
+ let target = e.target;
368
+ queueMicrotask(() => {
369
+ dispatchVirtualBlur(target, curFocusedNode);
370
+ dispatchVirtualFocus(curFocusedNode, target);
371
+ });
372
+ }
373
+ };
374
+
375
+ // Only apply the autocomplete specific behaviors if the collection component wrapped by it is actually
376
+ // being filtered/allows filtering by the Autocomplete.
377
+ let inputProps = {
378
+ value: state.inputValue,
379
+ onChange
380
+ } as AriaTextFieldProps<FocusableElement>;
381
+
382
+ let virtualFocusProps = {
383
+ onKeyDown,
384
+ 'aria-activedescendant': state.focusedNodeId ?? undefined,
385
+ onBlur,
386
+ onFocus
387
+ };
388
+
389
+ if (collectionId) {
390
+ inputProps = {
391
+ ...inputProps,
392
+ ...(shouldUseVirtualFocus && virtualFocusProps),
393
+ enterKeyHint: 'go',
394
+ 'aria-controls': collectionId,
395
+ // TODO: readd proper logic for completionMode = complete (aria-autocomplete: both)
396
+ 'aria-autocomplete': 'list',
397
+ // This disable's iOS's autocorrect suggestions, since the autocomplete provides its own suggestions.
398
+ autoCorrect: 'off',
399
+ // This disable's the macOS Safari spell check auto corrections.
400
+ spellCheck: 'false',
401
+ autoComplete: 'off'
402
+ };
403
+ }
404
+
405
+ return {
406
+ inputProps,
407
+ collectionProps: mergeProps(collectionProps, {
408
+ shouldUseVirtualFocus,
409
+ disallowTypeAhead: true
410
+ }),
411
+ collectionRef: mergedCollectionRef,
412
+ filter: filter != null ? filterFn : undefined
413
+ };
414
+ }