@react-aria/checkbox 3.0.0-nightly-641446f65-240905

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.
@@ -0,0 +1,100 @@
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
+ onChange(isSelected) {
34
+ if (isSelected) {
35
+ state.addValue(props.value);
36
+ } else {
37
+ state.removeValue(props.value);
38
+ }
39
+
40
+ if (props.onChange) {
41
+ props.onChange(isSelected);
42
+ }
43
+ }
44
+ });
45
+
46
+ let {name, descriptionId, errorMessageId, validationBehavior} = checkboxGroupData.get(state)!;
47
+ validationBehavior = props.validationBehavior ?? validationBehavior;
48
+
49
+ // Local validation for this checkbox.
50
+ let {realtimeValidation} = useFormValidationState({
51
+ ...props,
52
+ value: toggleState.isSelected,
53
+ // Server validation is handled at the group level.
54
+ name: undefined,
55
+ validationBehavior: 'aria'
56
+ });
57
+
58
+ // Update the checkbox group state when realtime validation changes.
59
+ let nativeValidation = useRef(DEFAULT_VALIDATION_RESULT);
60
+ let updateValidation = () => {
61
+ state.setInvalid(props.value, realtimeValidation.isInvalid ? realtimeValidation : nativeValidation.current);
62
+ };
63
+
64
+ useEffect(updateValidation);
65
+
66
+ // Combine group and checkbox level validation.
67
+ let combinedRealtimeValidation = state.realtimeValidation.isInvalid ? state.realtimeValidation : realtimeValidation;
68
+ let displayValidation = validationBehavior === 'native' ? state.displayValidation : combinedRealtimeValidation;
69
+
70
+ let res = useCheckbox({
71
+ ...props,
72
+ isReadOnly: props.isReadOnly || state.isReadOnly,
73
+ isDisabled: props.isDisabled || state.isDisabled,
74
+ name: props.name || name,
75
+ isRequired: props.isRequired ?? state.isRequired,
76
+ validationBehavior,
77
+ [privateValidationStateProp]: {
78
+ realtimeValidation: combinedRealtimeValidation,
79
+ displayValidation,
80
+ resetValidation: state.resetValidation,
81
+ commitValidation: state.commitValidation,
82
+ updateValidation(v: ValidationResult) {
83
+ nativeValidation.current = v;
84
+ updateValidation();
85
+ }
86
+ }
87
+ }, toggleState, inputRef);
88
+
89
+ return {
90
+ ...res,
91
+ inputProps: {
92
+ ...res.inputProps,
93
+ 'aria-describedby': [
94
+ props['aria-describedby'],
95
+ state.isInvalid ? errorMessageId : null,
96
+ descriptionId
97
+ ].filter(Boolean).join(' ') || undefined
98
+ }
99
+ };
100
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,22 @@
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
+ descriptionId?: string,
18
+ errorMessageId?: string,
19
+ validationBehavior: 'aria' | 'native'
20
+ }
21
+
22
+ export const checkboxGroupData = new WeakMap<CheckboxGroupState, CheckboxGroupData>();