UNPKG

12.6 kBTypeScriptView Raw
1import * as React from 'react';
2import { SxProps } from '@mui/system';
3import { OverridableStringUnion } from '@mui/types';
4import { InternalStandardProps as StandardProps } from '..';
5import { FormControlProps } from '../FormControl';
6import { FormHelperTextProps } from '../FormHelperText';
7import { InputBaseProps } from '../InputBase';
8import { InputProps as StandardInputProps } from '../Input';
9import { FilledInputProps } from '../FilledInput';
10import { OutlinedInputProps } from '../OutlinedInput';
11import { InputLabelProps } from '../InputLabel';
12import { SelectProps } from '../Select';
13import { Theme } from '../styles';
14import { TextFieldClasses } from './textFieldClasses';
15import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
16
17export interface TextFieldPropsColorOverrides {}
18export interface TextFieldPropsSizeOverrides {}
19
20export interface TextFieldSlots {
21 /**
22 * The component that renders the input.
23 * @default OutlinedInput
24 */
25 input: React.ElementType;
26 /**
27 * The component that renders the input's label.
28 * @default InputLabel
29 */
30 inputLabel: React.ElementType;
31 /**
32 * The html input element.
33 * @default 'input'
34 */
35 htmlInput: React.ElementType;
36 /**
37 * The component that renders the helper text.
38 * @default FormHelperText
39 */
40 formHelperText: React.ElementType;
41 /**
42 * The component that renders the select.
43 * @default Select
44 */
45 select: React.ElementType;
46}
47
48export type TextFieldSlotsAndSlotProps<InputPropsType> = CreateSlotsAndSlotProps<
49 TextFieldSlots,
50 {
51 input: SlotProps<React.ElementType<InputPropsType>, {}, TextFieldOwnerState>;
52 inputLabel: SlotProps<React.ElementType<InputLabelProps>, {}, TextFieldOwnerState>;
53 htmlInput: SlotProps<React.ElementType<InputBaseProps['inputProps']>, {}, TextFieldOwnerState>;
54 formHelperText: SlotProps<React.ElementType<FormHelperTextProps>, {}, TextFieldOwnerState>;
55 select: SlotProps<React.ElementType<SelectProps>, {}, TextFieldOwnerState>;
56 }
57>;
58
59export interface BaseTextFieldProps
60 extends StandardProps<
61 FormControlProps,
62 // event handlers are declared on derived interfaces
63 'onChange' | 'onBlur' | 'onFocus' | 'defaultValue'
64 > {
65 /**
66 * This prop helps users to fill forms faster, especially on mobile devices.
67 * The name can be confusing, as it's more like an autofill.
68 * You can learn more about it [following the specification](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).
69 */
70 autoComplete?: string;
71 /**
72 * If `true`, the `input` element is focused during the first mount.
73 * @default false
74 */
75 autoFocus?: boolean;
76 /**
77 * @ignore
78 */
79 children?: FormControlProps['children'];
80 /**
81 * Override or extend the styles applied to the component.
82 */
83 classes?: Partial<TextFieldClasses>;
84 /**
85 * The color of the component.
86 * It supports both default and custom theme colors, which can be added as shown in the
87 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
88 * @default 'primary'
89 */
90 color?: OverridableStringUnion<
91 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning',
92 TextFieldPropsColorOverrides
93 >;
94 /**
95 * The default value. Use when the component is not controlled.
96 */
97 defaultValue?: unknown;
98 /**
99 * If `true`, the component is disabled.
100 * @default false
101 */
102 disabled?: boolean;
103 /**
104 * If `true`, the label is displayed in an error state.
105 * @default false
106 */
107 error?: boolean;
108 /**
109 * Props applied to the [`FormHelperText`](https://mui.com/material-ui/api/form-helper-text/) element.
110 * @deprecated Use `slotProps.formHelperText` 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.
111 */
112 FormHelperTextProps?: Partial<FormHelperTextProps>;
113 /**
114 * If `true`, the input will take up the full width of its container.
115 * @default false
116 */
117 fullWidth?: boolean;
118 /**
119 * The helper text content.
120 */
121 helperText?: React.ReactNode;
122 /**
123 * The id of the `input` element.
124 * Use this prop to make `label` and `helperText` accessible for screen readers.
125 */
126 id?: string;
127 /**
128 * Props applied to the [`InputLabel`](https://mui.com/material-ui/api/input-label/) element.
129 * Pointer events like `onClick` are enabled if and only if `shrink` is `true`.
130 * @deprecated Use `slotProps.inputLabel` 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.
131 */
132 InputLabelProps?: Partial<InputLabelProps>;
133 /**
134 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
135 * @deprecated Use `slotProps.htmlInput` 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.
136 */
137 inputProps?: InputBaseProps['inputProps'];
138 /**
139 * Pass a ref to the `input` element.
140 */
141 inputRef?: React.Ref<any>;
142 /**
143 * The label content.
144 */
145 label?: React.ReactNode;
146 /**
147 * If `true`, a `textarea` element is rendered instead of an input.
148 * @default false
149 */
150 multiline?: boolean;
151 /**
152 * Name attribute of the `input` element.
153 */
154 name?: string;
155 onBlur?: InputBaseProps['onBlur'];
156 onFocus?: StandardInputProps['onFocus'];
157 /**
158 * The short hint displayed in the `input` before the user enters a value.
159 */
160 placeholder?: string;
161 /**
162 * If `true`, the label is displayed as required and the `input` element is required.
163 * @default false
164 */
165 required?: boolean;
166 /**
167 * Number of rows to display when multiline option is set to true.
168 */
169 rows?: string | number;
170 /**
171 * Maximum number of rows to display when multiline option is set to true.
172 */
173 maxRows?: string | number;
174 /**
175 * Minimum number of rows to display when multiline option is set to true.
176 */
177 minRows?: string | number;
178 /**
179 * Render a [`Select`](https://mui.com/material-ui/api/select/) element while passing the Input element to `Select` as `input` parameter.
180 * If this option is set you must pass the options of the select as children.
181 * @default false
182 */
183 select?: boolean;
184 /**
185 * Props applied to the [`Select`](https://mui.com/material-ui/api/select/) element.
186 * @deprecated Use `slotProps.select` 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.
187 */
188 SelectProps?: Partial<SelectProps>;
189 /**
190 * The size of the component.
191 */
192 size?: OverridableStringUnion<'small' | 'medium', TextFieldPropsSizeOverrides>;
193 /**
194 * The system prop that allows defining system overrides as well as additional CSS styles.
195 */
196 sx?: SxProps<Theme>;
197 /**
198 * 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).
199 */
200 type?: React.InputHTMLAttributes<unknown>['type'];
201 /**
202 * The value of the `input` element, required for a controlled component.
203 */
204 value?: unknown;
205}
206
207export interface StandardTextFieldProps
208 extends BaseTextFieldProps,
209 TextFieldSlotsAndSlotProps<StandardInputProps> {
210 /**
211 * Callback fired when the value is changed.
212 *
213 * @param {object} event The event source of the callback.
214 * You can pull out the new value by accessing `event.target.value` (string).
215 */
216 onChange?: StandardInputProps['onChange'];
217 /**
218 * The variant to use.
219 * @default 'outlined'
220 */
221 variant?: 'standard';
222 /**
223 * Props applied to the Input element.
224 * It will be a [`FilledInput`](https://mui.com/material-ui/api/filled-input/),
225 * [`OutlinedInput`](https://mui.com/material-ui/api/outlined-input/) or [`Input`](https://mui.com/material-ui/api/input/)
226 * component depending on the `variant` prop value.
227 * @deprecated Use `slotProps.input` 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.
228 */
229 InputProps?: Partial<StandardInputProps>;
230}
231
232export interface FilledTextFieldProps
233 extends BaseTextFieldProps,
234 TextFieldSlotsAndSlotProps<FilledInputProps> {
235 /**
236 * Callback fired when the value is changed.
237 *
238 * @param {object} event The event source of the callback.
239 * You can pull out the new value by accessing `event.target.value` (string).
240 */
241 onChange?: FilledInputProps['onChange'];
242 /**
243 * The variant to use.
244 * @default 'outlined'
245 */
246 variant: 'filled';
247 /**
248 * Props applied to the Input element.
249 * It will be a [`FilledInput`](https://mui.com/material-ui/api/filled-input/),
250 * [`OutlinedInput`](https://mui.com/material-ui/api/outlined-input/) or [`Input`](https://mui.com/material-ui/api/input/)
251 * component depending on the `variant` prop value.
252 * @deprecated Use `slotProps.input` 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.
253 */
254 InputProps?: Partial<FilledInputProps>;
255}
256
257export interface OutlinedTextFieldProps
258 extends BaseTextFieldProps,
259 TextFieldSlotsAndSlotProps<OutlinedInputProps> {
260 /**
261 * Callback fired when the value is changed.
262 *
263 * @param {object} event The event source of the callback.
264 * You can pull out the new value by accessing `event.target.value` (string).
265 */
266 onChange?: OutlinedInputProps['onChange'];
267 /**
268 * The variant to use.
269 * @default 'outlined'
270 */
271 variant: 'outlined';
272 /**
273 * Props applied to the Input element.
274 * It will be a [`FilledInput`](https://mui.com/material-ui/api/filled-input/),
275 * [`OutlinedInput`](https://mui.com/material-ui/api/outlined-input/) or [`Input`](https://mui.com/material-ui/api/input/)
276 * component depending on the `variant` prop value.
277 * @deprecated Use `slotProps.input` 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.
278 */
279 InputProps?: Partial<OutlinedInputProps>;
280}
281
282export type TextFieldVariants = 'outlined' | 'standard' | 'filled';
283
284export type TextFieldProps<Variant extends TextFieldVariants = TextFieldVariants> =
285 Variant extends 'filled'
286 ? FilledTextFieldProps
287 : Variant extends 'standard'
288 ? StandardTextFieldProps
289 : OutlinedTextFieldProps;
290
291export type TextFieldOwnerState = BaseTextFieldProps;
292
293/**
294 * The `TextField` is a convenience wrapper for the most common cases (80%).
295 * It cannot be all things to all people, otherwise the API would grow out of control.
296 *
297 * ## Advanced Configuration
298 *
299 * It's important to understand that the text field is a simple abstraction
300 * on top of the following components:
301 *
302 * * [FormControl](https://mui.com/material-ui/api/form-control/)
303 * * [InputLabel](https://mui.com/material-ui/api/input-label/)
304 * * [FilledInput](https://mui.com/material-ui/api/filled-input/)
305 * * [OutlinedInput](https://mui.com/material-ui/api/outlined-input/)
306 * * [Input](https://mui.com/material-ui/api/input/)
307 * * [FormHelperText](https://mui.com/material-ui/api/form-helper-text/)
308 *
309 * If you wish to alter the props applied to the `input` element, you can do so as follows:
310 *
311 * ```jsx
312 * const inputProps = {
313 * step: 300,
314 * };
315 *
316 * return <TextField id="time" type="time" inputProps={inputProps} />;
317 * ```
318 *
319 * For advanced cases, please look at the source of TextField by clicking on the
320 * "Edit this page" button above. Consider either:
321 *
322 * * using the upper case props for passing values directly to the components
323 * * using the underlying components directly as shown in the demos
324 *
325 * Demos:
326 *
327 * - [Autocomplete](https://mui.com/material-ui/react-autocomplete/)
328 * - [Text Field](https://mui.com/material-ui/react-text-field/)
329 *
330 * API:
331 *
332 * - [TextField API](https://mui.com/material-ui/api/text-field/)
333 * - inherits [FormControl API](https://mui.com/material-ui/api/form-control/)
334 */
335export default function TextField<Variant extends TextFieldVariants>(
336 props: {
337 /**
338 * The variant to use.
339 * @default 'outlined'
340 */
341 variant?: Variant;
342 } & Omit<TextFieldProps, 'variant'>,
343): React.JSX.Element;