UNPKG

17 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import deepmerge from '@mui/utils/deepmerge';
5import refType from '@mui/utils/refType';
6import PropTypes from 'prop-types';
7import composeClasses from '@mui/utils/composeClasses';
8import InputBase from "../InputBase/index.js";
9import rootShouldForwardProp from "../styles/rootShouldForwardProp.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 filledInputClasses, { getFilledInputUtilityClass } from "./filledInputClasses.js";
15import { rootOverridesResolver as inputBaseRootOverridesResolver, inputOverridesResolver as inputBaseInputOverridesResolver, InputBaseRoot, InputBaseInput } from "../InputBase/InputBase.js";
16import { capitalize } from "../utils/index.js";
17import { jsx as _jsx } from "react/jsx-runtime";
18const useUtilityClasses = ownerState => {
19 const {
20 classes,
21 disableUnderline,
22 startAdornment,
23 endAdornment,
24 size,
25 hiddenLabel,
26 multiline
27 } = ownerState;
28 const slots = {
29 root: ['root', !disableUnderline && 'underline', startAdornment && 'adornedStart', endAdornment && 'adornedEnd', size === 'small' && `size${capitalize(size)}`, hiddenLabel && 'hiddenLabel', multiline && 'multiline'],
30 input: ['input']
31 };
32 const composedClasses = composeClasses(slots, getFilledInputUtilityClass, classes);
33 return {
34 ...classes,
35 // forward classes to the InputBase
36 ...composedClasses
37 };
38};
39const FilledInputRoot = styled(InputBaseRoot, {
40 shouldForwardProp: prop => rootShouldForwardProp(prop) || prop === 'classes',
41 name: 'MuiFilledInput',
42 slot: 'Root',
43 overridesResolver: (props, styles) => {
44 const {
45 ownerState
46 } = props;
47 return [...inputBaseRootOverridesResolver(props, styles), !ownerState.disableUnderline && styles.underline];
48 }
49})(memoTheme(({
50 theme
51}) => {
52 const light = theme.palette.mode === 'light';
53 const bottomLineColor = light ? 'rgba(0, 0, 0, 0.42)' : 'rgba(255, 255, 255, 0.7)';
54 const backgroundColor = light ? 'rgba(0, 0, 0, 0.06)' : 'rgba(255, 255, 255, 0.09)';
55 const hoverBackground = light ? 'rgba(0, 0, 0, 0.09)' : 'rgba(255, 255, 255, 0.13)';
56 const disabledBackground = light ? 'rgba(0, 0, 0, 0.12)' : 'rgba(255, 255, 255, 0.12)';
57 return {
58 position: 'relative',
59 backgroundColor: theme.vars ? theme.vars.palette.FilledInput.bg : backgroundColor,
60 borderTopLeftRadius: (theme.vars || theme).shape.borderRadius,
61 borderTopRightRadius: (theme.vars || theme).shape.borderRadius,
62 transition: theme.transitions.create('background-color', {
63 duration: theme.transitions.duration.shorter,
64 easing: theme.transitions.easing.easeOut
65 }),
66 '&:hover': {
67 backgroundColor: theme.vars ? theme.vars.palette.FilledInput.hoverBg : hoverBackground,
68 // Reset on touch devices, it doesn't add specificity
69 '@media (hover: none)': {
70 backgroundColor: theme.vars ? theme.vars.palette.FilledInput.bg : backgroundColor
71 }
72 },
73 [`&.${filledInputClasses.focused}`]: {
74 backgroundColor: theme.vars ? theme.vars.palette.FilledInput.bg : backgroundColor
75 },
76 [`&.${filledInputClasses.disabled}`]: {
77 backgroundColor: theme.vars ? theme.vars.palette.FilledInput.disabledBg : disabledBackground
78 },
79 variants: [{
80 props: ({
81 ownerState
82 }) => !ownerState.disableUnderline,
83 style: {
84 '&::after': {
85 left: 0,
86 bottom: 0,
87 content: '""',
88 position: 'absolute',
89 right: 0,
90 transform: 'scaleX(0)',
91 transition: theme.transitions.create('transform', {
92 duration: theme.transitions.duration.shorter,
93 easing: theme.transitions.easing.easeOut
94 }),
95 pointerEvents: 'none' // Transparent to the hover style.
96 },
97 [`&.${filledInputClasses.focused}:after`]: {
98 // translateX(0) is a workaround for Safari transform scale bug
99 // See https://github.com/mui/material-ui/issues/31766
100 transform: 'scaleX(1) translateX(0)'
101 },
102 [`&.${filledInputClasses.error}`]: {
103 '&::before, &::after': {
104 borderBottomColor: (theme.vars || theme).palette.error.main
105 }
106 },
107 '&::before': {
108 borderBottom: `1px solid ${theme.vars ? `rgba(${theme.vars.palette.common.onBackgroundChannel} / ${theme.vars.opacity.inputUnderline})` : bottomLineColor}`,
109 left: 0,
110 bottom: 0,
111 content: '"\\00a0"',
112 position: 'absolute',
113 right: 0,
114 transition: theme.transitions.create('border-bottom-color', {
115 duration: theme.transitions.duration.shorter
116 }),
117 pointerEvents: 'none' // Transparent to the hover style.
118 },
119 [`&:hover:not(.${filledInputClasses.disabled}, .${filledInputClasses.error}):before`]: {
120 borderBottom: `1px solid ${(theme.vars || theme).palette.text.primary}`
121 },
122 [`&.${filledInputClasses.disabled}:before`]: {
123 borderBottomStyle: 'dotted'
124 }
125 }
126 }, ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()) // check all the used fields in the style below
127 .map(([color]) => ({
128 props: {
129 disableUnderline: false,
130 color
131 },
132 style: {
133 '&::after': {
134 borderBottom: `2px solid ${(theme.vars || theme).palette[color]?.main}`
135 }
136 }
137 })), {
138 props: ({
139 ownerState
140 }) => ownerState.startAdornment,
141 style: {
142 paddingLeft: 12
143 }
144 }, {
145 props: ({
146 ownerState
147 }) => ownerState.endAdornment,
148 style: {
149 paddingRight: 12
150 }
151 }, {
152 props: ({
153 ownerState
154 }) => ownerState.multiline,
155 style: {
156 padding: '25px 12px 8px'
157 }
158 }, {
159 props: ({
160 ownerState,
161 size
162 }) => ownerState.multiline && size === 'small',
163 style: {
164 paddingTop: 21,
165 paddingBottom: 4
166 }
167 }, {
168 props: ({
169 ownerState
170 }) => ownerState.multiline && ownerState.hiddenLabel,
171 style: {
172 paddingTop: 16,
173 paddingBottom: 17
174 }
175 }, {
176 props: ({
177 ownerState
178 }) => ownerState.multiline && ownerState.hiddenLabel && ownerState.size === 'small',
179 style: {
180 paddingTop: 8,
181 paddingBottom: 9
182 }
183 }]
184 };
185}));
186const FilledInputInput = styled(InputBaseInput, {
187 name: 'MuiFilledInput',
188 slot: 'Input',
189 overridesResolver: inputBaseInputOverridesResolver
190})(memoTheme(({
191 theme
192}) => ({
193 paddingTop: 25,
194 paddingRight: 12,
195 paddingBottom: 8,
196 paddingLeft: 12,
197 ...(!theme.vars && {
198 '&:-webkit-autofill': {
199 WebkitBoxShadow: theme.palette.mode === 'light' ? null : '0 0 0 100px #266798 inset',
200 WebkitTextFillColor: theme.palette.mode === 'light' ? null : '#fff',
201 caretColor: theme.palette.mode === 'light' ? null : '#fff',
202 borderTopLeftRadius: 'inherit',
203 borderTopRightRadius: 'inherit'
204 }
205 }),
206 ...(theme.vars && {
207 '&:-webkit-autofill': {
208 borderTopLeftRadius: 'inherit',
209 borderTopRightRadius: 'inherit'
210 },
211 [theme.getColorSchemeSelector('dark')]: {
212 '&:-webkit-autofill': {
213 WebkitBoxShadow: '0 0 0 100px #266798 inset',
214 WebkitTextFillColor: '#fff',
215 caretColor: '#fff'
216 }
217 }
218 }),
219 variants: [{
220 props: {
221 size: 'small'
222 },
223 style: {
224 paddingTop: 21,
225 paddingBottom: 4
226 }
227 }, {
228 props: ({
229 ownerState
230 }) => ownerState.hiddenLabel,
231 style: {
232 paddingTop: 16,
233 paddingBottom: 17
234 }
235 }, {
236 props: ({
237 ownerState
238 }) => ownerState.startAdornment,
239 style: {
240 paddingLeft: 0
241 }
242 }, {
243 props: ({
244 ownerState
245 }) => ownerState.endAdornment,
246 style: {
247 paddingRight: 0
248 }
249 }, {
250 props: ({
251 ownerState
252 }) => ownerState.hiddenLabel && ownerState.size === 'small',
253 style: {
254 paddingTop: 8,
255 paddingBottom: 9
256 }
257 }, {
258 props: ({
259 ownerState
260 }) => ownerState.multiline,
261 style: {
262 paddingTop: 0,
263 paddingBottom: 0,
264 paddingLeft: 0,
265 paddingRight: 0
266 }
267 }]
268})));
269const FilledInput = /*#__PURE__*/React.forwardRef(function FilledInput(inProps, ref) {
270 const props = useDefaultProps({
271 props: inProps,
272 name: 'MuiFilledInput'
273 });
274 const {
275 disableUnderline = false,
276 components = {},
277 componentsProps: componentsPropsProp,
278 fullWidth = false,
279 hiddenLabel,
280 // declare here to prevent spreading to DOM
281 inputComponent = 'input',
282 multiline = false,
283 slotProps,
284 slots = {},
285 type = 'text',
286 ...other
287 } = props;
288 const ownerState = {
289 ...props,
290 disableUnderline,
291 fullWidth,
292 inputComponent,
293 multiline,
294 type
295 };
296 const classes = useUtilityClasses(props);
297 const filledInputComponentsProps = {
298 root: {
299 ownerState
300 },
301 input: {
302 ownerState
303 }
304 };
305 const componentsProps = slotProps ?? componentsPropsProp ? deepmerge(filledInputComponentsProps, slotProps ?? componentsPropsProp) : filledInputComponentsProps;
306 const RootSlot = slots.root ?? components.Root ?? FilledInputRoot;
307 const InputSlot = slots.input ?? components.Input ?? FilledInputInput;
308 return /*#__PURE__*/_jsx(InputBase, {
309 slots: {
310 root: RootSlot,
311 input: InputSlot
312 },
313 componentsProps: componentsProps,
314 fullWidth: fullWidth,
315 inputComponent: inputComponent,
316 multiline: multiline,
317 ref: ref,
318 type: type,
319 ...other,
320 classes: classes
321 });
322});
323process.env.NODE_ENV !== "production" ? FilledInput.propTypes /* remove-proptypes */ = {
324 // ┌────────────────────────────── Warning ──────────────────────────────┐
325 // │ These PropTypes are generated from the TypeScript type definitions. │
326 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
327 // └─────────────────────────────────────────────────────────────────────┘
328 /**
329 * This prop helps users to fill forms faster, especially on mobile devices.
330 * The name can be confusing, as it's more like an autofill.
331 * You can learn more about it [following the specification](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).
332 */
333 autoComplete: PropTypes.string,
334 /**
335 * If `true`, the `input` element is focused during the first mount.
336 */
337 autoFocus: PropTypes.bool,
338 /**
339 * Override or extend the styles applied to the component.
340 */
341 classes: PropTypes.object,
342 /**
343 * The color of the component.
344 * It supports both default and custom theme colors, which can be added as shown in the
345 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
346 * The prop defaults to the value (`'primary'`) inherited from the parent FormControl component.
347 */
348 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['primary', 'secondary']), PropTypes.string]),
349 /**
350 * The components used for each slot inside.
351 *
352 * @deprecated use the `slots` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
353 *
354 * @default {}
355 */
356 components: PropTypes.shape({
357 Input: PropTypes.elementType,
358 Root: PropTypes.elementType
359 }),
360 /**
361 * The extra props for the slot components.
362 * You can override the existing props or add new ones.
363 *
364 * @deprecated use the `slotProps` prop instead. This prop will be removed in v7. See [Migrating from deprecated APIs](https://mui.com/material-ui/migration/migrating-from-deprecated-apis/) for more details.
365 *
366 * @default {}
367 */
368 componentsProps: PropTypes.shape({
369 input: PropTypes.object,
370 root: PropTypes.object
371 }),
372 /**
373 * The default value. Use when the component is not controlled.
374 */
375 defaultValue: PropTypes.any,
376 /**
377 * If `true`, the component is disabled.
378 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
379 */
380 disabled: PropTypes.bool,
381 /**
382 * If `true`, the input will not have an underline.
383 * @default false
384 */
385 disableUnderline: PropTypes.bool,
386 /**
387 * End `InputAdornment` for this component.
388 */
389 endAdornment: PropTypes.node,
390 /**
391 * If `true`, the `input` will indicate an error.
392 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
393 */
394 error: PropTypes.bool,
395 /**
396 * If `true`, the `input` will take up the full width of its container.
397 * @default false
398 */
399 fullWidth: PropTypes.bool,
400 /**
401 * If `true`, the label is hidden.
402 * This is used to increase density for a `FilledInput`.
403 * Be sure to add `aria-label` to the `input` element.
404 * @default false
405 */
406 hiddenLabel: PropTypes.bool,
407 /**
408 * The id of the `input` element.
409 */
410 id: PropTypes.string,
411 /**
412 * The component used for the `input` element.
413 * Either a string to use a HTML element or a component.
414 * @default 'input'
415 */
416 inputComponent: PropTypes.elementType,
417 /**
418 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
419 * @default {}
420 */
421 inputProps: PropTypes.object,
422 /**
423 * Pass a ref to the `input` element.
424 */
425 inputRef: refType,
426 /**
427 * If `dense`, will adjust vertical spacing. This is normally obtained via context from
428 * FormControl.
429 * The prop defaults to the value (`'none'`) inherited from the parent FormControl component.
430 */
431 margin: PropTypes.oneOf(['dense', 'none']),
432 /**
433 * Maximum number of rows to display when multiline option is set to true.
434 */
435 maxRows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
436 /**
437 * Minimum number of rows to display when multiline option is set to true.
438 */
439 minRows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
440 /**
441 * If `true`, a [TextareaAutosize](https://mui.com/material-ui/react-textarea-autosize/) element is rendered.
442 * @default false
443 */
444 multiline: PropTypes.bool,
445 /**
446 * Name attribute of the `input` element.
447 */
448 name: PropTypes.string,
449 /**
450 * Callback fired when the value is changed.
451 *
452 * @param {React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>} event The event source of the callback.
453 * You can pull out the new value by accessing `event.target.value` (string).
454 */
455 onChange: PropTypes.func,
456 /**
457 * The short hint displayed in the `input` before the user enters a value.
458 */
459 placeholder: PropTypes.string,
460 /**
461 * It prevents the user from changing the value of the field
462 * (not from interacting with the field).
463 */
464 readOnly: PropTypes.bool,
465 /**
466 * If `true`, the `input` element is required.
467 * The prop defaults to the value (`false`) inherited from the parent FormControl component.
468 */
469 required: PropTypes.bool,
470 /**
471 * Number of rows to display when multiline option is set to true.
472 */
473 rows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
474 /**
475 * The extra props for the slot components.
476 * You can override the existing props or add new ones.
477 *
478 * This prop is an alias for the `componentsProps` prop, which will be deprecated in the future.
479 *
480 * @default {}
481 */
482 slotProps: PropTypes.shape({
483 input: PropTypes.object,
484 root: PropTypes.object
485 }),
486 /**
487 * The components used for each slot inside.
488 *
489 * This prop is an alias for the `components` prop, which will be deprecated in the future.
490 *
491 * @default {}
492 */
493 slots: PropTypes.shape({
494 input: PropTypes.elementType,
495 root: PropTypes.elementType
496 }),
497 /**
498 * Start `InputAdornment` for this component.
499 */
500 startAdornment: PropTypes.node,
501 /**
502 * The system prop that allows defining system overrides as well as additional CSS styles.
503 */
504 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
505 /**
506 * 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).
507 * @default 'text'
508 */
509 type: PropTypes.string,
510 /**
511 * The value of the `input` element, required for a controlled component.
512 */
513 value: PropTypes.any
514} : void 0;
515if (FilledInput) {
516 FilledInput.muiName = 'Input';
517}
518export default FilledInput;
\No newline at end of file