UNPKG

7.58 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = exports.styles = void 0;
9
10var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
11
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
14var _react = _interopRequireDefault(require("react"));
15
16var _propTypes = _interopRequireDefault(require("prop-types"));
17
18var _clsx = _interopRequireDefault(require("clsx"));
19
20var _utils = require("@material-ui/utils");
21
22var _SwitchBase = _interopRequireDefault(require("../internal/SwitchBase"));
23
24var _CheckBoxOutlineBlank = _interopRequireDefault(require("../internal/svg-icons/CheckBoxOutlineBlank"));
25
26var _CheckBox = _interopRequireDefault(require("../internal/svg-icons/CheckBox"));
27
28var _colorManipulator = require("../styles/colorManipulator");
29
30var _IndeterminateCheckBox = _interopRequireDefault(require("../internal/svg-icons/IndeterminateCheckBox"));
31
32var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
33
34var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
35
36var styles = function styles(theme) {
37 return {
38 /* Styles applied to the root element. */
39 root: {
40 color: theme.palette.text.secondary
41 },
42
43 /* Pseudo-class applied to the root element if `checked={true}`. */
44 checked: {},
45
46 /* Pseudo-class applied to the root element if `disabled={true}`. */
47 disabled: {},
48
49 /* Pseudo-class applied to the root element if `indeterminate={true}`. */
50 indeterminate: {},
51
52 /* Styles applied to the root element if `color="primary"`. */
53 colorPrimary: {
54 '&$checked': {
55 color: theme.palette.primary.main,
56 '&:hover': {
57 backgroundColor: (0, _colorManipulator.fade)(theme.palette.primary.main, theme.palette.action.hoverOpacity),
58 // Reset on touch devices, it doesn't add specificity
59 '@media (hover: none)': {
60 backgroundColor: 'transparent'
61 }
62 }
63 },
64 '&$disabled': {
65 color: theme.palette.action.disabled
66 }
67 },
68
69 /* Styles applied to the root element if `color="secondary"`. */
70 colorSecondary: {
71 '&$checked': {
72 color: theme.palette.secondary.main,
73 '&:hover': {
74 backgroundColor: (0, _colorManipulator.fade)(theme.palette.secondary.main, theme.palette.action.hoverOpacity),
75 // Reset on touch devices, it doesn't add specificity
76 '@media (hover: none)': {
77 backgroundColor: 'transparent'
78 }
79 }
80 },
81 '&$disabled': {
82 color: theme.palette.action.disabled
83 }
84 }
85 };
86};
87
88exports.styles = styles;
89
90var defaultCheckedIcon = _react.default.createElement(_CheckBox.default, null);
91
92var defaultIcon = _react.default.createElement(_CheckBoxOutlineBlank.default, null);
93
94var defaultIndeterminateIcon = _react.default.createElement(_IndeterminateCheckBox.default, null);
95
96var Checkbox = _react.default.forwardRef(function Checkbox(props, ref) {
97 var _props$checkedIcon = props.checkedIcon,
98 checkedIcon = _props$checkedIcon === void 0 ? defaultCheckedIcon : _props$checkedIcon,
99 classes = props.classes,
100 _props$color = props.color,
101 color = _props$color === void 0 ? 'secondary' : _props$color,
102 _props$disabled = props.disabled,
103 disabled = _props$disabled === void 0 ? false : _props$disabled,
104 _props$icon = props.icon,
105 icon = _props$icon === void 0 ? defaultIcon : _props$icon,
106 _props$indeterminate = props.indeterminate,
107 indeterminate = _props$indeterminate === void 0 ? false : _props$indeterminate,
108 _props$indeterminateI = props.indeterminateIcon,
109 indeterminateIcon = _props$indeterminateI === void 0 ? defaultIndeterminateIcon : _props$indeterminateI,
110 inputProps = props.inputProps,
111 _props$size = props.size,
112 size = _props$size === void 0 ? 'medium' : _props$size,
113 other = (0, _objectWithoutProperties2.default)(props, ["checkedIcon", "classes", "color", "disabled", "icon", "indeterminate", "indeterminateIcon", "inputProps", "size"]);
114 return _react.default.createElement(_SwitchBase.default, (0, _extends2.default)({
115 type: "checkbox",
116 classes: {
117 root: (0, _clsx.default)(classes.root, classes["color".concat((0, _capitalize.default)(color))], indeterminate && classes.indeterminate),
118 checked: classes.checked,
119 disabled: classes.disabled
120 },
121 color: color,
122 inputProps: (0, _extends2.default)({
123 'data-indeterminate': indeterminate
124 }, inputProps),
125 icon: _react.default.cloneElement(indeterminate ? indeterminateIcon : icon, {
126 fontSize: size === 'small' ? 'small' : 'default'
127 }),
128 checkedIcon: _react.default.cloneElement(indeterminate ? indeterminateIcon : checkedIcon, {
129 fontSize: size === 'small' ? 'small' : 'default'
130 }),
131 ref: ref,
132 disabled: disabled
133 }, other));
134});
135
136process.env.NODE_ENV !== "production" ? Checkbox.propTypes = {
137 /**
138 * If `true`, the component is checked.
139 */
140 checked: _propTypes.default.bool,
141
142 /**
143 * The icon to display when the component is checked.
144 */
145 checkedIcon: _propTypes.default.node,
146
147 /**
148 * Override or extend the styles applied to the component.
149 * See [CSS API](#css) below for more details.
150 */
151 classes: _propTypes.default.object.isRequired,
152
153 /**
154 * The color of the component. It supports those theme colors that make sense for this component.
155 */
156 color: _propTypes.default.oneOf(['primary', 'secondary', 'default']),
157
158 /**
159 * If `true`, the switch will be disabled.
160 */
161 disabled: _propTypes.default.bool,
162
163 /**
164 * If `true`, the ripple effect will be disabled.
165 */
166 disableRipple: _propTypes.default.bool,
167
168 /**
169 * The icon to display when the component is unchecked.
170 */
171 icon: _propTypes.default.node,
172
173 /**
174 * The id of the `input` element.
175 */
176 id: _propTypes.default.string,
177
178 /**
179 * If `true`, the component appears indeterminate.
180 * This does not set the native input element to indeterminate due
181 * to inconsistent behavior across browsers.
182 * However, we set a `data-indeterminate` attribute on the input.
183 */
184 indeterminate: _propTypes.default.bool,
185
186 /**
187 * The icon to display when the component is indeterminate.
188 */
189 indeterminateIcon: _propTypes.default.node,
190
191 /**
192 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
193 */
194 inputProps: _propTypes.default.object,
195
196 /**
197 * Pass a ref to the `input` element.
198 */
199 inputRef: _utils.refType,
200
201 /**
202 * Callback fired when the state is changed.
203 *
204 * @param {object} event The event source of the callback.
205 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
206 */
207 onChange: _propTypes.default.func,
208
209 /**
210 * If `true`, the `input` element will be required.
211 */
212 required: _propTypes.default.bool,
213
214 /**
215 * The size of the checkbox.
216 * `small` is equivalent to the dense checkbox styling.
217 */
218 size: _propTypes.default.oneOf(['small', 'medium']),
219
220 /**
221 * The input component prop `type`.
222 */
223 type: _propTypes.default.string,
224
225 /**
226 * The value of the component. The DOM API casts this to a string.
227 */
228 value: _propTypes.default.any
229} : void 0;
230
231var _default = (0, _withStyles.default)(styles, {
232 name: 'MuiCheckbox'
233})(Checkbox);
234
235exports.default = _default;
\No newline at end of file