@react-aria/radio 3.12.4 → 3.13.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.
@@ -1,160 +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 {AriaRadioGroupProps} from '@react-types/radio';
14
- import {DOMAttributes, ValidationResult} from '@react-types/shared';
15
- import {filterDOMProps, getOwnerWindow, mergeProps, useId} from '@react-aria/utils';
16
- import {getFocusableTreeWalker} from '@react-aria/focus';
17
- import {radioGroupData} from './utils';
18
- import {RadioGroupState} from '@react-stately/radio';
19
- import {useField} from '@react-aria/label';
20
- import {useFocusWithin} from '@react-aria/interactions';
21
- import {useLocale} from '@react-aria/i18n';
22
-
23
- export interface RadioGroupAria extends ValidationResult {
24
- /** Props for the radio group wrapper element. */
25
- radioGroupProps: DOMAttributes,
26
- /** Props for the radio group's visible label (if any). */
27
- labelProps: DOMAttributes,
28
- /** Props for the radio group description element, if any. */
29
- descriptionProps: DOMAttributes,
30
- /** Props for the radio group error message element, if any. */
31
- errorMessageProps: DOMAttributes
32
- }
33
-
34
- /**
35
- * Provides the behavior and accessibility implementation for a radio group component.
36
- * Radio groups allow users to select a single item from a list of mutually exclusive options.
37
- * @param props - Props for the radio group.
38
- * @param state - State for the radio group, as returned by `useRadioGroupState`.
39
- */
40
- export function useRadioGroup(props: AriaRadioGroupProps, state: RadioGroupState): RadioGroupAria {
41
- let {
42
- name,
43
- form,
44
- isReadOnly,
45
- isRequired,
46
- isDisabled,
47
- orientation = 'vertical',
48
- validationBehavior = 'aria'
49
- } = props;
50
- let {direction} = useLocale();
51
-
52
- let {isInvalid, validationErrors, validationDetails} = state.displayValidation;
53
- let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
54
- ...props,
55
- // Radio group is not an HTML input element so it
56
- // shouldn't be labeled by a <label> element.
57
- labelElementType: 'span',
58
- isInvalid: state.isInvalid,
59
- errorMessage: props.errorMessage || validationErrors
60
- });
61
-
62
- let domProps = filterDOMProps(props, {labelable: true});
63
-
64
- // When the radio group loses focus, reset the focusable radio to null if
65
- // there is no selection. This allows tabbing into the group from either
66
- // direction to go to the first or last radio.
67
- let {focusWithinProps} = useFocusWithin({
68
- onBlurWithin(e) {
69
- props.onBlur?.(e);
70
- if (!state.selectedValue) {
71
- state.setLastFocusedValue(null);
72
- }
73
- },
74
- onFocusWithin: props.onFocus,
75
- onFocusWithinChange: props.onFocusChange
76
- });
77
-
78
- let onKeyDown = (e) => {
79
- let nextDir;
80
- switch (e.key) {
81
- case 'ArrowRight':
82
- if (direction === 'rtl' && orientation !== 'vertical') {
83
- nextDir = 'prev';
84
- } else {
85
- nextDir = 'next';
86
- }
87
- break;
88
- case 'ArrowLeft':
89
- if (direction === 'rtl' && orientation !== 'vertical') {
90
- nextDir = 'next';
91
- } else {
92
- nextDir = 'prev';
93
- }
94
- break;
95
- case 'ArrowDown':
96
- nextDir = 'next';
97
- break;
98
- case 'ArrowUp':
99
- nextDir = 'prev';
100
- break;
101
- default:
102
- return;
103
- }
104
- e.preventDefault();
105
- let walker = getFocusableTreeWalker(e.currentTarget, {
106
- from: e.target,
107
- accept: (node) => node instanceof getOwnerWindow(node).HTMLInputElement && node.type === 'radio'
108
- });
109
- let nextElem;
110
- if (nextDir === 'next') {
111
- nextElem = walker.nextNode();
112
- if (!nextElem) {
113
- walker.currentNode = e.currentTarget;
114
- nextElem = walker.firstChild();
115
- }
116
- } else {
117
- nextElem = walker.previousNode();
118
- if (!nextElem) {
119
- walker.currentNode = e.currentTarget;
120
- nextElem = walker.lastChild();
121
- }
122
- }
123
- if (nextElem) {
124
- // Call focus on nextElem so that keyboard navigation scrolls the radio into view
125
- nextElem.focus();
126
- state.setSelectedValue(nextElem.value);
127
- }
128
- };
129
-
130
- let groupName = useId(name);
131
- radioGroupData.set(state, {
132
- name: groupName,
133
- form,
134
- descriptionId: descriptionProps.id,
135
- errorMessageId: errorMessageProps.id,
136
- validationBehavior
137
- });
138
-
139
- return {
140
- radioGroupProps: mergeProps(domProps, {
141
- // https://www.w3.org/TR/wai-aria-1.2/#radiogroup
142
- role: 'radiogroup',
143
- onKeyDown,
144
- 'aria-invalid': state.isInvalid || undefined,
145
- 'aria-errormessage': props['aria-errormessage'],
146
- 'aria-readonly': isReadOnly || undefined,
147
- 'aria-required': isRequired || undefined,
148
- 'aria-disabled': isDisabled || undefined,
149
- 'aria-orientation': orientation,
150
- ...fieldProps,
151
- ...focusWithinProps
152
- }),
153
- labelProps,
154
- descriptionProps,
155
- errorMessageProps,
156
- isInvalid,
157
- validationErrors,
158
- validationDetails
159
- };
160
- }
package/src/utils.ts DELETED
@@ -1,23 +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 {RadioGroupState} from '@react-stately/radio';
14
-
15
- interface RadioGroupData {
16
- name: string,
17
- form: string | undefined,
18
- descriptionId: string | undefined,
19
- errorMessageId: string | undefined,
20
- validationBehavior: 'aria' | 'native'
21
- }
22
-
23
- export const radioGroupData: WeakMap<RadioGroupState, RadioGroupData> = new WeakMap<RadioGroupState, RadioGroupData>();