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