forked from adobe/react-spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckbox.tsx
More file actions
272 lines (260 loc) · 9.4 KB
/
Checkbox.tsx
File metadata and controls
272 lines (260 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright 2022 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {AriaCheckboxGroupProps, AriaCheckboxProps, HoverEvents, mergeProps, useCheckbox, useCheckboxGroup, useCheckboxGroupItem, useFocusRing, useFocusWithin, useHover, VisuallyHidden} from 'react-aria';
import {CheckboxContext} from './RSPContexts';
import {CheckboxGroupState, useCheckboxGroupState, useToggleState} from 'react-stately';
import {ContextValue, Provider, RACValidation, removeDataAttributes, RenderProps, SlotProps, useContextProps, useRenderProps, useSlot, useSlottedContext} from './utils';
import {FieldErrorContext} from './FieldError';
import {filterDOMProps, mergeRefs, useObjectRef} from '@react-aria/utils';
import {FormContext} from './Form';
import {forwardRefType, RefObject} from '@react-types/shared';
import {LabelContext} from './Label';
import React, {createContext, ForwardedRef, forwardRef, useContext} from 'react';
import {TextContext} from './Text';
export interface CheckboxGroupProps extends Omit<AriaCheckboxGroupProps, 'children' | 'label' | 'description' | 'errorMessage' | 'validationState' | 'validationBehavior'>, RACValidation, RenderProps<CheckboxGroupRenderProps>, SlotProps {}
export interface CheckboxProps extends Omit<AriaCheckboxProps, 'children' | 'validationState' | 'validationBehavior'>, HoverEvents, RACValidation, RenderProps<CheckboxRenderProps>, SlotProps {
/**
* A ref for the HTML input element.
*/
inputRef?: RefObject<HTMLInputElement | null>
}
export interface CheckboxGroupRenderProps {
/**
* Whether the checkbox group is disabled.
* @selector [data-disabled]
*/
isDisabled: boolean,
/**
* Whether the checkbox group is read only.
* @selector [data-readonly]
*/
isReadOnly: boolean,
/**
* Whether the checkbox group is required.
* @selector [data-required]
*/
isRequired: boolean,
/**
* Whether the checkbox group is invalid.
* @selector [data-invalid]
*/
isInvalid: boolean,
/**
* State of the checkbox group.
*/
state: CheckboxGroupState
}
export interface CheckboxRenderProps {
/**
* Whether the checkbox is selected.
* @selector [data-selected]
*/
isSelected: boolean,
/**
* Whether the checkbox is indeterminate.
* @selector [data-indeterminate]
*/
isIndeterminate: boolean,
/**
* Whether the checkbox is currently hovered with a mouse.
* @selector [data-hovered]
*/
isHovered: boolean,
/**
* Whether the checkbox is currently in a pressed state.
* @selector [data-pressed]
*/
isPressed: boolean,
/**
* Whether the checkbox is focused, either via a mouse or keyboard.
* @selector [data-focused]
*/
isFocused: boolean,
/**
* Whether the checkbox is keyboard focused.
* @selector [data-focus-visible]
*/
isFocusVisible: boolean,
/**
* Whether the checkbox is disabled.
* @selector [data-disabled]
*/
isDisabled: boolean,
/**
* Whether the checkbox is read only.
* @selector [data-readonly]
*/
isReadOnly: boolean,
/**
* Whether the checkbox invalid.
* @selector [data-invalid]
*/
isInvalid: boolean,
/**
* Whether the checkbox is required.
* @selector [data-required]
*/
isRequired: boolean
}
export const CheckboxGroupContext = createContext<ContextValue<CheckboxGroupProps, HTMLDivElement>>(null);
export const CheckboxGroupStateContext = createContext<CheckboxGroupState | null>(null);
/**
* A checkbox group allows a user to select multiple items from a list of options.
*/
export const CheckboxGroup = /*#__PURE__*/ (forwardRef as forwardRefType)(function CheckboxGroup(props: CheckboxGroupProps, ref: ForwardedRef<HTMLDivElement>) {
[props, ref] = useContextProps(props, ref, CheckboxGroupContext);
let {validationBehavior: formValidationBehavior} = useSlottedContext(FormContext) || {};
let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native';
let state = useCheckboxGroupState({
...props,
validationBehavior
});
let [labelRef, label] = useSlot(
!props['aria-label'] && !props['aria-labelledby']
);
let {groupProps, labelProps, descriptionProps, errorMessageProps, ...validation} = useCheckboxGroup({
...props,
label,
validationBehavior
}, state);
let renderProps = useRenderProps({
...props,
values: {
isDisabled: state.isDisabled,
isReadOnly: state.isReadOnly,
isRequired: props.isRequired || false,
isInvalid: state.isInvalid,
state
},
defaultClassName: 'react-aria-CheckboxGroup'
});
return (
<div
{...groupProps}
{...renderProps}
ref={ref}
slot={props.slot || undefined}
data-readonly={state.isReadOnly || undefined}
data-required={props.isRequired || undefined}
data-invalid={state.isInvalid || undefined}
data-disabled={props.isDisabled || undefined}>
<Provider
values={[
[CheckboxGroupStateContext, state],
[LabelContext, {...labelProps, ref: labelRef, elementType: 'span'}],
[TextContext, {
slots: {
description: descriptionProps,
errorMessage: errorMessageProps
}
}],
[FieldErrorContext, validation]
]}>
{renderProps.children}
</Provider>
</div>
);
});
/**
* A checkbox allows a user to select multiple items from a list of individual items, or
* to mark one individual item as selected.
*/
export const Checkbox = /*#__PURE__*/ (forwardRef as forwardRefType)(function Checkbox(props: CheckboxProps, ref: ForwardedRef<HTMLLabelElement>) {
let {
inputRef: userProvidedInputRef = null,
...otherProps
} = props;
[props, ref] = useContextProps(otherProps, ref, CheckboxContext);
let {validationBehavior: formValidationBehavior} = useSlottedContext(FormContext) || {};
let validationBehavior = props.validationBehavior ?? formValidationBehavior ?? 'native';
let groupState = useContext(CheckboxGroupStateContext);
let inputRef = useObjectRef(mergeRefs(userProvidedInputRef, props.inputRef !== undefined ? props.inputRef : null));
let {labelProps, inputProps, isSelected, isDisabled, isReadOnly, isPressed, isInvalid} = groupState
// eslint-disable-next-line react-hooks/rules-of-hooks
? useCheckboxGroupItem({
...removeDataAttributes(props),
// Value is optional for standalone checkboxes, but required for CheckboxGroup items;
// it's passed explicitly here to avoid typescript error (requires ignore).
// @ts-ignore
value: props.value,
// ReactNode type doesn't allow function children.
children: typeof props.children === 'function' ? true : props.children
}, groupState, inputRef)
// eslint-disable-next-line react-hooks/rules-of-hooks
: useCheckbox({
...removeDataAttributes(props),
onFocus: undefined,
onBlur: undefined,
children: typeof props.children === 'function' ? true : props.children,
validationBehavior
// eslint-disable-next-line react-hooks/rules-of-hooks
}, useToggleState(props), inputRef);
let {focusWithinProps} = useFocusWithin(props);
let {isFocused, isFocusVisible, focusProps} = useFocusRing();
let isInteractionDisabled = isDisabled || isReadOnly;
let {hoverProps, isHovered} = useHover({
...props,
isDisabled: isInteractionDisabled
});
let renderProps = useRenderProps({
...props,
defaultClassName: 'react-aria-Checkbox',
values: {
isSelected,
isIndeterminate: props.isIndeterminate || false,
isPressed,
isHovered,
isFocused,
isFocusVisible,
isDisabled,
isReadOnly,
isInvalid,
isRequired: props.isRequired || false
}
});
let DOMProps = filterDOMProps(props);
delete DOMProps.id;
return (
<label
{...mergeProps(DOMProps, labelProps, hoverProps, focusWithinProps, renderProps)}
ref={ref}
onBlur={(e) => {
const nextFocusTarget = e.relatedTarget as Node | null;
if (!e.currentTarget.contains(nextFocusTarget)) {
props.onBlur?.(e);
}
}}
onFocus={(e) => {
const prevFocusTarget = e.relatedTarget as Node | null;
if (!e.currentTarget.contains(prevFocusTarget)) {
props.onFocus?.(e);
}
}}
tabIndex={-1}
slot={props.slot || undefined}
data-selected={isSelected || undefined}
data-indeterminate={props.isIndeterminate || undefined}
data-pressed={isPressed || undefined}
data-hovered={isHovered || undefined}
data-focused={isFocused || undefined}
data-focus-visible={isFocusVisible || undefined}
data-disabled={isDisabled || undefined}
data-readonly={isReadOnly || undefined}
data-invalid={isInvalid || undefined}
data-required={props.isRequired || undefined}>
<VisuallyHidden elementType="span">
<input {...mergeProps(inputProps, focusProps)} ref={inputRef} />
</VisuallyHidden>
{renderProps.children}
</label>
);
});