UNPKG

7.66 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2import _extends from "@babel/runtime/helpers/esm/extends";
3const _excluded = ["checkedIcon", "color", "icon", "indeterminate", "indeterminateIcon", "inputProps", "size"];
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import { refType } from '@mui/utils';
7import { unstable_composeClasses as composeClasses } from '@mui/base';
8import { alpha } from '@mui/system';
9import SwitchBase from '../internal/SwitchBase';
10import CheckBoxOutlineBlankIcon from '../internal/svg-icons/CheckBoxOutlineBlank';
11import CheckBoxIcon from '../internal/svg-icons/CheckBox';
12import IndeterminateCheckBoxIcon from '../internal/svg-icons/IndeterminateCheckBox';
13import capitalize from '../utils/capitalize';
14import useThemeProps from '../styles/useThemeProps';
15import styled, { rootShouldForwardProp } from '../styles/styled';
16import checkboxClasses, { getCheckboxUtilityClass } from './checkboxClasses';
17import { jsx as _jsx } from "react/jsx-runtime";
18
19const useUtilityClasses = ownerState => {
20 const {
21 classes,
22 indeterminate,
23 color
24 } = ownerState;
25 const slots = {
26 root: ['root', indeterminate && 'indeterminate', `color${capitalize(color)}`]
27 };
28 const composedClasses = composeClasses(slots, getCheckboxUtilityClass, classes);
29 return _extends({}, classes, composedClasses);
30};
31
32const CheckboxRoot = styled(SwitchBase, {
33 shouldForwardProp: prop => rootShouldForwardProp(prop) || prop === 'classes',
34 name: 'MuiCheckbox',
35 slot: 'Root',
36 overridesResolver: (props, styles) => {
37 const {
38 ownerState
39 } = props;
40 return [styles.root, ownerState.indeterminate && styles.indeterminate, ownerState.color !== 'default' && styles[`color${capitalize(ownerState.color)}`]];
41 }
42})(({
43 theme,
44 ownerState
45}) => _extends({
46 color: (theme.vars || theme).palette.text.secondary
47}, !ownerState.disableRipple && {
48 '&:hover': {
49 backgroundColor: theme.vars ? `rgba(${ownerState.color === 'default' ? theme.vars.palette.action.activeChannel : theme.vars.palette.primary.mainChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(ownerState.color === 'default' ? theme.palette.action.active : theme.palette[ownerState.color].main, theme.palette.action.hoverOpacity),
50 // Reset on touch devices, it doesn't add specificity
51 '@media (hover: none)': {
52 backgroundColor: 'transparent'
53 }
54 }
55}, ownerState.color !== 'default' && {
56 [`&.${checkboxClasses.checked}, &.${checkboxClasses.indeterminate}`]: {
57 color: (theme.vars || theme).palette[ownerState.color].main
58 },
59 [`&.${checkboxClasses.disabled}`]: {
60 color: (theme.vars || theme).palette.action.disabled
61 }
62}));
63
64const defaultCheckedIcon = /*#__PURE__*/_jsx(CheckBoxIcon, {});
65
66const defaultIcon = /*#__PURE__*/_jsx(CheckBoxOutlineBlankIcon, {});
67
68const defaultIndeterminateIcon = /*#__PURE__*/_jsx(IndeterminateCheckBoxIcon, {});
69
70const Checkbox = /*#__PURE__*/React.forwardRef(function Checkbox(inProps, ref) {
71 const props = useThemeProps({
72 props: inProps,
73 name: 'MuiCheckbox'
74 });
75
76 const {
77 checkedIcon = defaultCheckedIcon,
78 color = 'primary',
79 icon: iconProp = defaultIcon,
80 indeterminate = false,
81 indeterminateIcon: indeterminateIconProp = defaultIndeterminateIcon,
82 inputProps,
83 size = 'medium'
84 } = props,
85 other = _objectWithoutPropertiesLoose(props, _excluded);
86
87 const icon = indeterminate ? indeterminateIconProp : iconProp;
88 const indeterminateIcon = indeterminate ? indeterminateIconProp : checkedIcon;
89
90 const ownerState = _extends({}, props, {
91 color,
92 indeterminate,
93 size
94 });
95
96 const classes = useUtilityClasses(ownerState);
97 return /*#__PURE__*/_jsx(CheckboxRoot, _extends({
98 type: "checkbox",
99 inputProps: _extends({
100 'data-indeterminate': indeterminate
101 }, inputProps),
102 icon: /*#__PURE__*/React.cloneElement(icon, {
103 fontSize: icon.props.fontSize ?? size
104 }),
105 checkedIcon: /*#__PURE__*/React.cloneElement(indeterminateIcon, {
106 fontSize: indeterminateIcon.props.fontSize ?? size
107 }),
108 ownerState: ownerState,
109 ref: ref
110 }, other, {
111 classes: classes
112 }));
113});
114process.env.NODE_ENV !== "production" ? Checkbox.propTypes
115/* remove-proptypes */
116= {
117 // ----------------------------- Warning --------------------------------
118 // | These PropTypes are generated from the TypeScript type definitions |
119 // | To update them edit the d.ts file and run "yarn proptypes" |
120 // ----------------------------------------------------------------------
121
122 /**
123 * If `true`, the component is checked.
124 */
125 checked: PropTypes.bool,
126
127 /**
128 * The icon to display when the component is checked.
129 * @default <CheckBoxIcon />
130 */
131 checkedIcon: PropTypes.node,
132
133 /**
134 * Override or extend the styles applied to the component.
135 */
136 classes: PropTypes.object,
137
138 /**
139 * The color of the component.
140 * It supports both default and custom theme colors, which can be added as shown in the
141 * [palette customization guide](https://mui.com/material-ui/customization/palette/#adding-new-colors).
142 * @default 'primary'
143 */
144 color: PropTypes
145 /* @typescript-to-proptypes-ignore */
146 .oneOfType([PropTypes.oneOf(['default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
147
148 /**
149 * The default checked state. Use when the component is not controlled.
150 */
151 defaultChecked: PropTypes.bool,
152
153 /**
154 * If `true`, the component is disabled.
155 */
156 disabled: PropTypes.bool,
157
158 /**
159 * If `true`, the ripple effect is disabled.
160 */
161 disableRipple: PropTypes.bool,
162
163 /**
164 * The icon to display when the component is unchecked.
165 * @default <CheckBoxOutlineBlankIcon />
166 */
167 icon: PropTypes.node,
168
169 /**
170 * The id of the `input` element.
171 */
172 id: PropTypes.string,
173
174 /**
175 * If `true`, the component appears indeterminate.
176 * This does not set the native input element to indeterminate due
177 * to inconsistent behavior across browsers.
178 * However, we set a `data-indeterminate` attribute on the `input`.
179 * @default false
180 */
181 indeterminate: PropTypes.bool,
182
183 /**
184 * The icon to display when the component is indeterminate.
185 * @default <IndeterminateCheckBoxIcon />
186 */
187 indeterminateIcon: PropTypes.node,
188
189 /**
190 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
191 */
192 inputProps: PropTypes.object,
193
194 /**
195 * Pass a ref to the `input` element.
196 */
197 inputRef: refType,
198
199 /**
200 * Callback fired when the state is changed.
201 *
202 * @param {React.ChangeEvent<HTMLInputElement>} event The event source of the callback.
203 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
204 */
205 onChange: PropTypes.func,
206
207 /**
208 * If `true`, the `input` element is required.
209 */
210 required: PropTypes.bool,
211
212 /**
213 * The size of the component.
214 * `small` is equivalent to the dense checkbox styling.
215 * @default 'medium'
216 */
217 size: PropTypes
218 /* @typescript-to-proptypes-ignore */
219 .oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.string]),
220
221 /**
222 * The system prop that allows defining system overrides as well as additional CSS styles.
223 */
224 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
225
226 /**
227 * The value of the component. The DOM API casts this to a string.
228 * The browser uses "on" as the default value.
229 */
230 value: PropTypes.any
231} : void 0;
232export default Checkbox;
\No newline at end of file