跳转到内容

FormControl API

想要学习更多组件属性和 CSS API,可以查阅 React FormControl 组件的 API 文档。

导入

import FormControl from '@mui/material/FormControl';
// 或
import { FormControl } from '@mui/material';
You can learn about the difference by reading this guide on minimizing bundle size.

Provides context such as filled/focused/error/required for form inputs. Relying on the context provides high flexibility and ensures that the state always stays consistent across the children of the FormControl. This context is used by the following components:

  • FormLabel
  • FormHelperText
  • Input
  • InputLabel

You can find one composition example below and more going to the demos.

<FormControl>
  <InputLabel htmlFor="my-input">Email address</InputLabel>
  <Input id="my-input" aria-describedby="my-helper-text" />
  <FormHelperText id="my-helper-text">We'll never share your email.</FormHelperText>
</FormControl>

⚠️ Only one InputBase can be used within a FormControl because it create visual inconsistencies. For instance, only one input can be focused at the same time, the state shouldn't be shared.

组件名称

The name MuiFormControl can be used when providing default props or style overrides in the theme.

属性

原生(Native) 组件的属性也是可用的。

名称类型默认值描述
childrennode
The content of the component.
classesobject
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.
componentelementType
The component used for the root node. Either a string to use a HTML element or a component.
disabledboolfalse
If true, the label, input and helper text should be displayed in a disabled state.
errorboolfalse
If true, the label should be displayed in an error state.
focusedboolfalse
If true, the component is displayed in focused state.
fullWidthboolfalse
If true, the component will take up the full width of its container.
hiddenLabelboolfalse
If true, the label is hidden. This is used to increase density for a FilledInput. Be sure to add aria-label to the input element.
margin'dense'
| 'none'
| 'normal'
'none'
If dense or normal, will adjust vertical spacing of this and contained components.
requiredboolfalse
If true, the label will indicate that the input is required.
size'medium'
| 'small'
| string
'medium'
The size of the autocomplete.
sxArray<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.
variant'filled'
| 'outlined'
| 'standard'
'outlined'
The variant to use.

ref 则会被传递到根元素中。

CSS

规则名称全局类名描述
root.MuiFormControl-rootStyles applied to the root element.
marginNormal.MuiFormControl-marginNormalStyles applied to the root element if margin="normal".
marginDense.MuiFormControl-marginDenseStyles applied to the root element if margin="dense".
fullWidth.MuiFormControl-fullWidthStyles applied to the root element if fullWidth={true}.

您可以使用组件自定义选项对组件进行个性化:

演示项目