@react-spectrum/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.
Files changed (42) hide show
  1. package/README.md +3 -0
  2. package/dist/Checkbox.main.js +121 -0
  3. package/dist/Checkbox.main.js.map +1 -0
  4. package/dist/Checkbox.mjs +116 -0
  5. package/dist/Checkbox.module.js +116 -0
  6. package/dist/Checkbox.module.js.map +1 -0
  7. package/dist/CheckboxGroup.main.js +71 -0
  8. package/dist/CheckboxGroup.main.js.map +1 -0
  9. package/dist/CheckboxGroup.mjs +66 -0
  10. package/dist/CheckboxGroup.module.js +66 -0
  11. package/dist/CheckboxGroup.module.js.map +1 -0
  12. package/dist/checkbox_vars_css.main.js +59 -0
  13. package/dist/checkbox_vars_css.main.js.map +1 -0
  14. package/dist/checkbox_vars_css.mjs +61 -0
  15. package/dist/checkbox_vars_css.module.js +61 -0
  16. package/dist/checkbox_vars_css.module.js.map +1 -0
  17. package/dist/context.main.js +27 -0
  18. package/dist/context.main.js.map +1 -0
  19. package/dist/context.mjs +18 -0
  20. package/dist/context.module.js +18 -0
  21. package/dist/context.module.js.map +1 -0
  22. package/dist/fieldgroup_vars_css.main.js +32 -0
  23. package/dist/fieldgroup_vars_css.main.js.map +1 -0
  24. package/dist/fieldgroup_vars_css.mjs +34 -0
  25. package/dist/fieldgroup_vars_css.module.js +34 -0
  26. package/dist/fieldgroup_vars_css.module.js.map +1 -0
  27. package/dist/import.mjs +20 -0
  28. package/dist/main.js +26 -0
  29. package/dist/main.js.map +1 -0
  30. package/dist/module.js +20 -0
  31. package/dist/module.js.map +1 -0
  32. package/dist/types.d.ts +15 -0
  33. package/dist/types.d.ts.map +1 -0
  34. package/dist/vars.880c6573.css +94 -0
  35. package/dist/vars.880c6573.css.map +1 -0
  36. package/dist/vars.fe746eae.css +487 -0
  37. package/dist/vars.fe746eae.css.map +1 -0
  38. package/package.json +65 -0
  39. package/src/Checkbox.tsx +124 -0
  40. package/src/CheckboxGroup.tsx +70 -0
  41. package/src/context.ts +16 -0
  42. package/src/index.ts +15 -0
@@ -0,0 +1,124 @@
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 {CheckboxContext, useContextProps} from 'react-aria-components';
14
+ import {CheckboxGroupContext} from './context';
15
+ import CheckmarkSmall from '@spectrum-icons/ui/CheckmarkSmall';
16
+ import {classNames, useFocusableRef, useStyleProps} from '@react-spectrum/utils';
17
+ import DashSmall from '@spectrum-icons/ui/DashSmall';
18
+ import {FocusableRef} from '@react-types/shared';
19
+ import {FocusRing} from '@react-aria/focus';
20
+ import React, {forwardRef, useContext, useRef} from 'react';
21
+ import {SpectrumCheckboxProps} from '@react-types/checkbox';
22
+ import styles from '@adobe/spectrum-css-temp/components/checkbox/vars.css';
23
+ import {useCheckbox, useCheckboxGroupItem} from '@react-aria/checkbox';
24
+ import {useFormProps} from '@react-spectrum/form';
25
+ import {useHover} from '@react-aria/interactions';
26
+ import {useProviderProps} from '@react-spectrum/provider';
27
+ import {useToggleState} from '@react-stately/toggle';
28
+
29
+ function Checkbox(props: SpectrumCheckboxProps, ref: FocusableRef<HTMLLabelElement>) {
30
+ let originalProps = props;
31
+ let inputRef = useRef<HTMLInputElement>(null);
32
+ let domRef = useFocusableRef(ref, inputRef);
33
+
34
+ [props, domRef] = useContextProps(props, domRef, CheckboxContext);
35
+ props = useProviderProps(props);
36
+ props = useFormProps(props);
37
+ let {
38
+ isIndeterminate = false,
39
+ isEmphasized = false,
40
+ autoFocus,
41
+ children,
42
+ ...otherProps
43
+ } = props;
44
+ let {styleProps} = useStyleProps(otherProps);
45
+
46
+ // Swap hooks depending on whether this checkbox is inside a CheckboxGroup.
47
+ // This is a bit unorthodox. Typically, hooks cannot be called in a conditional,
48
+ // but since the checkbox won't move in and out of a group, it should be safe.
49
+ let groupState = useContext(CheckboxGroupContext);
50
+ let {inputProps, isInvalid, isDisabled} = groupState
51
+ // eslint-disable-next-line react-hooks/rules-of-hooks
52
+ ? useCheckboxGroupItem({
53
+ ...props,
54
+ // Value is optional for standalone checkboxes, but required for CheckboxGroup items;
55
+ // it's passed explicitly here to avoid typescript error (requires ignore).
56
+ // @ts-ignore
57
+ value: props.value,
58
+ // Only pass isRequired and validationState to react-aria if they came from
59
+ // the props for this individual checkbox, and not from the group via context.
60
+ isRequired: originalProps.isRequired,
61
+ validationState: originalProps.validationState,
62
+ isInvalid: originalProps.isInvalid
63
+ }, groupState, inputRef)
64
+ // eslint-disable-next-line react-hooks/rules-of-hooks
65
+ : useCheckbox(props, useToggleState(props), inputRef);
66
+
67
+ let {hoverProps, isHovered} = useHover({isDisabled});
68
+
69
+ let markIcon = isIndeterminate
70
+ ? <DashSmall UNSAFE_className={classNames(styles, 'spectrum-Checkbox-partialCheckmark')} />
71
+ : <CheckmarkSmall UNSAFE_className={classNames(styles, 'spectrum-Checkbox-checkmark')} />;
72
+
73
+ if (groupState) {
74
+ for (let key of ['isSelected', 'defaultSelected', 'isEmphasized']) {
75
+ if (originalProps[key] != null) {
76
+ console.warn(`${key} is unsupported on individual <Checkbox> elements within a <CheckboxGroup>. Please apply these props to the group instead.`);
77
+ }
78
+ }
79
+ if (props.value == null) {
80
+ console.warn('A <Checkbox> element within a <CheckboxGroup> requires a `value` property.');
81
+ }
82
+ }
83
+
84
+ return (
85
+ <label
86
+ {...styleProps}
87
+ {...hoverProps}
88
+ ref={domRef}
89
+ className={
90
+ classNames(
91
+ styles,
92
+ 'spectrum-Checkbox',
93
+ {
94
+ 'is-checked': inputProps.checked,
95
+ 'is-indeterminate': isIndeterminate,
96
+ 'spectrum-Checkbox--quiet': !isEmphasized,
97
+ 'is-invalid': isInvalid,
98
+ 'is-disabled': isDisabled,
99
+ 'is-hovered': isHovered
100
+ },
101
+ styleProps.className
102
+ )
103
+ }>
104
+ <FocusRing focusRingClass={classNames(styles, 'focus-ring')} autoFocus={autoFocus}>
105
+ <input
106
+ {...inputProps}
107
+ ref={inputRef}
108
+ className={classNames(styles, 'spectrum-Checkbox-input')} />
109
+ </FocusRing>
110
+ <span className={classNames(styles, 'spectrum-Checkbox-box')}>{markIcon}</span>
111
+ {children && (
112
+ <span className={classNames(styles, 'spectrum-Checkbox-label')}>
113
+ {children}
114
+ </span>
115
+ )}
116
+ </label>
117
+ );
118
+ }
119
+ /**
120
+ * Checkboxes allow users to select multiple items from a list of individual items,
121
+ * or to mark one individual item as selected.
122
+ */
123
+ let _Checkbox = forwardRef(Checkbox);
124
+ export {_Checkbox as Checkbox};
@@ -0,0 +1,70 @@
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 {CheckboxGroupContext} from './context';
14
+ import {classNames, useDOMRef} from '@react-spectrum/utils';
15
+ import {DOMRef} from '@react-types/shared';
16
+ import {Field} from '@react-spectrum/label';
17
+ import {Provider, useProviderProps} from '@react-spectrum/provider';
18
+ import React from 'react';
19
+ import {SpectrumCheckboxGroupProps} from '@react-types/checkbox';
20
+ import styles from '@adobe/spectrum-css-temp/components/fieldgroup/vars.css';
21
+ import {useCheckboxGroup} from '@react-aria/checkbox';
22
+ import {useCheckboxGroupState} from '@react-stately/checkbox';
23
+ import {useFormProps} from '@react-spectrum/form';
24
+
25
+ function CheckboxGroup(props: SpectrumCheckboxGroupProps, ref: DOMRef<HTMLDivElement>) {
26
+ props = useProviderProps(props);
27
+ props = useFormProps(props);
28
+ let {
29
+ isEmphasized,
30
+ children,
31
+ orientation = 'vertical'
32
+ } = props;
33
+ let domRef = useDOMRef(ref);
34
+ let state = useCheckboxGroupState(props);
35
+ let {groupProps, ...otherProps} = useCheckboxGroup(props, state);
36
+
37
+ return (
38
+ <Field
39
+ {...props}
40
+ {...otherProps}
41
+ ref={domRef}
42
+ wrapperClassName={classNames(styles, 'spectrum-FieldGroup')}
43
+ elementType="span"
44
+ includeNecessityIndicatorInAccessibilityName>
45
+ <div
46
+ {...groupProps}
47
+ className={
48
+ classNames(
49
+ styles,
50
+ 'spectrum-FieldGroup-group',
51
+ {
52
+ 'spectrum-FieldGroup-group--horizontal': orientation === 'horizontal'
53
+ }
54
+ )
55
+ }>
56
+ <Provider isEmphasized={isEmphasized}>
57
+ <CheckboxGroupContext.Provider value={state}>
58
+ {children}
59
+ </CheckboxGroupContext.Provider>
60
+ </Provider>
61
+ </div>
62
+ </Field>
63
+ );
64
+ }
65
+
66
+ /**
67
+ * A CheckboxGroup allows users to select one or more items from a list of choices.
68
+ */
69
+ const _CheckboxGroup = React.forwardRef(CheckboxGroup);
70
+ export {_CheckboxGroup as CheckboxGroup};
package/src/context.ts ADDED
@@ -0,0 +1,16 @@
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
+ import React from 'react';
15
+
16
+ export const CheckboxGroupContext = React.createContext<CheckboxGroupState | null>(null);
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
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
+ /// <reference types="css-module-types" />
13
+ export {Checkbox} from './Checkbox';
14
+ export {CheckboxGroup} from './CheckboxGroup';
15
+ export type {SpectrumCheckboxProps, SpectrumCheckboxGroupProps} from '@react-types/checkbox';