1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import refType from '@mui/utils/refType';
|
7 | import composeClasses from '@mui/utils/composeClasses';
|
8 | import { alpha } from '@mui/system/colorManipulator';
|
9 | import SwitchBase from "../internal/SwitchBase.js";
|
10 | import CheckBoxOutlineBlankIcon from "../internal/svg-icons/CheckBoxOutlineBlank.js";
|
11 | import CheckBoxIcon from "../internal/svg-icons/CheckBox.js";
|
12 | import IndeterminateCheckBoxIcon from "../internal/svg-icons/IndeterminateCheckBox.js";
|
13 | import capitalize from "../utils/capitalize.js";
|
14 | import rootShouldForwardProp from "../styles/rootShouldForwardProp.js";
|
15 | import checkboxClasses, { getCheckboxUtilityClass } from "./checkboxClasses.js";
|
16 | import { styled } from "../zero-styled/index.js";
|
17 | import memoTheme from "../utils/memoTheme.js";
|
18 | import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
|
19 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
20 | import { jsx as _jsx } from "react/jsx-runtime";
|
21 | const 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 |
|
35 | ...composedClasses
|
36 | };
|
37 | };
|
38 | const 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 |
|
86 | props: {
|
87 | disableRipple: false
|
88 | },
|
89 | style: {
|
90 |
|
91 | '&:hover': {
|
92 | '@media (hover: none)': {
|
93 | backgroundColor: 'transparent'
|
94 | }
|
95 | }
|
96 | }
|
97 | }]
|
98 | })));
|
99 | const defaultCheckedIcon = _jsx(CheckBoxIcon, {});
|
100 | const defaultIcon = _jsx(CheckBoxOutlineBlankIcon, {});
|
101 | const defaultIndeterminateIcon = _jsx(IndeterminateCheckBoxIcon, {});
|
102 | const Checkbox = 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 _jsx(CheckboxRoot, {
|
130 | type: "checkbox",
|
131 | inputProps: {
|
132 | 'data-indeterminate': indeterminate,
|
133 | ...inputProps
|
134 | },
|
135 | icon: React.cloneElement(icon, {
|
136 | fontSize: icon.props.fontSize ?? size
|
137 | }),
|
138 | checkedIcon: 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 | });
|
149 | process.env.NODE_ENV !== "production" ? Checkbox.propTypes = {
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | |
155 |
|
156 |
|
157 | checked: PropTypes.bool,
|
158 | |
159 |
|
160 |
|
161 |
|
162 | checkedIcon: PropTypes.node,
|
163 | |
164 |
|
165 |
|
166 | classes: PropTypes.object,
|
167 | |
168 |
|
169 |
|
170 | className: PropTypes.string,
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | color: PropTypes .oneOfType([PropTypes.oneOf(['default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
|
178 | |
179 |
|
180 |
|
181 | defaultChecked: PropTypes.bool,
|
182 | |
183 |
|
184 |
|
185 |
|
186 | disabled: PropTypes.bool,
|
187 | |
188 |
|
189 |
|
190 |
|
191 | disableRipple: PropTypes.bool,
|
192 | |
193 |
|
194 |
|
195 |
|
196 | icon: PropTypes.node,
|
197 | |
198 |
|
199 |
|
200 | id: PropTypes.string,
|
201 | |
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | indeterminate: PropTypes.bool,
|
209 | |
210 |
|
211 |
|
212 |
|
213 | indeterminateIcon: PropTypes.node,
|
214 | |
215 |
|
216 |
|
217 | inputProps: PropTypes.object,
|
218 | |
219 |
|
220 |
|
221 | inputRef: refType,
|
222 | |
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | onChange: PropTypes.func,
|
229 | |
230 |
|
231 |
|
232 |
|
233 | required: PropTypes.bool,
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 | size: PropTypes .oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.string]),
|
240 | |
241 |
|
242 |
|
243 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
|
244 | |
245 |
|
246 |
|
247 |
|
248 | value: PropTypes.any
|
249 | } : void 0;
|
250 | export default Checkbox; |
\ | No newline at end of file |