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 { useFormControl } from "../FormControl/index.js";
|
9 | import { styled } from "../zero-styled/index.js";
|
10 | import memoTheme from "../utils/memoTheme.js";
|
11 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
12 | import Typography from "../Typography/index.js";
|
13 | import capitalize from "../utils/capitalize.js";
|
14 | import formControlLabelClasses, { getFormControlLabelUtilityClasses } from "./formControlLabelClasses.js";
|
15 | import formControlState from "../FormControl/formControlState.js";
|
16 | import useSlot from "../utils/useSlot.js";
|
17 | import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
18 | const useUtilityClasses = ownerState => {
|
19 | const {
|
20 | classes,
|
21 | disabled,
|
22 | labelPlacement,
|
23 | error,
|
24 | required
|
25 | } = ownerState;
|
26 | const slots = {
|
27 | root: ['root', disabled && 'disabled', `labelPlacement${capitalize(labelPlacement)}`, error && 'error', required && 'required'],
|
28 | label: ['label', disabled && 'disabled'],
|
29 | asterisk: ['asterisk', error && 'error']
|
30 | };
|
31 | return composeClasses(slots, getFormControlLabelUtilityClasses, classes);
|
32 | };
|
33 | export const FormControlLabelRoot = styled('label', {
|
34 | name: 'MuiFormControlLabel',
|
35 | slot: 'Root',
|
36 | overridesResolver: (props, styles) => {
|
37 | const {
|
38 | ownerState
|
39 | } = props;
|
40 | return [{
|
41 | [`& .${formControlLabelClasses.label}`]: styles.label
|
42 | }, styles.root, styles[`labelPlacement${capitalize(ownerState.labelPlacement)}`]];
|
43 | }
|
44 | })(memoTheme(({
|
45 | theme
|
46 | }) => ({
|
47 | display: 'inline-flex',
|
48 | alignItems: 'center',
|
49 | cursor: 'pointer',
|
50 |
|
51 | verticalAlign: 'middle',
|
52 | WebkitTapHighlightColor: 'transparent',
|
53 | marginLeft: -11,
|
54 | marginRight: 16,
|
55 |
|
56 | [`&.${formControlLabelClasses.disabled}`]: {
|
57 | cursor: 'default'
|
58 | },
|
59 | [`& .${formControlLabelClasses.label}`]: {
|
60 | [`&.${formControlLabelClasses.disabled}`]: {
|
61 | color: (theme.vars || theme).palette.text.disabled
|
62 | }
|
63 | },
|
64 | variants: [{
|
65 | props: {
|
66 | labelPlacement: 'start'
|
67 | },
|
68 | style: {
|
69 | flexDirection: 'row-reverse',
|
70 | marginRight: -11
|
71 | }
|
72 | }, {
|
73 | props: {
|
74 | labelPlacement: 'top'
|
75 | },
|
76 | style: {
|
77 | flexDirection: 'column-reverse'
|
78 | }
|
79 | }, {
|
80 | props: {
|
81 | labelPlacement: 'bottom'
|
82 | },
|
83 | style: {
|
84 | flexDirection: 'column'
|
85 | }
|
86 | }, {
|
87 | props: ({
|
88 | labelPlacement
|
89 | }) => labelPlacement === 'start' || labelPlacement === 'top' || labelPlacement === 'bottom',
|
90 | style: {
|
91 | marginLeft: 16
|
92 | }
|
93 | }]
|
94 | })));
|
95 | const AsteriskComponent = styled('span', {
|
96 | name: 'MuiFormControlLabel',
|
97 | slot: 'Asterisk',
|
98 | overridesResolver: (props, styles) => styles.asterisk
|
99 | })(memoTheme(({
|
100 | theme
|
101 | }) => ({
|
102 | [`&.${formControlLabelClasses.error}`]: {
|
103 | color: (theme.vars || theme).palette.error.main
|
104 | }
|
105 | })));
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | const FormControlLabel = React.forwardRef(function FormControlLabel(inProps, ref) {
|
112 | const props = useDefaultProps({
|
113 | props: inProps,
|
114 | name: 'MuiFormControlLabel'
|
115 | });
|
116 | const {
|
117 | checked,
|
118 | className,
|
119 | componentsProps = {},
|
120 | control,
|
121 | disabled: disabledProp,
|
122 | disableTypography,
|
123 | inputRef,
|
124 | label: labelProp,
|
125 | labelPlacement = 'end',
|
126 | name,
|
127 | onChange,
|
128 | required: requiredProp,
|
129 | slots = {},
|
130 | slotProps = {},
|
131 | value,
|
132 | ...other
|
133 | } = props;
|
134 | const muiFormControl = useFormControl();
|
135 | const disabled = disabledProp ?? control.props.disabled ?? muiFormControl?.disabled;
|
136 | const required = requiredProp ?? control.props.required;
|
137 | const controlProps = {
|
138 | disabled,
|
139 | required
|
140 | };
|
141 | ['checked', 'name', 'onChange', 'value', 'inputRef'].forEach(key => {
|
142 | if (typeof control.props[key] === 'undefined' && typeof props[key] !== 'undefined') {
|
143 | controlProps[key] = props[key];
|
144 | }
|
145 | });
|
146 | const fcs = formControlState({
|
147 | props,
|
148 | muiFormControl,
|
149 | states: ['error']
|
150 | });
|
151 | const ownerState = {
|
152 | ...props,
|
153 | disabled,
|
154 | labelPlacement,
|
155 | required,
|
156 | error: fcs.error
|
157 | };
|
158 | const classes = useUtilityClasses(ownerState);
|
159 | const externalForwardedProps = {
|
160 | slots,
|
161 | slotProps: {
|
162 | ...componentsProps,
|
163 | ...slotProps
|
164 | }
|
165 | };
|
166 | const [TypographySlot, typographySlotProps] = useSlot('typography', {
|
167 | elementType: Typography,
|
168 | externalForwardedProps,
|
169 | ownerState
|
170 | });
|
171 | let label = labelProp;
|
172 | if (label != null && label.type !== Typography && !disableTypography) {
|
173 | label = _jsx(TypographySlot, {
|
174 | component: "span",
|
175 | ...typographySlotProps,
|
176 | className: clsx(classes.label, typographySlotProps?.className),
|
177 | children: label
|
178 | });
|
179 | }
|
180 | return _jsxs(FormControlLabelRoot, {
|
181 | className: clsx(classes.root, className),
|
182 | ownerState: ownerState,
|
183 | ref: ref,
|
184 | ...other,
|
185 | children: [React.cloneElement(control, controlProps), required ? _jsxs("div", {
|
186 | children: [label, _jsxs(AsteriskComponent, {
|
187 | ownerState: ownerState,
|
188 | "aria-hidden": true,
|
189 | className: classes.asterisk,
|
190 | children: ["\u2009", '*']
|
191 | })]
|
192 | }) : label]
|
193 | });
|
194 | });
|
195 | process.env.NODE_ENV !== "production" ? FormControlLabel.propTypes = {
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | |
201 |
|
202 |
|
203 | checked: PropTypes.bool,
|
204 | |
205 |
|
206 |
|
207 | classes: PropTypes.object,
|
208 | |
209 |
|
210 |
|
211 | className: PropTypes.string,
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 | componentsProps: PropTypes.shape({
|
218 | typography: PropTypes.object
|
219 | }),
|
220 | |
221 |
|
222 |
|
223 | control: PropTypes.element.isRequired,
|
224 | |
225 |
|
226 |
|
227 | disabled: PropTypes.bool,
|
228 | |
229 |
|
230 |
|
231 | disableTypography: PropTypes.bool,
|
232 | |
233 |
|
234 |
|
235 | inputRef: refType,
|
236 | |
237 |
|
238 |
|
239 | label: PropTypes.node,
|
240 | |
241 |
|
242 |
|
243 |
|
244 | labelPlacement: PropTypes.oneOf(['bottom', 'end', 'start', 'top']),
|
245 | |
246 |
|
247 |
|
248 | name: PropTypes.string,
|
249 | |
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | onChange: PropTypes.func,
|
256 | |
257 |
|
258 |
|
259 | required: PropTypes.bool,
|
260 | |
261 |
|
262 |
|
263 |
|
264 | slotProps: PropTypes.shape({
|
265 | typography: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
|
266 | }),
|
267 | |
268 |
|
269 |
|
270 |
|
271 | slots: PropTypes.shape({
|
272 | typography: PropTypes.elementType
|
273 | }),
|
274 | |
275 |
|
276 |
|
277 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
|
278 | |
279 |
|
280 |
|
281 | value: PropTypes.any
|
282 | } : void 0;
|
283 | export default FormControlLabel; |
\ | No newline at end of file |