UNPKG

10.8 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
2import _extends from "@babel/runtime/helpers/esm/extends";
3const _excluded = ["disableUnderline", "components", "componentsProps", "fullWidth", "inputComponent", "multiline", "type"];
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import { unstable_composeClasses as composeClasses } from '@mui/base';
7import { refType, deepmerge } from '@mui/utils';
8import InputBase from '../InputBase';
9import styled, { rootShouldForwardProp } from '../styles/styled';
10import useThemeProps from '../styles/useThemeProps';
11import inputClasses, { getInputUtilityClass } from './inputClasses';
12import { rootOverridesResolver as inputBaseRootOverridesResolver, inputOverridesResolver as inputBaseInputOverridesResolver, InputBaseRoot, InputBaseComponent as InputBaseInput } from '../InputBase/InputBase';
13import { jsx as _jsx } from "react/jsx-runtime";
14
15const useUtilityClasses = ownerState => {
16 const {
17 classes,
18 disableUnderline
19 } = ownerState;
20 const slots = {
21 root: ['root', !disableUnderline && 'underline'],
22 input: ['input']
23 };
24 const composedClasses = composeClasses(slots, getInputUtilityClass, classes);
25 return _extends({}, classes, composedClasses);
26};
27
28const InputRoot = styled(InputBaseRoot, {
29 shouldForwardProp: prop => rootShouldForwardProp(prop) || prop === 'classes',
30 name: 'MuiInput',
31 slot: 'Root',
32 overridesResolver: (props, styles) => {
33 const {
34 ownerState
35 } = props;
36 return [...inputBaseRootOverridesResolver(props, styles), !ownerState.disableUnderline && styles.underline];
37 }
38})(({
39 theme,
40 ownerState
41}) => {
42 const light = theme.palette.mode === 'light';
43 let bottomLineColor = light ? 'rgba(0, 0, 0, 0.42)' : 'rgba(255, 255, 255, 0.7)';
44
45 if (theme.vars) {
46 bottomLineColor = `rgba(${theme.vars.palette.common.onBackgroundChannel} / ${theme.vars.opacity.inputUnderline})`;
47 }
48
49 return _extends({
50 position: 'relative'
51 }, ownerState.formControl && {
52 'label + &': {
53 marginTop: 16
54 }
55 }, !ownerState.disableUnderline && {
56 '&:after': {
57 borderBottom: `2px solid ${(theme.vars || theme).palette[ownerState.color].main}`,
58 left: 0,
59 bottom: 0,
60 // Doing the other way around crash on IE11 "''" https://github.com/cssinjs/jss/issues/242
61 content: '""',
62 position: 'absolute',
63 right: 0,
64 transform: 'scaleX(0)',
65 transition: theme.transitions.create('transform', {
66 duration: theme.transitions.duration.shorter,
67 easing: theme.transitions.easing.easeOut
68 }),
69 pointerEvents: 'none' // Transparent to the hover style.
70
71 },
72 [`&.${inputClasses.focused}:after`]: {
73 // translateX(0) is a workaround for Safari transform scale bug
74 // See https://github.com/mui/material-ui/issues/31766
75 transform: 'scaleX(1) translateX(0)'
76 },
77 [`&.${inputClasses.error}:after`]: {
78 borderBottomColor: (theme.vars || theme).palette.error.main,
79 transform: 'scaleX(1)' // error is always underlined in red
80
81 },
82 '&:before': {
83 borderBottom: `1px solid ${bottomLineColor}`,
84 left: 0,
85 bottom: 0,
86 // Doing the other way around crash on IE11 "''" https://github.com/cssinjs/jss/issues/242
87 content: '"\\00a0"',
88 position: 'absolute',
89 right: 0,
90 transition: theme.transitions.create('border-bottom-color', {
91 duration: theme.transitions.duration.shorter
92 }),
93 pointerEvents: 'none' // Transparent to the hover style.
94
95 },
96 [`&:hover:not(.${inputClasses.disabled}):before`]: {
97 borderBottom: `2px solid ${(theme.vars || theme).palette.text.primary}`,
98 // Reset on touch devices, it doesn't add specificity
99 '@media (hover: none)': {
100 borderBottom: `1px solid ${bottomLineColor}`
101 }
102 },
103 [`&.${inputClasses.disabled}:before`]: {
104 borderBottomStyle: 'dotted'
105 }
106 });
107});
108const InputInput = styled(InputBaseInput, {
109 name: 'MuiInput',
110 slot: 'Input',
111 overridesResolver: inputBaseInputOverridesResolver
112})({});
113const Input = /*#__PURE__*/React.forwardRef(function Input(inProps, ref) {
114 const props = useThemeProps({
115 props: inProps,
116 name: 'MuiInput'
117 });
118
119 const {
120 disableUnderline,
121 components = {},
122 componentsProps: componentsPropsProp,
123 fullWidth = false,
124 inputComponent = 'input',
125 multiline = false,
126 type = 'text'
127 } = props,
128 other = _objectWithoutPropertiesLoose(props, _excluded);
129
130 const classes = useUtilityClasses(props);
131 const ownerState = {
132 disableUnderline
133 };
134 const inputComponentsProps = {
135 root: {
136 ownerState
137 }
138 };
139 const componentsProps = componentsPropsProp ? deepmerge(componentsPropsProp, inputComponentsProps) : inputComponentsProps;
140 return /*#__PURE__*/_jsx(InputBase, _extends({
141 components: _extends({
142 Root: InputRoot,
143 Input: InputInput
144 }, components),
145 componentsProps: componentsProps,
146 fullWidth: fullWidth,
147 inputComponent: inputComponent,
148 multiline: multiline,
149 ref: ref,
150 type: type
151 }, other, {
152 classes: classes
153 }));
154});
155process.env.NODE_ENV !== "production" ? Input.propTypes
156/* remove-proptypes */
157= {
158 // ----------------------------- Warning --------------------------------
159 // | These PropTypes are generated from the TypeScript type definitions |
160 // | To update them edit the d.ts file and run "yarn proptypes" |
161 // ----------------------------------------------------------------------
162
163 /**
164 * This prop helps users to fill forms faster, especially on mobile devices.
165 * The name can be confusing, as it's more like an autofill.
166 * You can learn more about it [following the specification](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).
167 */
168 autoComplete: PropTypes.string,
169
170 /**
171 * If `true`, the `input` element is focused during the first mount.
172 */
173 autoFocus: PropTypes.bool,
174
175 /**
176 * Override or extend the styles applied to the component.
177 */
178 classes: PropTypes.object,
179
180 /**
181 * The color of the component.
182 * It supports both default and custom theme colors, which can be added as shown in the
183 * [palette customization guide](https://mui.com/material-ui/customization/palette/#adding-new-colors).
184 * The prop defaults to the value (`'primary'`) inherited from the parent FormControl component.
185 */
186 color: PropTypes
187 /* @typescript-to-proptypes-ignore */
188 .oneOfType([PropTypes.oneOf(['primary', 'secondary']), PropTypes.string]),
189
190 /**
191 * The components used for each slot inside the InputBase.
192 * Either a string to use a HTML element or a component.
193 * @default {}
194 */
195 components: PropTypes.shape({
196 Input: PropTypes.elementType,
197 Root: PropTypes.elementType
198 }),
199
200 /**
201 * The props used for each slot inside the Input.
202 * @default {}
203 */
204 componentsProps: PropTypes.shape({
205 input: PropTypes.object,
206 root: PropTypes.object
207 }),
208
209 /**
210 * The default value. Use when the component is not controlled.
211 */
212 defaultValue: PropTypes.any,
213
214 /**
215 * If `true`, the component is disabled.
216 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
217 */
218 disabled: PropTypes.bool,
219
220 /**
221 * If `true`, the `input` will not have an underline.
222 */
223 disableUnderline: PropTypes.bool,
224
225 /**
226 * End `InputAdornment` for this component.
227 */
228 endAdornment: PropTypes.node,
229
230 /**
231 * If `true`, the `input` will indicate an error.
232 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
233 */
234 error: PropTypes.bool,
235
236 /**
237 * If `true`, the `input` will take up the full width of its container.
238 * @default false
239 */
240 fullWidth: PropTypes.bool,
241
242 /**
243 * The id of the `input` element.
244 */
245 id: PropTypes.string,
246
247 /**
248 * The component used for the `input` element.
249 * Either a string to use a HTML element or a component.
250 * @default 'input'
251 */
252 inputComponent: PropTypes.elementType,
253
254 /**
255 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
256 * @default {}
257 */
258 inputProps: PropTypes.object,
259
260 /**
261 * Pass a ref to the `input` element.
262 */
263 inputRef: refType,
264
265 /**
266 * If `dense`, will adjust vertical spacing. This is normally obtained via context from
267 * FormControl.
268 * The prop defaults to the value (`'none'`) inherited from the parent FormControl component.
269 */
270 margin: PropTypes.oneOf(['dense', 'none']),
271
272 /**
273 * Maximum number of rows to display when multiline option is set to true.
274 */
275 maxRows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
276
277 /**
278 * Minimum number of rows to display when multiline option is set to true.
279 */
280 minRows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
281
282 /**
283 * If `true`, a [TextareaAutosize](/material-ui/react-textarea-autosize/) element is rendered.
284 * @default false
285 */
286 multiline: PropTypes.bool,
287
288 /**
289 * Name attribute of the `input` element.
290 */
291 name: PropTypes.string,
292
293 /**
294 * Callback fired when the value is changed.
295 *
296 * @param {React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>} event The event source of the callback.
297 * You can pull out the new value by accessing `event.target.value` (string).
298 */
299 onChange: PropTypes.func,
300
301 /**
302 * The short hint displayed in the `input` before the user enters a value.
303 */
304 placeholder: PropTypes.string,
305
306 /**
307 * It prevents the user from changing the value of the field
308 * (not from interacting with the field).
309 */
310 readOnly: PropTypes.bool,
311
312 /**
313 * If `true`, the `input` element is required.
314 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
315 */
316 required: PropTypes.bool,
317
318 /**
319 * Number of rows to display when multiline option is set to true.
320 */
321 rows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
322
323 /**
324 * Start `InputAdornment` for this component.
325 */
326 startAdornment: PropTypes.node,
327
328 /**
329 * The system prop that allows defining system overrides as well as additional CSS styles.
330 */
331 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
332
333 /**
334 * Type of the `input` element. It should be [a valid HTML5 input type](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Form_%3Cinput%3E_types).
335 * @default 'text'
336 */
337 type: PropTypes.string,
338
339 /**
340 * The value of the `input` element, required for a controlled component.
341 */
342 value: PropTypes.any
343} : void 0;
344Input.muiName = 'Input';
345export default Input;
\No newline at end of file