OTPField API
API reference docs for the React OTPField component. Learn about the props, CSS, and other APIs of this exported module.
Demos
Import
import OTPField from '@mui/material/OTPField';
// or
import { OTPField } from '@mui/material';Learn about the difference by reading this guide on minimizing bundle size.
A field for entering a one-time password, one character per input.
Props of the native component are also available.
| Name | Type | Default | Description |
|---|---|---|---|
| aria-describedby | string | - | Identifies the element (or elements) that describes the object. |
| aria-label | string | - | Defines a string value that labels the current element. |
| aria-labelledby | string | - | Identifies the element (or elements) that labels the current element. |
| autoComplete | string | 'one-time-code' | The autocomplete hint for the first character input and the form input. |
| autoFocus | bool | - | Focus the first character input on mount. |
| autoSubmit | bool | false | Submit the associated form when the code becomes complete. |
| children | node | - | Character inputs and optional decorative separators. When omitted, the field renders one OTPFieldInput per character. |
| classes | object | - | Override or extend the styles applied to the component. See CSS classes API below for more details. |
| color | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' | string | 'primary' | The theme palette color of the focused character's outline. To use a custom palette color in TypeScript, augment |
| defaultValue | string | '' | The initial code for an uncontrolled field. |
| disabled | bool | - | Disable all the character inputs and omit the code from form submission. |
| error | bool | false | Display the field in an error state. |
| form | string | - | The ID of the form that owns the field. |
| fullWidth | bool | false | If |
| getInputAriaLabel | func | function defaultGetInputAriaLabel(position: number, length: number) { return `Character ${position} of ${length}`; } | Accessible name for a character after the first. Positions start at one. For localization purposes, you can use the provided translations. |
| helperText | node | - | The supporting or error text displayed below the inputs. |
| id | string | - | The ID of the first character input. Subsequent input IDs are derived from it. |
| inputMode | 'decimal' | 'email' | 'none' | 'numeric' | 'search' | 'tel' | 'text' | 'url' | - | The virtual keyboard hint. Defaults to the keyboard for the validation type. |
| inputRef | func | { current?: object } | - | A ref to the first visible character input. |
| label | node | - | The label associated with the first character input. |
| length | number | 6 | The number of characters in the code. When passing children, match the number of OTPFieldInput elements. |
| mask | bool | false | Mask the characters as they are entered. |
| name | string | - | The name used to submit the field value as one string. |
| normalizeValue | func | - | Normalize the filtered code before it is stored. The function should be idempotent. |
| onValueChange | func | - | Called with the new code and the change details. The change can be canceled. |
| onValueComplete | func | - | Called when the code becomes complete or a complete code is pasted, before optional form submission. |
| onValueInvalid | func | - | Called with the attempted input when filtering or normalization rejects characters. |
| readOnly | bool | - | Keep the field focusable while preventing edits. |
| render | element | func | - | Replaces the outer field element while preserving its styles and behavior. Accepts an element or a function that receives the root props and Base UI field state. |
| required | bool | - | Require a complete code before form submission. |
| size | 'medium' | 'small' | string | 'medium' | The size of the field. Augment |
| slotProps | { formHelperText?: func | object, group?: func | object, label?: func | object, root?: func | object } | - | Props for the Material slots and their corresponding Base UI parts. |
| slots | { formHelperText?: elementType, group?: elementType, label?: elementType, root?: elementType } | - | Components used to render the field's slots. |
| sx | Array<func | object | bool> | func | object | - | System styles for the outer root element. See the `sx` page for more details. |
| validationType | 'alpha' | 'alphanumeric' | 'none' | 'numeric' | 'numeric' | The set of characters accepted in the code. |
| value | string | - | The code when controlling the field. Use an empty string for an empty field. |
ref is forwarded to the root element.Theme default props
You can use MuiOTPField to change the default props of this component with the theme.
| Slot name | Class name | Default component | Description |
|---|---|---|---|
| root | .MuiOTPField-root | FormControl | The outer field container. |
| label | .MuiOTPField-label | FormLabel | The visible field label. |
| group | .MuiOTPField-group | 'div' | The host for Base UI OTPField.Root. |
| formHelperText | .MuiOTPField-formHelperText | FormHelperText | The supporting or error text. |
These class names are useful for styling with CSS. They are applied to the component's slots when specific states are triggered.
| Class name | Rule name | Description |
|---|---|---|
| .MuiOTPField-input | input | Styles applied to each character input. |
| .MuiOTPField-separator | separator | Styles applied to each decorative separator. |
You can override the style of the component using one of these customization options:
- With a global class name.
- With a rule name as part of the component's
styleOverridesproperty in a custom theme.
Source code
If you did not find the information in this page, consider having a look at the implementation of the component for more detail.