Pular para o conteúdo

TextField API

Documentação da API para o componente React TextField . Aprenda sobre as propriedades disponíveis e a API CSS.

Importação

import TextField from '@mui/material/TextField';
// ou
import { TextField } from '@mui/material';
Você pode aprender sobre a diferença lendo este guia sobre como minimizar o tamanho do pacote.

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

Nome do componente

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

Propriedades

Props of the FormControl component are also available.

NomeTipoPadrãoDescrição
autoCompletestring
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.
autoFocusboolfalse
If true, the input element is focused during the first mount.
classesobject
Sobrescreve ou extende os estilos aplicados para o componente. Veja a API CSS abaixo para maiores detalhes.
color'primary'
| 'secondary'
| 'error'
| 'info'
| 'success'
| 'warning'
| string
'primary'
A cor do componente. Ela da suporte para as cores do tema que fazem sentido para este componente.
defaultValueany
The default input value. Use when the component is not controlled.
disabledboolfalse
Se true, o componente está desabilitado.
errorboolfalse
If true, the label should be displayed in an error state.
FormHelperTextPropsobject
Props applied to the FormHelperText element.
fullWidthboolfalse
If true, the input will take up the full width of its container.
helperTextnode
The helper text content.
idstring
The id of the input element. Use this prop to make label and helperText accessible for screen readers.
InputLabelPropsobject
Props applied to the InputLabel element.
inputPropsobject
Attributes applied to the input element.
InputPropsobject
Props applied to the Input element. It will be a FilledInput, OutlinedInput or Input component depending on the variant prop value.
inputRefref
Pass a ref to the input element.
labelnode
The label content.
margin'dense'
| 'none'
| 'normal'
'none'
If dense or normal, will adjust vertical spacing of this and contained components.
maxRowsnumber
| string
Maximum number of rows to display when multiline option is set to true.
minRowsnumber
| string
Minimum number of rows to display when multiline option is set to true.
multilineboolfalse
If true, a textarea element is rendered.instead of an input.
namestring
Name attribute of the input element.
onChangefunc
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).
placeholderstring
The short hint displayed in the input before the user enters a value.
requiredboolfalse
If true, the label is displayed as required and the input element is required.
rowsnumber
| string
Number of rows to display when multiline option is set to true.
selectboolfalse
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.
SelectPropsobject
Props applied to the Select element.
size'medium'
| 'small'
| string
The size of the autocomplete.
sxfunc
| object
The system prop that allows defining system overrides as well as additional CSS styles. See the `sx` page for more details.
typestring
Type of the input element. It should be a valid HTML5 input type.
valueany
The value of the input element, required for a controlled component.
variant'filled'
| 'outlined'
| 'standard'
'outlined'
A variante a usar.

O ref é encaminhado para o elemento raiz.

Herança

Embora não explicitamente documentado acima, as propriedades do componente FormControl também estão disponíveis em TextField. Você pode tirar vantagem disso para manipular componentes aninhados.

CSS

Nome da regraClasse globalDescrição
root.MuiTextField-rootEstilos aplicados ao elemento raiz.

Você pode sobrescrever o estilo do componente usando uma dessas opções de customização:

Demonstrações