@react-aria/select 3.17.3 → 3.18.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.
package/src/useSelect.ts DELETED
@@ -1,259 +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 {AriaSelectProps, SelectionMode} from '@react-types/select';
16
- import {chain, filterDOMProps, mergeProps, nodeContains, useId} from '@react-aria/utils';
17
- import {DOMAttributes, KeyboardDelegate, RefObject, ValidationResult} from '@react-types/shared';
18
- import {FocusEvent, useMemo} from 'react';
19
- import {HiddenSelectProps} from './HiddenSelect';
20
- import {ListKeyboardDelegate, useTypeSelect} from '@react-aria/selection';
21
- import {SelectState} from '@react-stately/select';
22
- import {setInteractionModality} from '@react-aria/interactions';
23
- import {useCollator} from '@react-aria/i18n';
24
- import {useField} from '@react-aria/label';
25
- import {useMenuTrigger} from '@react-aria/menu';
26
-
27
- export interface AriaSelectOptions<T, M extends SelectionMode = 'single'> extends Omit<AriaSelectProps<T, M>, 'children'> {
28
- /**
29
- * An optional keyboard delegate implementation for type to select,
30
- * to override the default.
31
- */
32
- keyboardDelegate?: KeyboardDelegate
33
- }
34
-
35
- export interface SelectAria<T, M extends SelectionMode = 'single'> extends ValidationResult {
36
- /** Props for the label element. */
37
- labelProps: DOMAttributes,
38
-
39
- /** Props for the popup trigger element. */
40
- triggerProps: AriaButtonProps,
41
-
42
- /** Props for the element representing the selected value. */
43
- valueProps: DOMAttributes,
44
-
45
- /** Props for the popup. */
46
- menuProps: AriaListBoxOptions<T>,
47
-
48
- /** Props for the select's description element, if any. */
49
- descriptionProps: DOMAttributes,
50
-
51
- /** Props for the select's error message element, if any. */
52
- errorMessageProps: DOMAttributes,
53
-
54
- /** Props for the hidden select element. */
55
- hiddenSelectProps: HiddenSelectProps<T, M>
56
- }
57
-
58
- interface SelectData {
59
- isDisabled?: boolean,
60
- isRequired?: boolean,
61
- name?: string,
62
- form?: string,
63
- validationBehavior?: 'aria' | 'native'
64
- }
65
-
66
- export const selectData: WeakMap<SelectState<any, any>, SelectData> = new WeakMap<SelectState<any>, SelectData>();
67
-
68
- /**
69
- * Provides the behavior and accessibility implementation for a select component.
70
- * A select displays a collapsible list of options and allows a user to select one of them.
71
- * @param props - Props for the select.
72
- * @param state - State for the select, as returned by `useListState`.
73
- */
74
- export function useSelect<T, M extends SelectionMode = 'single'>(props: AriaSelectOptions<T, M>, state: SelectState<T, M>, ref: RefObject<HTMLElement | null>): SelectAria<T, M> {
75
- let {
76
- keyboardDelegate,
77
- isDisabled,
78
- isRequired,
79
- name,
80
- form,
81
- validationBehavior = 'aria'
82
- } = props;
83
-
84
- // By default, a KeyboardDelegate is provided which uses the DOM to query layout information (e.g. for page up/page down).
85
- // When virtualized, the layout object will be passed in as a prop and override this.
86
- let collator = useCollator({usage: 'search', sensitivity: 'base'});
87
- let delegate = useMemo(() => keyboardDelegate || new ListKeyboardDelegate(state.collection, state.disabledKeys, ref, collator), [keyboardDelegate, state.collection, state.disabledKeys, collator, ref]);
88
-
89
- let {menuTriggerProps, menuProps} = useMenuTrigger<T>(
90
- {
91
- isDisabled,
92
- type: 'listbox'
93
- },
94
- state,
95
- ref
96
- );
97
-
98
- let onKeyDown = (e: KeyboardEvent) => {
99
- if (state.selectionManager.selectionMode === 'multiple') {
100
- return;
101
- }
102
-
103
- switch (e.key) {
104
- case 'ArrowLeft': {
105
- // prevent scrolling containers
106
- e.preventDefault();
107
-
108
- let key = state.selectedKey != null ? delegate.getKeyAbove?.(state.selectedKey) : delegate.getFirstKey?.();
109
- if (key) {
110
- state.setSelectedKey(key);
111
- }
112
- break;
113
- }
114
- case 'ArrowRight': {
115
- // prevent scrolling containers
116
- e.preventDefault();
117
-
118
- let key = state.selectedKey != null ? delegate.getKeyBelow?.(state.selectedKey) : delegate.getFirstKey?.();
119
- if (key) {
120
- state.setSelectedKey(key);
121
- }
122
- break;
123
- }
124
- }
125
- };
126
-
127
- let {typeSelectProps} = useTypeSelect({
128
- keyboardDelegate: delegate,
129
- selectionManager: state.selectionManager,
130
- onTypeSelect(key) {
131
- state.setSelectedKey(key);
132
- }
133
- });
134
-
135
- let {isInvalid, validationErrors, validationDetails} = state.displayValidation;
136
- let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
137
- ...props,
138
- labelElementType: 'span',
139
- isInvalid,
140
- errorMessage: props.errorMessage || validationErrors
141
- });
142
-
143
- typeSelectProps.onKeyDown = typeSelectProps.onKeyDownCapture;
144
- delete typeSelectProps.onKeyDownCapture;
145
- if (state.selectionManager.selectionMode === 'multiple') {
146
- typeSelectProps = {};
147
- }
148
-
149
- let domProps = filterDOMProps(props, {labelable: true});
150
- let triggerProps = mergeProps(typeSelectProps, menuTriggerProps, fieldProps);
151
-
152
- let valueId = useId();
153
-
154
- selectData.set(state, {
155
- isDisabled,
156
- isRequired,
157
- name,
158
- form,
159
- validationBehavior
160
- });
161
-
162
- return {
163
- labelProps: {
164
- ...labelProps,
165
- onClick: () => {
166
- if (!props.isDisabled) {
167
- ref.current?.focus();
168
-
169
- // Show the focus ring so the user knows where focus went
170
- setInteractionModality('keyboard');
171
- }
172
- }
173
- },
174
- triggerProps: mergeProps(domProps, {
175
- ...triggerProps,
176
- isDisabled,
177
- onKeyDown: chain(triggerProps.onKeyDown, onKeyDown, props.onKeyDown),
178
- onKeyUp: props.onKeyUp,
179
- 'aria-labelledby': [
180
- valueId,
181
- triggerProps['aria-labelledby'],
182
- triggerProps['aria-label'] && !triggerProps['aria-labelledby'] ? triggerProps.id : null
183
- ].filter(Boolean).join(' '),
184
- onFocus(e: FocusEvent) {
185
- if (state.isFocused) {
186
- return;
187
- }
188
-
189
- if (props.onFocus) {
190
- props.onFocus(e);
191
- }
192
-
193
- if (props.onFocusChange) {
194
- props.onFocusChange(true);
195
- }
196
-
197
- state.setFocused(true);
198
- },
199
- onBlur(e: FocusEvent) {
200
- if (state.isOpen) {
201
- return;
202
- }
203
-
204
- if (props.onBlur) {
205
- props.onBlur(e);
206
- }
207
-
208
- if (props.onFocusChange) {
209
- props.onFocusChange(false);
210
- }
211
-
212
- state.setFocused(false);
213
- }
214
- }),
215
- valueProps: {
216
- id: valueId
217
- },
218
- menuProps: {
219
- ...menuProps,
220
- autoFocus: state.focusStrategy || true,
221
- shouldSelectOnPressUp: true,
222
- shouldFocusOnHover: true,
223
- disallowEmptySelection: true,
224
- linkBehavior: 'selection',
225
- onBlur: (e) => {
226
- if (nodeContains(e.currentTarget, e.relatedTarget as Node)) {
227
- return;
228
- }
229
-
230
- if (props.onBlur) {
231
- props.onBlur(e);
232
- }
233
-
234
- if (props.onFocusChange) {
235
- props.onFocusChange(false);
236
- }
237
-
238
- state.setFocused(false);
239
- },
240
- 'aria-labelledby': [
241
- fieldProps['aria-labelledby'],
242
- triggerProps['aria-label'] && !fieldProps['aria-labelledby'] ? triggerProps.id : null
243
- ].filter(Boolean).join(' ')
244
- },
245
- descriptionProps,
246
- errorMessageProps,
247
- isInvalid,
248
- validationErrors,
249
- validationDetails,
250
- hiddenSelectProps: {
251
- isDisabled,
252
- name,
253
- label: props.label,
254
- state,
255
- triggerRef: ref,
256
- form
257
- }
258
- };
259
- }