TextField API
想要学习更多组件属性和 CSS API,可以查阅 React TextField 组件的 API 文档。
导入
import TextField from '@mui/material/TextField';
// 或
import { TextField } from '@mui/material';
The TextField
is a convenience wrapper for the most common cases (80%).
It cannot be all things to all people, otherwise the API would grow out of control.
Advanced Configuration
It's important to understand that the text field is a simple abstraction on top of the following components:
If you wish to alter the props applied to the input
element, you can do so as follows:
const inputProps = {
step: 300,
};
return <TextField id="time" type="time" inputProps={inputProps} />;
For advanced cases, please look at the source of TextField by clicking on the "Edit this page" button above. Consider either:
- using the upper case props for passing values directly to the components
- using the underlying components directly as shown in the demos
组件名称
在主题中,名称“MuiTextField
”可用于提供默认属性(props),或者样式覆盖属性
FormControl 组件的属性也是可用的。
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
autoComplete | string | This prop helps users to fill forms faster, especially on mobile devices. The name can be confusing, as it's more like an autofill. You can learn more about it following the specification. | |
autoFocus | bool | false | If true , the input element is focused during the first mount. |
classes | object | Override or extend the styles applied to the component. See CSS API below for more details. | |
color | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' | string | 'primary' | The color of the component. It supports both default and custom theme colors, which can be added as shown in the palette customization guide. |
defaultValue | any | The default input value. Use when the component is not controlled. | |
disabled | bool | false | 如果被设置为 true ,那么该组件将会被禁用。 |
error | bool | false | If true , the label should be displayed in an error state. |
FormHelperTextProps | object | Props applied to the FormHelperText element. | |
fullWidth | bool | false | If true , the input will take up the full width of its container. |
helperText | node | The helper text content. | |
id | string | The id of the input element. Use this prop to make label and helperText accessible for screen readers. | |
InputLabelProps | object | Props applied to the InputLabel element. Pointer events like onClick are enabled if and only if shrink is true . | |
inputProps | object | Attributes applied to the input element. | |
InputProps | object | Props applied to the Input element. It will be a FilledInput , OutlinedInput or Input component depending on the variant prop value. | |
inputRef | ref | Pass a ref to the input element. | |
label | node | The label content. | |
margin | 'dense' | 'none' | 'normal' | 'none' | If dense or normal , will adjust vertical spacing of this and contained components. |
maxRows | number | string | Maximum number of rows to display when multiline option is set to true. | |
minRows | number | string | Minimum number of rows to display when multiline option is set to true. | |
multiline | bool | false | If true , a textarea element is rendered.instead of an input. |
name | string | Name attribute of the input element. | |
onChange | func | Callback fired when the value is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value (string). | |
placeholder | string | The short hint displayed in the input before the user enters a value. | |
required | bool | false | If true , the label is displayed as required and the input element is required. |
rows | number | string | Number of rows to display when multiline option is set to true. | |
select | bool | false | Render a Select element while passing the Input element to Select as input parameter. If this option is set you must pass the options of the select as children. |
SelectProps | object | Props applied to the Select element. | |
size | 'medium' | 'small' | string | The size of the autocomplete. | |
sx | Array<func | object | bool> | func | object | The system prop that allows defining system overrides as well as additional CSS styles. See the `sx` page for more details. | |
type | string | Type of the input element. It should be a valid HTML5 input type. | |
value | any | The value of the input element, required for a controlled component. | |
variant | 'filled' | 'outlined' | 'standard' | 'outlined' | The variant to use. |
ref
则会被传递到根元素中。继承
尽管上文没有明文记录, FormControl 组件 的属性(props) 在组件TextField上同样是可用的。 你可以利用这一点来 指向嵌套组件CSS
规则名称 | 全局类名 | 描述 |
---|---|---|
root | .MuiTextField-root | Styles applied to the root element. |
您可以使用组件自定义选项对组件进行个性化:
- 具备 全局类名.
- 在自定义主体中,设定一个规则名称作为组件的
styleOverrides
属性的一部分。