UNPKG

13.5 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import { unstable_composeClasses as composeClasses } from '@mui/base';
7import { refType, unstable_useId as useId } from '@mui/utils';
8import styled from '../styles/styled';
9import useThemeProps from '../styles/useThemeProps';
10import Input from '../Input';
11import FilledInput from '../FilledInput';
12import OutlinedInput from '../OutlinedInput';
13import InputLabel from '../InputLabel';
14import FormControl from '../FormControl';
15import FormHelperText from '../FormHelperText';
16import Select from '../Select';
17import { getTextFieldUtilityClass } from './textFieldClasses';
18import { jsx as _jsx } from "react/jsx-runtime";
19import { jsxs as _jsxs } from "react/jsx-runtime";
20var variantComponent = {
21 standard: Input,
22 filled: FilledInput,
23 outlined: OutlinedInput
24};
25
26var useUtilityClasses = function useUtilityClasses(ownerState) {
27 var classes = ownerState.classes;
28 var slots = {
29 root: ['root']
30 };
31 return composeClasses(slots, getTextFieldUtilityClass, classes);
32};
33
34var TextFieldRoot = styled(FormControl, {
35 name: 'MuiTextField',
36 slot: 'Root',
37 overridesResolver: function overridesResolver(props, styles) {
38 return styles.root;
39 }
40})({});
41/**
42 * The `TextField` is a convenience wrapper for the most common cases (80%).
43 * It cannot be all things to all people, otherwise the API would grow out of control.
44 *
45 * ## Advanced Configuration
46 *
47 * It's important to understand that the text field is a simple abstraction
48 * on top of the following components:
49 *
50 * - [FormControl](/material-ui/api/form-control/)
51 * - [InputLabel](/material-ui/api/input-label/)
52 * - [FilledInput](/material-ui/api/filled-input/)
53 * - [OutlinedInput](/material-ui/api/outlined-input/)
54 * - [Input](/material-ui/api/input/)
55 * - [FormHelperText](/material-ui/api/form-helper-text/)
56 *
57 * If you wish to alter the props applied to the `input` element, you can do so as follows:
58 *
59 * ```jsx
60 * const inputProps = {
61 * step: 300,
62 * };
63 *
64 * return <TextField id="time" type="time" inputProps={inputProps} />;
65 * ```
66 *
67 * For advanced cases, please look at the source of TextField by clicking on the
68 * "Edit this page" button above. Consider either:
69 *
70 * - using the upper case props for passing values directly to the components
71 * - using the underlying components directly as shown in the demos
72 */
73
74var TextField = /*#__PURE__*/React.forwardRef(function TextField(inProps, ref) {
75 var props = useThemeProps({
76 props: inProps,
77 name: 'MuiTextField'
78 });
79
80 var autoComplete = props.autoComplete,
81 _props$autoFocus = props.autoFocus,
82 autoFocus = _props$autoFocus === void 0 ? false : _props$autoFocus,
83 children = props.children,
84 className = props.className,
85 _props$color = props.color,
86 color = _props$color === void 0 ? 'primary' : _props$color,
87 defaultValue = props.defaultValue,
88 _props$disabled = props.disabled,
89 disabled = _props$disabled === void 0 ? false : _props$disabled,
90 _props$error = props.error,
91 error = _props$error === void 0 ? false : _props$error,
92 FormHelperTextProps = props.FormHelperTextProps,
93 _props$fullWidth = props.fullWidth,
94 fullWidth = _props$fullWidth === void 0 ? false : _props$fullWidth,
95 helperText = props.helperText,
96 idOverride = props.id,
97 InputLabelProps = props.InputLabelProps,
98 inputProps = props.inputProps,
99 InputProps = props.InputProps,
100 inputRef = props.inputRef,
101 label = props.label,
102 maxRows = props.maxRows,
103 minRows = props.minRows,
104 _props$multiline = props.multiline,
105 multiline = _props$multiline === void 0 ? false : _props$multiline,
106 name = props.name,
107 onBlur = props.onBlur,
108 onChange = props.onChange,
109 onFocus = props.onFocus,
110 placeholder = props.placeholder,
111 _props$required = props.required,
112 required = _props$required === void 0 ? false : _props$required,
113 rows = props.rows,
114 _props$select = props.select,
115 select = _props$select === void 0 ? false : _props$select,
116 SelectProps = props.SelectProps,
117 type = props.type,
118 value = props.value,
119 _props$variant = props.variant,
120 variant = _props$variant === void 0 ? 'outlined' : _props$variant,
121 other = _objectWithoutProperties(props, ["autoComplete", "autoFocus", "children", "className", "color", "defaultValue", "disabled", "error", "FormHelperTextProps", "fullWidth", "helperText", "id", "InputLabelProps", "inputProps", "InputProps", "inputRef", "label", "maxRows", "minRows", "multiline", "name", "onBlur", "onChange", "onFocus", "placeholder", "required", "rows", "select", "SelectProps", "type", "value", "variant"]);
122
123 var ownerState = _extends({}, props, {
124 autoFocus: autoFocus,
125 color: color,
126 disabled: disabled,
127 error: error,
128 fullWidth: fullWidth,
129 multiline: multiline,
130 required: required,
131 select: select,
132 variant: variant
133 });
134
135 var classes = useUtilityClasses(ownerState);
136
137 if (process.env.NODE_ENV !== 'production') {
138 if (select && !children) {
139 console.error('MUI: `children` must be passed when using the `TextField` component with `select`.');
140 }
141 }
142
143 var InputMore = {};
144
145 if (variant === 'outlined') {
146 if (InputLabelProps && typeof InputLabelProps.shrink !== 'undefined') {
147 InputMore.notched = InputLabelProps.shrink;
148 }
149
150 InputMore.label = label;
151 }
152
153 if (select) {
154 // unset defaults from textbox inputs
155 if (!SelectProps || !SelectProps.native) {
156 InputMore.id = undefined;
157 }
158
159 InputMore['aria-describedby'] = undefined;
160 }
161
162 var id = useId(idOverride);
163 var helperTextId = helperText && id ? "".concat(id, "-helper-text") : undefined;
164 var inputLabelId = label && id ? "".concat(id, "-label") : undefined;
165 var InputComponent = variantComponent[variant];
166
167 var InputElement = /*#__PURE__*/_jsx(InputComponent, _extends({
168 "aria-describedby": helperTextId,
169 autoComplete: autoComplete,
170 autoFocus: autoFocus,
171 defaultValue: defaultValue,
172 fullWidth: fullWidth,
173 multiline: multiline,
174 name: name,
175 rows: rows,
176 maxRows: maxRows,
177 minRows: minRows,
178 type: type,
179 value: value,
180 id: id,
181 inputRef: inputRef,
182 onBlur: onBlur,
183 onChange: onChange,
184 onFocus: onFocus,
185 placeholder: placeholder,
186 inputProps: inputProps
187 }, InputMore, InputProps));
188
189 return /*#__PURE__*/_jsxs(TextFieldRoot, _extends({
190 className: clsx(classes.root, className),
191 disabled: disabled,
192 error: error,
193 fullWidth: fullWidth,
194 ref: ref,
195 required: required,
196 color: color,
197 variant: variant,
198 ownerState: ownerState
199 }, other, {
200 children: [label != null && label !== '' && /*#__PURE__*/_jsx(InputLabel, _extends({
201 htmlFor: id,
202 id: inputLabelId
203 }, InputLabelProps, {
204 children: label
205 })), select ? /*#__PURE__*/_jsx(Select, _extends({
206 "aria-describedby": helperTextId,
207 id: id,
208 labelId: inputLabelId,
209 value: value,
210 input: InputElement
211 }, SelectProps, {
212 children: children
213 })) : InputElement, helperText && /*#__PURE__*/_jsx(FormHelperText, _extends({
214 id: helperTextId
215 }, FormHelperTextProps, {
216 children: helperText
217 }))]
218 }));
219});
220process.env.NODE_ENV !== "production" ? TextField.propTypes
221/* remove-proptypes */
222= {
223 // ----------------------------- Warning --------------------------------
224 // | These PropTypes are generated from the TypeScript type definitions |
225 // | To update them edit the d.ts file and run "yarn proptypes" |
226 // ----------------------------------------------------------------------
227
228 /**
229 * This prop helps users to fill forms faster, especially on mobile devices.
230 * The name can be confusing, as it's more like an autofill.
231 * You can learn more about it [following the specification](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).
232 */
233 autoComplete: PropTypes.string,
234
235 /**
236 * If `true`, the `input` element is focused during the first mount.
237 * @default false
238 */
239 autoFocus: PropTypes.bool,
240
241 /**
242 * @ignore
243 */
244 children: PropTypes.node,
245
246 /**
247 * Override or extend the styles applied to the component.
248 */
249 classes: PropTypes.object,
250
251 /**
252 * @ignore
253 */
254 className: PropTypes.string,
255
256 /**
257 * The color of the component.
258 * It supports both default and custom theme colors, which can be added as shown in the
259 * [palette customization guide](https://mui.com/material-ui/customization/palette/#adding-new-colors).
260 * @default 'primary'
261 */
262 color: PropTypes
263 /* @typescript-to-proptypes-ignore */
264 .oneOfType([PropTypes.oneOf(['primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
265
266 /**
267 * The default value. Use when the component is not controlled.
268 */
269 defaultValue: PropTypes.any,
270
271 /**
272 * If `true`, the component is disabled.
273 * @default false
274 */
275 disabled: PropTypes.bool,
276
277 /**
278 * If `true`, the label is displayed in an error state.
279 * @default false
280 */
281 error: PropTypes.bool,
282
283 /**
284 * Props applied to the [`FormHelperText`](/material-ui/api/form-helper-text/) element.
285 */
286 FormHelperTextProps: PropTypes.object,
287
288 /**
289 * If `true`, the input will take up the full width of its container.
290 * @default false
291 */
292 fullWidth: PropTypes.bool,
293
294 /**
295 * The helper text content.
296 */
297 helperText: PropTypes.node,
298
299 /**
300 * The id of the `input` element.
301 * Use this prop to make `label` and `helperText` accessible for screen readers.
302 */
303 id: PropTypes.string,
304
305 /**
306 * Props applied to the [`InputLabel`](/material-ui/api/input-label/) element.
307 * Pointer events like `onClick` are enabled if and only if `shrink` is `true`.
308 */
309 InputLabelProps: PropTypes.object,
310
311 /**
312 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
313 */
314 inputProps: PropTypes.object,
315
316 /**
317 * Props applied to the Input element.
318 * It will be a [`FilledInput`](/material-ui/api/filled-input/),
319 * [`OutlinedInput`](/material-ui/api/outlined-input/) or [`Input`](/material-ui/api/input/)
320 * component depending on the `variant` prop value.
321 */
322 InputProps: PropTypes.object,
323
324 /**
325 * Pass a ref to the `input` element.
326 */
327 inputRef: refType,
328
329 /**
330 * The label content.
331 */
332 label: PropTypes.node,
333
334 /**
335 * If `dense` or `normal`, will adjust vertical spacing of this and contained components.
336 * @default 'none'
337 */
338 margin: PropTypes.oneOf(['dense', 'none', 'normal']),
339
340 /**
341 * Maximum number of rows to display when multiline option is set to true.
342 */
343 maxRows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
344
345 /**
346 * Minimum number of rows to display when multiline option is set to true.
347 */
348 minRows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
349
350 /**
351 * If `true`, a `textarea` element is rendered instead of an input.
352 * @default false
353 */
354 multiline: PropTypes.bool,
355
356 /**
357 * Name attribute of the `input` element.
358 */
359 name: PropTypes.string,
360
361 /**
362 * @ignore
363 */
364 onBlur: PropTypes.func,
365
366 /**
367 * Callback fired when the value is changed.
368 *
369 * @param {object} event The event source of the callback.
370 * You can pull out the new value by accessing `event.target.value` (string).
371 */
372 onChange: PropTypes.func,
373
374 /**
375 * @ignore
376 */
377 onFocus: PropTypes.func,
378
379 /**
380 * The short hint displayed in the `input` before the user enters a value.
381 */
382 placeholder: PropTypes.string,
383
384 /**
385 * If `true`, the label is displayed as required and the `input` element is required.
386 * @default false
387 */
388 required: PropTypes.bool,
389
390 /**
391 * Number of rows to display when multiline option is set to true.
392 */
393 rows: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
394
395 /**
396 * Render a [`Select`](/material-ui/api/select/) element while passing the Input element to `Select` as `input` parameter.
397 * If this option is set you must pass the options of the select as children.
398 * @default false
399 */
400 select: PropTypes.bool,
401
402 /**
403 * Props applied to the [`Select`](/material-ui/api/select/) element.
404 */
405 SelectProps: PropTypes.object,
406
407 /**
408 * The size of the component.
409 */
410 size: PropTypes
411 /* @typescript-to-proptypes-ignore */
412 .oneOfType([PropTypes.oneOf(['medium', 'small']), PropTypes.string]),
413
414 /**
415 * The system prop that allows defining system overrides as well as additional CSS styles.
416 */
417 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
418
419 /**
420 * 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).
421 */
422 type: PropTypes
423 /* @typescript-to-proptypes-ignore */
424 .string,
425
426 /**
427 * The value of the `input` element, required for a controlled component.
428 */
429 value: PropTypes.any,
430
431 /**
432 * The variant to use.
433 * @default 'outlined'
434 */
435 variant: PropTypes.oneOf(['filled', 'outlined', 'standard'])
436} : void 0;
437export default TextField;
\No newline at end of file