@react-aria/checkbox 3.16.5 → 3.17.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,81 +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 {AriaCheckboxGroupProps} from '@react-types/checkbox';
14
- import {checkboxGroupData} from './utils';
15
- import {CheckboxGroupState} from '@react-stately/checkbox';
16
- import {DOMAttributes, ValidationResult} from '@react-types/shared';
17
- import {filterDOMProps, mergeProps} from '@react-aria/utils';
18
- import {useField} from '@react-aria/label';
19
- import {useFocusWithin} from '@react-aria/interactions';
20
-
21
- export interface CheckboxGroupAria extends ValidationResult {
22
- /** Props for the checkbox group wrapper element. */
23
- groupProps: DOMAttributes,
24
- /** Props for the checkbox group's visible label (if any). */
25
- labelProps: DOMAttributes,
26
- /** Props for the checkbox group description element, if any. */
27
- descriptionProps: DOMAttributes,
28
- /** Props for the checkbox group error message element, if any. */
29
- errorMessageProps: DOMAttributes
30
- }
31
-
32
- /**
33
- * Provides the behavior and accessibility implementation for a checkbox group component.
34
- * Checkbox groups allow users to select multiple items from a list of options.
35
- * @param props - Props for the checkbox group.
36
- * @param state - State for the checkbox group, as returned by `useCheckboxGroupState`.
37
- */
38
- export function useCheckboxGroup(props: AriaCheckboxGroupProps, state: CheckboxGroupState): CheckboxGroupAria {
39
- let {isDisabled, name, form, validationBehavior = 'aria'} = props;
40
- let {isInvalid, validationErrors, validationDetails} = state.displayValidation;
41
-
42
- let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
43
- ...props,
44
- // Checkbox group is not an HTML input element so it
45
- // shouldn't be labeled by a <label> element.
46
- labelElementType: 'span',
47
- isInvalid,
48
- errorMessage: props.errorMessage || validationErrors
49
- });
50
-
51
- checkboxGroupData.set(state, {
52
- name,
53
- form,
54
- descriptionId: descriptionProps.id,
55
- errorMessageId: errorMessageProps.id,
56
- validationBehavior
57
- });
58
-
59
- let domProps = filterDOMProps(props, {labelable: true});
60
-
61
- let {focusWithinProps} = useFocusWithin({
62
- onBlurWithin: props.onBlur,
63
- onFocusWithin: props.onFocus,
64
- onFocusWithinChange: props.onFocusChange
65
- });
66
-
67
- return {
68
- groupProps: mergeProps(domProps, {
69
- role: 'group',
70
- 'aria-disabled': isDisabled || undefined,
71
- ...fieldProps,
72
- ...focusWithinProps
73
- }),
74
- labelProps,
75
- descriptionProps,
76
- errorMessageProps,
77
- isInvalid,
78
- validationErrors,
79
- validationDetails
80
- };
81
- }
@@ -1,102 +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 {AriaCheckboxGroupItemProps} from '@react-types/checkbox';
14
- import {CheckboxAria, useCheckbox} from './useCheckbox';
15
- import {checkboxGroupData} from './utils';
16
- import {CheckboxGroupState} from '@react-stately/checkbox';
17
- import {DEFAULT_VALIDATION_RESULT, privateValidationStateProp, useFormValidationState} from '@react-stately/form';
18
- import {RefObject, ValidationResult} from '@react-types/shared';
19
- import {useEffect, useRef} from 'react';
20
- import {useToggleState} from '@react-stately/toggle';
21
-
22
- /**
23
- * Provides the behavior and accessibility implementation for a checkbox component contained within a checkbox group.
24
- * Checkbox groups allow users to select multiple items from a list of options.
25
- * @param props - Props for the checkbox.
26
- * @param state - State for the checkbox, as returned by `useCheckboxGroupState`.
27
- * @param inputRef - A ref for the HTML input element.
28
- */
29
- export function useCheckboxGroupItem(props: AriaCheckboxGroupItemProps, state: CheckboxGroupState, inputRef: RefObject<HTMLInputElement | null>): CheckboxAria {
30
- const toggleState = useToggleState({
31
- isReadOnly: props.isReadOnly || state.isReadOnly,
32
- isSelected: state.isSelected(props.value),
33
- defaultSelected: state.defaultValue.includes(props.value),
34
- onChange(isSelected) {
35
- if (isSelected) {
36
- state.addValue(props.value);
37
- } else {
38
- state.removeValue(props.value);
39
- }
40
-
41
- if (props.onChange) {
42
- props.onChange(isSelected);
43
- }
44
- }
45
- });
46
-
47
- let {name, form, descriptionId, errorMessageId, validationBehavior} = checkboxGroupData.get(state)!;
48
- validationBehavior = props.validationBehavior ?? validationBehavior;
49
-
50
- // Local validation for this checkbox.
51
- let {realtimeValidation} = useFormValidationState({
52
- ...props,
53
- value: toggleState.isSelected,
54
- // Server validation is handled at the group level.
55
- name: undefined,
56
- validationBehavior: 'aria'
57
- });
58
-
59
- // Update the checkbox group state when realtime validation changes.
60
- let nativeValidation = useRef(DEFAULT_VALIDATION_RESULT);
61
- let updateValidation = () => {
62
- state.setInvalid(props.value, realtimeValidation.isInvalid ? realtimeValidation : nativeValidation.current);
63
- };
64
-
65
- useEffect(updateValidation);
66
-
67
- // Combine group and checkbox level validation.
68
- let combinedRealtimeValidation = state.realtimeValidation.isInvalid ? state.realtimeValidation : realtimeValidation;
69
- let displayValidation = validationBehavior === 'native' ? state.displayValidation : combinedRealtimeValidation;
70
-
71
- let res = useCheckbox({
72
- ...props,
73
- isReadOnly: props.isReadOnly || state.isReadOnly,
74
- isDisabled: props.isDisabled || state.isDisabled,
75
- name: props.name || name,
76
- form: props.form || form,
77
- isRequired: props.isRequired ?? state.isRequired,
78
- validationBehavior,
79
- [privateValidationStateProp]: {
80
- realtimeValidation: combinedRealtimeValidation,
81
- displayValidation,
82
- resetValidation: state.resetValidation,
83
- commitValidation: state.commitValidation,
84
- updateValidation(v: ValidationResult) {
85
- nativeValidation.current = v;
86
- updateValidation();
87
- }
88
- }
89
- }, toggleState, inputRef);
90
-
91
- return {
92
- ...res,
93
- inputProps: {
94
- ...res.inputProps,
95
- 'aria-describedby': [
96
- props['aria-describedby'],
97
- state.isInvalid ? errorMessageId : null,
98
- descriptionId
99
- ].filter(Boolean).join(' ') || undefined
100
- }
101
- };
102
- }
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 {CheckboxGroupState} from '@react-stately/checkbox';
14
-
15
- interface CheckboxGroupData {
16
- name?: string,
17
- form?: string,
18
- descriptionId?: string,
19
- errorMessageId?: string,
20
- validationBehavior: 'aria' | 'native'
21
- }
22
-
23
- export const checkboxGroupData: WeakMap<CheckboxGroupState, CheckboxGroupData> = new WeakMap<CheckboxGroupState, CheckboxGroupData>();