1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import composeClasses from '@mui/utils/composeClasses';
|
7 | import formControlState from "../FormControl/formControlState.js";
|
8 | import useFormControl from "../FormControl/useFormControl.js";
|
9 | import capitalize from "../utils/capitalize.js";
|
10 | import { styled } from "../zero-styled/index.js";
|
11 | import memoTheme from "../utils/memoTheme.js";
|
12 | import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
|
13 | import { useDefaultProps } from "../DefaultPropsProvider/index.js";
|
14 | import formLabelClasses, { getFormLabelUtilityClasses } from "./formLabelClasses.js";
|
15 | import { jsxs as _jsxs } from "react/jsx-runtime";
|
16 | const useUtilityClasses = ownerState => {
|
17 | const {
|
18 | classes,
|
19 | color,
|
20 | focused,
|
21 | disabled,
|
22 | error,
|
23 | filled,
|
24 | required
|
25 | } = ownerState;
|
26 | const slots = {
|
27 | root: ['root', `color${capitalize(color)}`, disabled && 'disabled', error && 'error', filled && 'filled', focused && 'focused', required && 'required'],
|
28 | asterisk: ['asterisk', error && 'error']
|
29 | };
|
30 | return composeClasses(slots, getFormLabelUtilityClasses, classes);
|
31 | };
|
32 | export const FormLabelRoot = styled('label', {
|
33 | name: 'MuiFormLabel',
|
34 | slot: 'Root',
|
35 | overridesResolver: ({
|
36 | ownerState
|
37 | }, styles) => {
|
38 | return {
|
39 | ...styles.root,
|
40 | ...(ownerState.color === 'secondary' && styles.colorSecondary),
|
41 | ...(ownerState.filled && styles.filled)
|
42 | };
|
43 | }
|
44 | })(memoTheme(({
|
45 | theme
|
46 | }) => ({
|
47 | color: (theme.vars || theme).palette.text.secondary,
|
48 | ...theme.typography.body1,
|
49 | lineHeight: '1.4375em',
|
50 | padding: 0,
|
51 | position: 'relative',
|
52 | variants: [...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()).map(([color]) => ({
|
53 | props: {
|
54 | color
|
55 | },
|
56 | style: {
|
57 | [`&.${formLabelClasses.focused}`]: {
|
58 | color: (theme.vars || theme).palette[color].main
|
59 | }
|
60 | }
|
61 | })), {
|
62 | props: {},
|
63 | style: {
|
64 | [`&.${formLabelClasses.disabled}`]: {
|
65 | color: (theme.vars || theme).palette.text.disabled
|
66 | },
|
67 | [`&.${formLabelClasses.error}`]: {
|
68 | color: (theme.vars || theme).palette.error.main
|
69 | }
|
70 | }
|
71 | }]
|
72 | })));
|
73 | const AsteriskComponent = styled('span', {
|
74 | name: 'MuiFormLabel',
|
75 | slot: 'Asterisk',
|
76 | overridesResolver: (props, styles) => styles.asterisk
|
77 | })(memoTheme(({
|
78 | theme
|
79 | }) => ({
|
80 | [`&.${formLabelClasses.error}`]: {
|
81 | color: (theme.vars || theme).palette.error.main
|
82 | }
|
83 | })));
|
84 | const FormLabel = React.forwardRef(function FormLabel(inProps, ref) {
|
85 | const props = useDefaultProps({
|
86 | props: inProps,
|
87 | name: 'MuiFormLabel'
|
88 | });
|
89 | const {
|
90 | children,
|
91 | className,
|
92 | color,
|
93 | component = 'label',
|
94 | disabled,
|
95 | error,
|
96 | filled,
|
97 | focused,
|
98 | required,
|
99 | ...other
|
100 | } = props;
|
101 | const muiFormControl = useFormControl();
|
102 | const fcs = formControlState({
|
103 | props,
|
104 | muiFormControl,
|
105 | states: ['color', 'required', 'focused', 'disabled', 'error', 'filled']
|
106 | });
|
107 | const ownerState = {
|
108 | ...props,
|
109 | color: fcs.color || 'primary',
|
110 | component,
|
111 | disabled: fcs.disabled,
|
112 | error: fcs.error,
|
113 | filled: fcs.filled,
|
114 | focused: fcs.focused,
|
115 | required: fcs.required
|
116 | };
|
117 | const classes = useUtilityClasses(ownerState);
|
118 | return _jsxs(FormLabelRoot, {
|
119 | as: component,
|
120 | ownerState: ownerState,
|
121 | className: clsx(classes.root, className),
|
122 | ref: ref,
|
123 | ...other,
|
124 | children: [children, fcs.required && _jsxs(AsteriskComponent, {
|
125 | ownerState: ownerState,
|
126 | "aria-hidden": true,
|
127 | className: classes.asterisk,
|
128 | children: ["\u2009", '*']
|
129 | })]
|
130 | });
|
131 | });
|
132 | process.env.NODE_ENV !== "production" ? FormLabel.propTypes = {
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | |
138 |
|
139 |
|
140 | children: PropTypes.node,
|
141 | |
142 |
|
143 |
|
144 | classes: PropTypes.object,
|
145 | |
146 |
|
147 |
|
148 | className: PropTypes.string,
|
149 | |
150 |
|
151 |
|
152 |
|
153 |
|
154 | color: PropTypes .oneOfType([PropTypes.oneOf(['error', 'info', 'primary', 'secondary', 'success', 'warning']), PropTypes.string]),
|
155 | |
156 |
|
157 |
|
158 |
|
159 | component: PropTypes.elementType,
|
160 | |
161 |
|
162 |
|
163 | disabled: PropTypes.bool,
|
164 | |
165 |
|
166 |
|
167 | error: PropTypes.bool,
|
168 | |
169 |
|
170 |
|
171 | filled: PropTypes.bool,
|
172 | |
173 |
|
174 |
|
175 | focused: PropTypes.bool,
|
176 | |
177 |
|
178 |
|
179 | required: PropTypes.bool,
|
180 | |
181 |
|
182 |
|
183 | sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
|
184 | } : void 0;
|
185 | export default FormLabel; |
\ | No newline at end of file |