UNPKG

6.42 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import { refType } from '@material-ui/utils';
7import SwitchBase from '../internal/SwitchBase';
8import CheckBoxOutlineBlankIcon from '../internal/svg-icons/CheckBoxOutlineBlank';
9import CheckBoxIcon from '../internal/svg-icons/CheckBox';
10import { alpha } from '../styles/colorManipulator';
11import IndeterminateCheckBoxIcon from '../internal/svg-icons/IndeterminateCheckBox';
12import capitalize from '../utils/capitalize';
13import withStyles from '../styles/withStyles';
14export const styles = theme => ({
15 /* Styles applied to the root element. */
16 root: {
17 color: theme.palette.text.secondary
18 },
19
20 /* Pseudo-class applied to the root element if `checked={true}`. */
21 checked: {},
22
23 /* Pseudo-class applied to the root element if `disabled={true}`. */
24 disabled: {},
25
26 /* Pseudo-class applied to the root element if `indeterminate={true}`. */
27 indeterminate: {},
28
29 /* Styles applied to the root element if `color="primary"`. */
30 colorPrimary: {
31 '&$checked': {
32 color: theme.palette.primary.main,
33 '&:hover': {
34 backgroundColor: alpha(theme.palette.primary.main, theme.palette.action.hoverOpacity),
35 // Reset on touch devices, it doesn't add specificity
36 '@media (hover: none)': {
37 backgroundColor: 'transparent'
38 }
39 }
40 },
41 '&$disabled': {
42 color: theme.palette.action.disabled
43 }
44 },
45
46 /* Styles applied to the root element if `color="secondary"`. */
47 colorSecondary: {
48 '&$checked': {
49 color: theme.palette.secondary.main,
50 '&:hover': {
51 backgroundColor: alpha(theme.palette.secondary.main, theme.palette.action.hoverOpacity),
52 // Reset on touch devices, it doesn't add specificity
53 '@media (hover: none)': {
54 backgroundColor: 'transparent'
55 }
56 }
57 },
58 '&$disabled': {
59 color: theme.palette.action.disabled
60 }
61 }
62});
63const defaultCheckedIcon = /*#__PURE__*/React.createElement(CheckBoxIcon, null);
64const defaultIcon = /*#__PURE__*/React.createElement(CheckBoxOutlineBlankIcon, null);
65const defaultIndeterminateIcon = /*#__PURE__*/React.createElement(IndeterminateCheckBoxIcon, null);
66const Checkbox = /*#__PURE__*/React.forwardRef(function Checkbox(props, ref) {
67 const {
68 checkedIcon = defaultCheckedIcon,
69 classes,
70 color = 'secondary',
71 icon: iconProp = defaultIcon,
72 indeterminate = false,
73 indeterminateIcon: indeterminateIconProp = defaultIndeterminateIcon,
74 inputProps,
75 size = 'medium'
76 } = props,
77 other = _objectWithoutPropertiesLoose(props, ["checkedIcon", "classes", "color", "icon", "indeterminate", "indeterminateIcon", "inputProps", "size"]);
78
79 const icon = indeterminate ? indeterminateIconProp : iconProp;
80 const indeterminateIcon = indeterminate ? indeterminateIconProp : checkedIcon;
81 return /*#__PURE__*/React.createElement(SwitchBase, _extends({
82 type: "checkbox",
83 classes: {
84 root: clsx(classes.root, classes[`color${capitalize(color)}`], indeterminate && classes.indeterminate),
85 checked: classes.checked,
86 disabled: classes.disabled
87 },
88 color: color,
89 inputProps: _extends({
90 'data-indeterminate': indeterminate
91 }, inputProps),
92 icon: /*#__PURE__*/React.cloneElement(icon, {
93 fontSize: icon.props.fontSize === undefined && size === "small" ? size : icon.props.fontSize
94 }),
95 checkedIcon: /*#__PURE__*/React.cloneElement(indeterminateIcon, {
96 fontSize: indeterminateIcon.props.fontSize === undefined && size === "small" ? size : indeterminateIcon.props.fontSize
97 }),
98 ref: ref
99 }, other));
100});
101process.env.NODE_ENV !== "production" ? Checkbox.propTypes = {
102 // ----------------------------- Warning --------------------------------
103 // | These PropTypes are generated from the TypeScript type definitions |
104 // | To update them edit the d.ts file and run "yarn proptypes" |
105 // ----------------------------------------------------------------------
106
107 /**
108 * If `true`, the component is checked.
109 */
110 checked: PropTypes.bool,
111
112 /**
113 * The icon to display when the component is checked.
114 */
115 checkedIcon: PropTypes.node,
116
117 /**
118 * Override or extend the styles applied to the component.
119 * See [CSS API](#css) below for more details.
120 */
121 classes: PropTypes.object,
122
123 /**
124 * The color of the component. It supports those theme colors that make sense for this component.
125 */
126 color: PropTypes.oneOf(['default', 'primary', 'secondary']),
127
128 /**
129 * If `true`, the checkbox will be disabled.
130 */
131 disabled: PropTypes.bool,
132
133 /**
134 * If `true`, the ripple effect will be disabled.
135 */
136 disableRipple: PropTypes.bool,
137
138 /**
139 * The icon to display when the component is unchecked.
140 */
141 icon: PropTypes.node,
142
143 /**
144 * The id of the `input` element.
145 */
146 id: PropTypes.string,
147
148 /**
149 * If `true`, the component appears indeterminate.
150 * This does not set the native input element to indeterminate due
151 * to inconsistent behavior across browsers.
152 * However, we set a `data-indeterminate` attribute on the input.
153 */
154 indeterminate: PropTypes.bool,
155
156 /**
157 * The icon to display when the component is indeterminate.
158 */
159 indeterminateIcon: PropTypes.node,
160
161 /**
162 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
163 */
164 inputProps: PropTypes.object,
165
166 /**
167 * Pass a ref to the `input` element.
168 */
169 inputRef: refType,
170
171 /**
172 * Callback fired when the state is changed.
173 *
174 * @param {object} event The event source of the callback.
175 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
176 */
177 onChange: PropTypes.func,
178
179 /**
180 * If `true`, the `input` element will be required.
181 */
182 required: PropTypes.bool,
183
184 /**
185 * The size of the checkbox.
186 * `small` is equivalent to the dense checkbox styling.
187 */
188 size: PropTypes.oneOf(['medium', 'small']),
189
190 /**
191 * The value of the component. The DOM API casts this to a string.
192 * The browser uses "on" as the default value.
193 */
194 value: PropTypes.any
195} : void 0;
196export default withStyles(styles, {
197 name: 'MuiCheckbox'
198})(Checkbox);
\No newline at end of file