UNPKG

8.58 kBTypeScriptView Raw
1import * as React from 'react';
2import { StandardProps, PropTypes } from '..';
3import { FormControlProps } from '../FormControl';
4import { FormHelperTextProps } from '../FormHelperText';
5import { InputProps as StandardInputProps } from '../Input';
6import { FilledInputProps } from '../FilledInput';
7import { OutlinedInputProps } from '../OutlinedInput';
8import { InputLabelProps } from '../InputLabel';
9import { SelectProps } from '../Select';
10
11export interface BaseTextFieldProps
12 extends StandardProps<
13 FormControlProps,
14 TextFieldClassKey,
15 // event handlers are declared on derived interfaces
16 'onChange' | 'onBlur' | 'onFocus' | 'defaultValue'
17 > {
18 /**
19 * This prop helps users to fill forms faster, especially on mobile devices.
20 * The name can be confusing, as it's more like an autofill.
21 * You can learn more about it [following the specification](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill).
22 */
23 autoComplete?: string;
24 /**
25 * If `true`, the `input` element will be focused during the first mount.
26 */
27 autoFocus?: boolean;
28 /**
29 * @ignore
30 */
31 children?: React.ReactNode;
32 /**
33 * The color of the component. It supports those theme colors that make sense for this component.
34 */
35 color?: 'primary' | 'secondary';
36 /**
37 * The default value of the `input` element.
38 */
39 defaultValue?: unknown;
40 /**
41 * If `true`, the `input` element will be disabled.
42 */
43 disabled?: boolean;
44 /**
45 * If `true`, the label will be displayed in an error state.
46 */
47 error?: boolean;
48 /**
49 * Props applied to the [`FormHelperText`](/api/form-helper-text/) element.
50 */
51 FormHelperTextProps?: Partial<FormHelperTextProps>;
52 /**
53 * If `true`, the input will take up the full width of its container.
54 */
55 fullWidth?: boolean;
56 /**
57 * The helper text content.
58 */
59 helperText?: React.ReactNode;
60 /**
61 * The id of the `input` element.
62 * Use this prop to make `label` and `helperText` accessible for screen readers.
63 */
64 id?: string;
65 /**
66 * Props applied to the [`InputLabel`](/api/input-label/) element.
67 */
68 InputLabelProps?: Partial<InputLabelProps>;
69 /**
70 * Pass a ref to the `input` element.
71 */
72 inputRef?: React.Ref<any>;
73 /**
74 * The label content.
75 */
76 label?: React.ReactNode;
77 /**
78 * If `dense` or `normal`, will adjust vertical spacing of this and contained components.
79 */
80 margin?: PropTypes.Margin;
81 /**
82 * If `true`, a textarea element will be rendered instead of an input.
83 */
84 multiline?: boolean;
85 /**
86 * Name attribute of the `input` element.
87 */
88 name?: string;
89 /**
90 * The short hint displayed in the input before the user enters a value.
91 */
92 placeholder?: string;
93 /**
94 * If `true`, the label is displayed as required and the `input` element` will be required.
95 */
96 required?: boolean;
97 /**
98 * Number of rows to display when multiline option is set to true.
99 * @deprecated Use `minRows` instead.
100 */
101 rows?: string | number;
102 /**
103 * Maximum number of rows to display.
104 * @deprecated Use `maxRows` instead.
105 */
106 rowsMax?: string | number;
107 /**
108 * Maximum number of rows to display when multiline option is set to true.
109 */
110 maxRows?: string | number;
111 /**
112 * Minimum number of rows to display.
113 */
114 minRows?: string | number;
115 /**
116 * Render a [`Select`](/api/select/) element while passing the Input element to `Select` as `input` parameter.
117 * If this option is set you must pass the options of the select as children.
118 */
119 select?: boolean;
120 /**
121 * Props applied to the [`Select`](/api/select/) element.
122 */
123 SelectProps?: Partial<SelectProps>;
124 /**
125 * The size of the text field.
126 */
127 size?: 'small' | 'medium';
128 /**
129 * 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).
130 */
131 type?: React.InputHTMLAttributes<unknown>['type'];
132 /**
133 * The value of the `input` element, required for a controlled component.
134 */
135 value?: unknown;
136}
137
138export interface StandardTextFieldProps extends BaseTextFieldProps {
139 onBlur?: StandardInputProps['onBlur'];
140 /**
141 * Callback fired when the value is changed.
142 *
143 * @param {object} event The event source of the callback.
144 * You can pull out the new value by accessing `event.target.value` (string).
145 */
146 onChange?: StandardInputProps['onChange'];
147 onFocus?: StandardInputProps['onFocus'];
148 /**
149 * The variant to use.
150 */
151 variant?: 'standard';
152 /**
153 * Props applied to the Input element.
154 * It will be a [`FilledInput`](/api/filled-input/),
155 * [`OutlinedInput`](/api/outlined-input/) or [`Input`](/api/input/)
156 * component depending on the `variant` prop value.
157 */
158 InputProps?: Partial<StandardInputProps>;
159 /**
160 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
161 */
162 inputProps?: StandardInputProps['inputProps'];
163}
164
165export interface FilledTextFieldProps extends BaseTextFieldProps {
166 onBlur?: FilledInputProps['onBlur'];
167 /**
168 * Callback fired when the value is changed.
169 *
170 * @param {object} event The event source of the callback.
171 * You can pull out the new value by accessing `event.target.value` (string).
172 */
173 onChange?: FilledInputProps['onChange'];
174 onFocus?: FilledInputProps['onFocus'];
175 /**
176 * The variant to use.
177 */
178 variant: 'filled';
179 /**
180 * Props applied to the Input element.
181 * It will be a [`FilledInput`](/api/filled-input/),
182 * [`OutlinedInput`](/api/outlined-input/) or [`Input`](/api/input/)
183 * component depending on the `variant` prop value.
184 */
185 InputProps?: Partial<FilledInputProps>;
186 /**
187 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
188 */
189 inputProps?: FilledInputProps['inputProps'];
190}
191
192export interface OutlinedTextFieldProps extends BaseTextFieldProps {
193 onBlur?: OutlinedInputProps['onBlur'];
194 /**
195 * Callback fired when the value is changed.
196 *
197 * @param {object} event The event source of the callback.
198 * You can pull out the new value by accessing `event.target.value` (string).
199 */
200 onChange?: OutlinedInputProps['onChange'];
201 onFocus?: OutlinedInputProps['onFocus'];
202 /**
203 * The variant to use.
204 */
205 variant: 'outlined';
206 /**
207 * Props applied to the Input element.
208 * It will be a [`FilledInput`](/api/filled-input/),
209 * [`OutlinedInput`](/api/outlined-input/) or [`Input`](/api/input/)
210 * component depending on the `variant` prop value.
211 */
212 InputProps?: Partial<OutlinedInputProps>;
213 /**
214 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
215 */
216 inputProps?: OutlinedInputProps['inputProps'];
217}
218
219export type TextFieldProps = StandardTextFieldProps | FilledTextFieldProps | OutlinedTextFieldProps;
220
221export type TextFieldClassKey = 'root';
222
223/**
224 * The `TextField` is a convenience wrapper for the most common cases (80%).
225 * It cannot be all things to all people, otherwise the API would grow out of control.
226 *
227 * ## Advanced Configuration
228 *
229 * It's important to understand that the text field is a simple abstraction
230 * on top of the following components:
231 *
232 * - [FormControl](https://mui.com/api/form-control/)
233 * - [InputLabel](https://mui.com/api/input-label/)
234 * - [FilledInput](https://mui.com/api/filled-input/)
235 * - [OutlinedInput](https://mui.com/api/outlined-input/)
236 * - [Input](https://mui.com/api/input/)
237 * - [FormHelperText](https://mui.com/api/form-helper-text/)
238 *
239 * If you wish to alter the props applied to the `input` element, you can do so as follows:
240 *
241 * ```jsx
242 * const inputProps = {
243 * step: 300,
244 * };
245 *
246 * return <TextField id="time" type="time" inputProps={inputProps} />;
247 * ```
248 *
249 * For advanced cases, please look at the source of TextField by clicking on the
250 * "Edit this page" button above. Consider either:
251 *
252 * - using the upper case props for passing values directly to the components
253 * - using the underlying components directly as shown in the demos
254 * Demos:
255 *
256 * - [Autocomplete](https://mui.com/components/autocomplete/)
257 * - [Pickers](https://mui.com/components/pickers/)
258 * - [Text Fields](https://mui.com/components/text-fields/)
259 *
260 * API:
261 *
262 * - [TextField API](https://mui.com/api/text-field/)
263 * - inherits [FormControl API](https://mui.com/api/form-control/)
264 */
265export default function TextField(props: TextFieldProps): JSX.Element;