@react-aria/autocomplete 3.0.0-alpha.35 → 3.0.0-alpha.37

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 +37 -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 +222 -0
  174. package/dist/useAutocomplete.main.js.map +1 -0
  175. package/dist/useAutocomplete.mjs +217 -0
  176. package/dist/useAutocomplete.module.js +217 -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 +16 -12
  189. package/src/index.ts +3 -0
  190. package/src/useAutocomplete.ts +290 -0
@@ -0,0 +1,290 @@
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, RefObject} 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, isCtrlKeyPressed, mergeProps, mergeRefs, UPDATE_ACTIVEDESCENDANT, useEffectEvent, useId, useLabels, useObjectRef} from '@react-aria/utils';
17
+ // @ts-ignore
18
+ import intlMessages from '../intl/*.json';
19
+ import {KeyboardEvent as ReactKeyboardEvent, useCallback, useEffect, useMemo, useRef} from 'react';
20
+ import {useLocalizedStringFormatter} from '@react-aria/i18n';
21
+
22
+ export interface CollectionOptions extends DOMProps, AriaLabelingProps {
23
+ /** Whether the collection items should use virtual focus instead of being focused directly. */
24
+ shouldUseVirtualFocus: boolean,
25
+ /** Whether typeahead is disabled. */
26
+ disallowTypeAhead: boolean
27
+ }
28
+ export interface AriaAutocompleteProps extends AutocompleteProps {
29
+ /**
30
+ * An optional filter function used to determine if a option should be included in the autocomplete list.
31
+ * Include this if the items you are providing to your wrapped collection aren't filtered by default.
32
+ */
33
+ filter?: (textValue: string, inputValue: string) => boolean
34
+ }
35
+
36
+ export interface AriaAutocompleteOptions extends Omit<AriaAutocompleteProps, 'children'> {
37
+ /** The ref for the wrapped collection element. */
38
+ collectionRef: RefObject<HTMLElement | null>
39
+ }
40
+
41
+ export interface AutocompleteAria {
42
+ /** Props for the autocomplete textfield/searchfield element. These should be passed to the textfield/searchfield aria hooks respectively. */
43
+ textFieldProps: AriaTextFieldProps,
44
+ /** Props for the collection, to be passed to collection's respective aria hook (e.g. useMenu). */
45
+ collectionProps: CollectionOptions,
46
+ /** Ref to attach to the wrapped collection. */
47
+ collectionRef: RefObject<HTMLElement | null>,
48
+ /** A filter function that returns if the provided collection node should be filtered out of the collection. */
49
+ filterFn?: (nodeTextValue: string) => boolean
50
+ }
51
+
52
+ /**
53
+ * Provides the behavior and accessibility implementation for a autocomplete component.
54
+ * A autocomplete combines a text input with a collection, allowing users to filter the collection's contents match a query.
55
+ * @param props - Props for the autocomplete.
56
+ * @param state - State for the autocomplete, as returned by `useAutocompleteState`.
57
+ */
58
+ export function UNSTABLE_useAutocomplete(props: AriaAutocompleteOptions, state: AutocompleteState): AutocompleteAria {
59
+ let {
60
+ collectionRef,
61
+ filter
62
+ } = props;
63
+
64
+ let collectionId = useId();
65
+ let timeout = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
66
+ let delayNextActiveDescendant = useRef(false);
67
+ let queuedActiveDescendant = useRef(null);
68
+ let lastCollectionNode = useRef<HTMLElement>(null);
69
+
70
+ let updateActiveDescendant = useEffectEvent((e) => {
71
+ let {target} = e;
72
+ if (queuedActiveDescendant.current === target.id) {
73
+ return;
74
+ }
75
+
76
+ clearTimeout(timeout.current);
77
+ e.stopPropagation();
78
+
79
+ if (target !== collectionRef.current) {
80
+ if (delayNextActiveDescendant.current) {
81
+ queuedActiveDescendant.current = target.id;
82
+ timeout.current = setTimeout(() => {
83
+ state.setFocusedNodeId(target.id);
84
+ queuedActiveDescendant.current = null;
85
+ }, 500);
86
+ } else {
87
+ state.setFocusedNodeId(target.id);
88
+ }
89
+ } else {
90
+ state.setFocusedNodeId(null);
91
+ }
92
+
93
+ delayNextActiveDescendant.current = false;
94
+ });
95
+
96
+ let callbackRef = useCallback((collectionNode) => {
97
+ if (collectionNode != null) {
98
+ // When typing forward, we want to delay the setting of active descendant to not interrupt the native screen reader announcement
99
+ // of the letter you just typed. If we recieve another UPDATE_ACTIVEDESCENDANT call then we clear the queued update
100
+ // We track lastCollectionNode to do proper cleanup since callbackRefs just pass null when unmounting. This also handles
101
+ // React 19's extra call of the callback ref in strict mode
102
+ lastCollectionNode.current?.removeEventListener(UPDATE_ACTIVEDESCENDANT, updateActiveDescendant);
103
+ lastCollectionNode.current = collectionNode;
104
+ collectionNode.addEventListener(UPDATE_ACTIVEDESCENDANT, updateActiveDescendant);
105
+ } else {
106
+ lastCollectionNode.current?.removeEventListener(UPDATE_ACTIVEDESCENDANT, updateActiveDescendant);
107
+ }
108
+ }, [updateActiveDescendant]);
109
+
110
+ // Make sure to memo so that React doesn't keep registering a new event listeners on every rerender of the wrapped collection
111
+ let mergedCollectionRef = useObjectRef(useMemo(() => mergeRefs(collectionRef, callbackRef), [collectionRef, callbackRef]));
112
+
113
+ let focusFirstItem = useEffectEvent(() => {
114
+ delayNextActiveDescendant.current = true;
115
+ collectionRef.current?.dispatchEvent(
116
+ new CustomEvent(FOCUS_EVENT, {
117
+ cancelable: true,
118
+ bubbles: true,
119
+ detail: {
120
+ focusStrategy: 'first'
121
+ }
122
+ })
123
+ );
124
+ });
125
+
126
+ let clearVirtualFocus = useEffectEvent(() => {
127
+ state.setFocusedNodeId(null);
128
+ let clearFocusEvent = new CustomEvent(CLEAR_FOCUS_EVENT, {
129
+ cancelable: true,
130
+ bubbles: true
131
+ });
132
+ clearTimeout(timeout.current);
133
+ delayNextActiveDescendant.current = false;
134
+ collectionRef.current?.dispatchEvent(clearFocusEvent);
135
+ });
136
+
137
+ // TODO: update to see if we can tell what kind of event (paste vs backspace vs typing) is happening instead
138
+ let onChange = (value: string) => {
139
+ // Tell wrapped collection to focus the first element in the list when typing forward and to clear focused key when deleting text
140
+ // for screen reader announcements
141
+ if (state.inputValue !== value && state.inputValue.length <= value.length) {
142
+ focusFirstItem();
143
+ } else {
144
+ clearVirtualFocus();
145
+ }
146
+
147
+ state.setInputValue(value);
148
+ };
149
+
150
+ let keyDownTarget = useRef<Element | null>(null);
151
+ // For textfield specific keydown operations
152
+ let onKeyDown = (e: BaseEvent<ReactKeyboardEvent<any>>) => {
153
+ keyDownTarget.current = e.target as Element;
154
+ if (e.nativeEvent.isComposing) {
155
+ return;
156
+ }
157
+
158
+ switch (e.key) {
159
+ case 'a':
160
+ if (isCtrlKeyPressed(e)) {
161
+ return;
162
+ }
163
+ break;
164
+ case 'Escape':
165
+ // Early return for Escape here so it doesn't leak the Escape event from the simulated collection event below and
166
+ // close the dialog prematurely. Ideally that should be up to the discretion of the input element hence the check
167
+ // for isPropagationStopped
168
+ if (e.isDefaultPrevented()) {
169
+ return;
170
+ }
171
+ break;
172
+ case ' ':
173
+ // Space shouldn't trigger onAction so early return.
174
+
175
+ return;
176
+ case 'Home':
177
+ case 'End':
178
+ case 'PageDown':
179
+ case 'PageUp':
180
+ case 'ArrowUp':
181
+ case 'ArrowDown': {
182
+ if ((e.key === 'Home' || e.key === 'End') && state.focusedNodeId == null && e.shiftKey) {
183
+ return;
184
+ }
185
+
186
+ // Prevent these keys from moving the text cursor in the input
187
+ e.preventDefault();
188
+ // Move virtual focus into the wrapped collection
189
+ let focusCollection = new CustomEvent(FOCUS_EVENT, {
190
+ cancelable: true,
191
+ bubbles: true
192
+ });
193
+
194
+ collectionRef.current?.dispatchEvent(focusCollection);
195
+ break;
196
+ }
197
+ case 'ArrowLeft':
198
+ case 'ArrowRight':
199
+ // TODO: will need to special case this so it doesn't clear the focused key if we are currently
200
+ // focused on a submenutrigger? May not need to since focus would
201
+ // But what about wrapped grids where ArrowLeft and ArrowRight should navigate left/right
202
+ clearVirtualFocus();
203
+ break;
204
+ }
205
+
206
+ // Emulate the keyboard events that happen in the input field in the wrapped collection. This is for triggering things like onAction via Enter
207
+ // 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
208
+ // like ESC, the dispatched event below will bubble out of the collection and be stopped if handled by useSelectableCollection, otherwise will bubble
209
+ // as expected
210
+ if (!e.isPropagationStopped()) {
211
+ e.stopPropagation();
212
+ }
213
+
214
+ if (state.focusedNodeId == null) {
215
+ collectionRef.current?.dispatchEvent(
216
+ new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
217
+ );
218
+ } else {
219
+ let item = document.getElementById(state.focusedNodeId);
220
+ item?.dispatchEvent(
221
+ new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
222
+ );
223
+ }
224
+ };
225
+
226
+ let onKeyUpCapture = useEffectEvent((e) => {
227
+ // Dispatch simulated key up events for things like triggering links in listbox
228
+ // Make sure to stop the propagation of the input keyup event so that the simulated keyup/down pair
229
+ // is detected by usePress instead of the original keyup originating from the input
230
+ if (e.target === keyDownTarget.current) {
231
+ e.stopImmediatePropagation();
232
+ if (state.focusedNodeId == null) {
233
+ collectionRef.current?.dispatchEvent(
234
+ new KeyboardEvent(e.type, e)
235
+ );
236
+ } else {
237
+ let item = document.getElementById(state.focusedNodeId);
238
+ item?.dispatchEvent(
239
+ new KeyboardEvent(e.type, e)
240
+ );
241
+ }
242
+ }
243
+ });
244
+
245
+ useEffect(() => {
246
+ document.addEventListener('keyup', onKeyUpCapture, true);
247
+ return () => {
248
+ document.removeEventListener('keyup', onKeyUpCapture, true);
249
+ };
250
+ }, [onKeyUpCapture]);
251
+
252
+ let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/autocomplete');
253
+ let collectionProps = useLabels({
254
+ id: collectionId,
255
+ 'aria-label': stringFormatter.format('collectionLabel')
256
+ });
257
+
258
+ let filterFn = useCallback((nodeTextValue: string) => {
259
+ if (filter) {
260
+ return filter(nodeTextValue, state.inputValue);
261
+ }
262
+
263
+ return true;
264
+ }, [state.inputValue, filter]);
265
+
266
+ return {
267
+ textFieldProps: {
268
+ value: state.inputValue,
269
+ onChange,
270
+ onKeyDown,
271
+ autoComplete: 'off',
272
+ 'aria-haspopup': 'listbox',
273
+ 'aria-controls': collectionId,
274
+ // TODO: readd proper logic for completionMode = complete (aria-autocomplete: both)
275
+ 'aria-autocomplete': 'list',
276
+ 'aria-activedescendant': state.focusedNodeId ?? undefined,
277
+ // This disable's iOS's autocorrect suggestions, since the autocomplete provides its own suggestions.
278
+ autoCorrect: 'off',
279
+ // This disable's the macOS Safari spell check auto corrections.
280
+ spellCheck: 'false'
281
+ },
282
+ collectionProps: mergeProps(collectionProps, {
283
+ // TODO: shouldFocusOnHover? shouldFocusWrap? Should it be up to the wrapped collection?
284
+ shouldUseVirtualFocus: true,
285
+ disallowTypeAhead: true
286
+ }),
287
+ collectionRef: mergedCollectionRef,
288
+ filterFn: filter != null ? filterFn : undefined
289
+ };
290
+ }