UNPKG

4.3 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5exports.__esModule = true;
6exports["default"] = void 0;
7
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
10var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
11
12var _core = require("@emotion/core");
13
14var _react = require("react");
15
16var _Box = _interopRequireDefault(require("../Box"));
17
18var _ColorModeProvider = require("../ColorModeProvider");
19
20var _ControlBox = _interopRequireDefault(require("../ControlBox"));
21
22var _Icon = _interopRequireDefault(require("../Icon"));
23
24var _VisuallyHidden = _interopRequireDefault(require("../VisuallyHidden"));
25
26var _styles = _interopRequireDefault(require("./styles"));
27
28var _utils = require("../utils");
29
30/** @jsx jsx */
31var Checkbox = (0, _react.forwardRef)(function (_ref2, ref) {
32 var id = _ref2.id,
33 name = _ref2.name,
34 value = _ref2.value,
35 ariaLabel = _ref2["aria-label"],
36 ariaLabelledBy = _ref2["aria-labelledby"],
37 _ref2$variantColor = _ref2.variantColor,
38 variantColor = _ref2$variantColor === void 0 ? "blue" : _ref2$variantColor,
39 defaultIsChecked = _ref2.defaultIsChecked,
40 isChecked = _ref2.isChecked,
41 isFullWidth = _ref2.isFullWidth,
42 _ref2$size = _ref2.size,
43 size = _ref2$size === void 0 ? "md" : _ref2$size,
44 isDisabled = _ref2.isDisabled,
45 isInvalid = _ref2.isInvalid,
46 isReadOnly = _ref2.isReadOnly,
47 onChange = _ref2.onChange,
48 onBlur = _ref2.onBlur,
49 onFocus = _ref2.onFocus,
50 isIndeterminate = _ref2.isIndeterminate,
51 children = _ref2.children,
52 iconColor = _ref2.iconColor,
53 _ref2$iconSize = _ref2.iconSize,
54 iconSize = _ref2$iconSize === void 0 ? "10px" : _ref2$iconSize,
55 rest = (0, _objectWithoutPropertiesLoose2["default"])(_ref2, ["id", "name", "value", "aria-label", "aria-labelledby", "variantColor", "defaultIsChecked", "isChecked", "isFullWidth", "size", "isDisabled", "isInvalid", "isReadOnly", "onChange", "onBlur", "onFocus", "isIndeterminate", "children", "iconColor", "iconSize"]);
56 // Wrong usage of `variantColor` prop is quite common
57 // Let's add a warning hook that validates the passed variantColor
58 (0, _utils.useVariantColorWarning)("Checkbox", variantColor);
59
60 var _useColorMode = (0, _ColorModeProvider.useColorMode)(),
61 colorMode = _useColorMode.colorMode;
62
63 var styleProps = (0, _styles["default"])({
64 color: variantColor,
65 size: size,
66 colorMode: colorMode
67 });
68 var ownRef = (0, _react.useRef)();
69
70 var _ref = (0, _utils.useForkRef)(ownRef, ref);
71
72 (0, _react.useEffect)(function () {
73 if (_ref.current) {
74 _ref.current.indeterminate = Boolean(isIndeterminate);
75 }
76 }, [isIndeterminate, _ref]);
77 return (0, _core.jsx)(_Box["default"], (0, _extends2["default"])({
78 as: "label",
79 display: "inline-flex",
80 verticalAlign: "top",
81 alignItems: "center",
82 width: isFullWidth ? "full" : undefined,
83 cursor: isDisabled ? "not-allowed" : "pointer"
84 }, rest), (0, _core.jsx)(_VisuallyHidden["default"], {
85 as: "input",
86 type: "checkbox",
87 "aria-label": ariaLabel,
88 "aria-labelledby": ariaLabelledBy,
89 id: id,
90 ref: _ref,
91 name: name,
92 value: value,
93 onChange: isReadOnly ? undefined : onChange,
94 onBlur: onBlur,
95 onFocus: onFocus,
96 defaultChecked: isReadOnly ? undefined : defaultIsChecked,
97 checked: isReadOnly ? Boolean(isChecked) : defaultIsChecked ? undefined : isChecked,
98 disabled: isDisabled,
99 readOnly: isReadOnly,
100 "aria-readonly": isReadOnly,
101 "aria-invalid": isInvalid,
102 "aria-checked": isIndeterminate ? "mixed" : isChecked
103 }), (0, _core.jsx)(_ControlBox["default"], (0, _extends2["default"])({
104 opacity: isReadOnly ? 0.8 : 1
105 }, styleProps), (0, _core.jsx)(_Icon["default"], {
106 name: isIndeterminate ? "minus" : "check",
107 size: iconSize,
108 color: iconColor,
109 transition: "transform 240ms, opacity 240ms"
110 })), children && (0, _core.jsx)(_Box["default"], {
111 ml: 2,
112 fontSize: size,
113 userSelect: "none",
114 opacity: isDisabled ? 0.4 : 1
115 }, children));
116});
117Checkbox.displayName = "Checkbox";
118var _default = Checkbox;
119exports["default"] = _default;
\No newline at end of file