UNPKG

8.79 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import composeClasses from '@mui/utils/composeClasses';
6import clsx from 'clsx';
7import formControlState from "../FormControl/formControlState.js";
8import useFormControl from "../FormControl/useFormControl.js";
9import FormLabel, { formLabelClasses } from "../FormLabel/index.js";
10import capitalize from "../utils/capitalize.js";
11import rootShouldForwardProp from "../styles/rootShouldForwardProp.js";
12import { styled } from "../zero-styled/index.js";
13import memoTheme from "../utils/memoTheme.js";
14import { useDefaultProps } from "../DefaultPropsProvider/index.js";
15import { getInputLabelUtilityClasses } from "./inputLabelClasses.js";
16import { jsx as _jsx } from "react/jsx-runtime";
17const useUtilityClasses = ownerState => {
18 const {
19 classes,
20 formControl,
21 size,
22 shrink,
23 disableAnimation,
24 variant,
25 required
26 } = ownerState;
27 const slots = {
28 root: ['root', formControl && 'formControl', !disableAnimation && 'animated', shrink && 'shrink', size && size !== 'normal' && `size${capitalize(size)}`, variant],
29 asterisk: [required && 'asterisk']
30 };
31 const composedClasses = composeClasses(slots, getInputLabelUtilityClasses, classes);
32 return {
33 ...classes,
34 // forward the focused, disabled, etc. classes to the FormLabel
35 ...composedClasses
36 };
37};
38const InputLabelRoot = styled(FormLabel, {
39 shouldForwardProp: prop => rootShouldForwardProp(prop) || prop === 'classes',
40 name: 'MuiInputLabel',
41 slot: 'Root',
42 overridesResolver: (props, styles) => {
43 const {
44 ownerState
45 } = props;
46 return [{
47 [`& .${formLabelClasses.asterisk}`]: styles.asterisk
48 }, styles.root, ownerState.formControl && styles.formControl, ownerState.size === 'small' && styles.sizeSmall, ownerState.shrink && styles.shrink, !ownerState.disableAnimation && styles.animated, ownerState.focused && styles.focused, styles[ownerState.variant]];
49 }
50})(memoTheme(({
51 theme
52}) => ({
53 display: 'block',
54 transformOrigin: 'top left',
55 whiteSpace: 'nowrap',
56 overflow: 'hidden',
57 textOverflow: 'ellipsis',
58 maxWidth: '100%',
59 variants: [{
60 props: ({
61 ownerState
62 }) => ownerState.formControl,
63 style: {
64 position: 'absolute',
65 left: 0,
66 top: 0,
67 // slight alteration to spec spacing to match visual spec result
68 transform: 'translate(0, 20px) scale(1)'
69 }
70 }, {
71 props: {
72 size: 'small'
73 },
74 style: {
75 // Compensation for the `Input.inputSizeSmall` style.
76 transform: 'translate(0, 17px) scale(1)'
77 }
78 }, {
79 props: ({
80 ownerState
81 }) => ownerState.shrink,
82 style: {
83 transform: 'translate(0, -1.5px) scale(0.75)',
84 transformOrigin: 'top left',
85 maxWidth: '133%'
86 }
87 }, {
88 props: ({
89 ownerState
90 }) => !ownerState.disableAnimation,
91 style: {
92 transition: theme.transitions.create(['color', 'transform', 'max-width'], {
93 duration: theme.transitions.duration.shorter,
94 easing: theme.transitions.easing.easeOut
95 })
96 }
97 }, {
98 props: {
99 variant: 'filled'
100 },
101 style: {
102 // Chrome's autofill feature gives the input field a yellow background.
103 // Since the input field is behind the label in the HTML tree,
104 // the input field is drawn last and hides the label with an opaque background color.
105 // zIndex: 1 will raise the label above opaque background-colors of input.
106 zIndex: 1,
107 pointerEvents: 'none',
108 transform: 'translate(12px, 16px) scale(1)',
109 maxWidth: 'calc(100% - 24px)'
110 }
111 }, {
112 props: {
113 variant: 'filled',
114 size: 'small'
115 },
116 style: {
117 transform: 'translate(12px, 13px) scale(1)'
118 }
119 }, {
120 props: ({
121 variant,
122 ownerState
123 }) => variant === 'filled' && ownerState.shrink,
124 style: {
125 userSelect: 'none',
126 pointerEvents: 'auto',
127 transform: 'translate(12px, 7px) scale(0.75)',
128 maxWidth: 'calc(133% - 24px)'
129 }
130 }, {
131 props: ({
132 variant,
133 ownerState,
134 size
135 }) => variant === 'filled' && ownerState.shrink && size === 'small',
136 style: {
137 transform: 'translate(12px, 4px) scale(0.75)'
138 }
139 }, {
140 props: {
141 variant: 'outlined'
142 },
143 style: {
144 // see comment above on filled.zIndex
145 zIndex: 1,
146 pointerEvents: 'none',
147 transform: 'translate(14px, 16px) scale(1)',
148 maxWidth: 'calc(100% - 24px)'
149 }
150 }, {
151 props: {
152 variant: 'outlined',
153 size: 'small'
154 },
155 style: {
156 transform: 'translate(14px, 9px) scale(1)'
157 }
158 }, {
159 props: ({
160 variant,
161 ownerState
162 }) => variant === 'outlined' && ownerState.shrink,
163 style: {
164 userSelect: 'none',
165 pointerEvents: 'auto',
166 // Theoretically, we should have (8+5)*2/0.75 = 34px
167 // but it feels a better when it bleeds a bit on the left, so 32px.
168 maxWidth: 'calc(133% - 32px)',
169 transform: 'translate(14px, -9px) scale(0.75)'
170 }
171 }]
172})));
173const InputLabel = /*#__PURE__*/React.forwardRef(function InputLabel(inProps, ref) {
174 const props = useDefaultProps({
175 name: 'MuiInputLabel',
176 props: inProps
177 });
178 const {
179 disableAnimation = false,
180 margin,
181 shrink: shrinkProp,
182 variant,
183 className,
184 ...other
185 } = props;
186 const muiFormControl = useFormControl();
187 let shrink = shrinkProp;
188 if (typeof shrink === 'undefined' && muiFormControl) {
189 shrink = muiFormControl.filled || muiFormControl.focused || muiFormControl.adornedStart;
190 }
191 const fcs = formControlState({
192 props,
193 muiFormControl,
194 states: ['size', 'variant', 'required', 'focused']
195 });
196 const ownerState = {
197 ...props,
198 disableAnimation,
199 formControl: muiFormControl,
200 shrink,
201 size: fcs.size,
202 variant: fcs.variant,
203 required: fcs.required,
204 focused: fcs.focused
205 };
206 const classes = useUtilityClasses(ownerState);
207 return /*#__PURE__*/_jsx(InputLabelRoot, {
208 "data-shrink": shrink,
209 ref: ref,
210 className: clsx(classes.root, className),
211 ...other,
212 ownerState: ownerState,
213 classes: classes
214 });
215});
216process.env.NODE_ENV !== "production" ? InputLabel.propTypes /* remove-proptypes */ = {
217 // ┌────────────────────────────── Warning ──────────────────────────────┐
218 // │ These PropTypes are generated from the TypeScript type definitions. │
219 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
220 // └─────────────────────────────────────────────────────────────────────┘
221 /**
222 * The content of the component.
223 */
224 children: PropTypes.node,
225 /**
226 * Override or extend the styles applied to the component.
227 */
228 classes: PropTypes.object,
229 /**
230 * @ignore
231 */
232 className: PropTypes.string,
233 /**
234 * The color of the component.
235 * It supports both default and custom theme colors, which can be added as shown in the
236 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
237 */
238 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['error', 'info', 'primary', 'secondary', 'success', 'warning']), PropTypes.string]),
239 /**
240 * If `true`, the transition animation is disabled.
241 * @default false
242 */
243 disableAnimation: PropTypes.bool,
244 /**
245 * If `true`, the component is disabled.
246 */
247 disabled: PropTypes.bool,
248 /**
249 * If `true`, the label is displayed in an error state.
250 */
251 error: PropTypes.bool,
252 /**
253 * If `true`, the `input` of this label is focused.
254 */
255 focused: PropTypes.bool,
256 /**
257 * If `dense`, will adjust vertical spacing. This is normally obtained via context from
258 * FormControl.
259 */
260 margin: PropTypes.oneOf(['dense']),
261 /**
262 * if `true`, the label will indicate that the `input` is required.
263 */
264 required: PropTypes.bool,
265 /**
266 * If `true`, the label is shrunk.
267 */
268 shrink: PropTypes.bool,
269 /**
270 * The size of the component.
271 * @default 'normal'
272 */
273 size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['normal', 'small']), PropTypes.string]),
274 /**
275 * The system prop that allows defining system overrides as well as additional CSS styles.
276 */
277 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
278 /**
279 * The variant to use.
280 */
281 variant: PropTypes.oneOf(['filled', 'outlined', 'standard'])
282} : void 0;
283export default InputLabel;
\No newline at end of file