UNPKG

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