UNPKG

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