UNPKG

5.95 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import composeClasses from '@mui/utils/composeClasses';
7import formControlState from "../FormControl/formControlState.js";
8import useFormControl from "../FormControl/useFormControl.js";
9import capitalize from "../utils/capitalize.js";
10import { styled } from "../zero-styled/index.js";
11import memoTheme from "../utils/memoTheme.js";
12import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
13import { useDefaultProps } from "../DefaultPropsProvider/index.js";
14import formLabelClasses, { getFormLabelUtilityClasses } from "./formLabelClasses.js";
15import { jsxs as _jsxs } from "react/jsx-runtime";
16const 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};
32export 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})));
73const 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})));
84const FormLabel = /*#__PURE__*/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 /*#__PURE__*/_jsxs(FormLabelRoot, {
119 as: component,
120 ownerState: ownerState,
121 className: clsx(classes.root, className),
122 ref: ref,
123 ...other,
124 children: [children, fcs.required && /*#__PURE__*/_jsxs(AsteriskComponent, {
125 ownerState: ownerState,
126 "aria-hidden": true,
127 className: classes.asterisk,
128 children: ["\u2009", '*']
129 })]
130 });
131});
132process.env.NODE_ENV !== "production" ? FormLabel.propTypes /* remove-proptypes */ = {
133 // ┌────────────────────────────── Warning ──────────────────────────────┐
134 // │ These PropTypes are generated from the TypeScript type definitions. │
135 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
136 // └─────────────────────────────────────────────────────────────────────┘
137 /**
138 * The content of the component.
139 */
140 children: PropTypes.node,
141 /**
142 * Override or extend the styles applied to the component.
143 */
144 classes: PropTypes.object,
145 /**
146 * @ignore
147 */
148 className: PropTypes.string,
149 /**
150 * The color of the component.
151 * It supports both default and custom theme colors, which can be added as shown in the
152 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
153 */
154 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['error', 'info', 'primary', 'secondary', 'success', 'warning']), PropTypes.string]),
155 /**
156 * The component used for the root node.
157 * Either a string to use a HTML element or a component.
158 */
159 component: PropTypes.elementType,
160 /**
161 * If `true`, the label should be displayed in a disabled state.
162 */
163 disabled: PropTypes.bool,
164 /**
165 * If `true`, the label is displayed in an error state.
166 */
167 error: PropTypes.bool,
168 /**
169 * If `true`, the label should use filled classes key.
170 */
171 filled: PropTypes.bool,
172 /**
173 * If `true`, the input of this label is focused (used by `FormGroup` components).
174 */
175 focused: PropTypes.bool,
176 /**
177 * If `true`, the label will indicate that the `input` is required.
178 */
179 required: PropTypes.bool,
180 /**
181 * The system prop that allows defining system overrides as well as additional CSS styles.
182 */
183 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
184} : void 0;
185export default FormLabel;
\No newline at end of file