UNPKG

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