Input OTP
The InputOtp component enables users to enter one-time passwords (OTP). It is built on top of the input-otp library by @guilherme_rodz.
Installation
npx nextui-cli@latest add input-otp
The above command is for individual installation only. You may skip this step if @nextui-org/react
is already installed globally.
Import
Usage
Disabled
The isDisabled
prop disables user interaction with the InputOtp
component.
Read Only
The isReadOnly
prop makes the InputOtp
component read-only while maintaining its visual appearance.
Required
The isRequired
prop marks the InputOtp
as a required field.
Sizes
The size of the InputOtp
can be customized using the size
prop. The default value is md
.
Colors
Color of the InputOtp
can be changed by color
property.
Variants
Styling/Variant of the InputOtp
can be changed by variant
property. By default, variant
property is set to flat
.
Radius
Radius of the InputOtp
can be changed by radius
property. By default, radius
property is set to md
.
Password
InputOtp
can be used as password/secured-pin input by setting type
as password
.
Description
Description of the InputOtp
can be set by description
property.
Error Message
Custom error message of the InputOtp
can be set by errorMessage
property.
Allowed Keys
- The
InputOtp
component only accepts specified input keys. Any other input is ignored. - You can customize the allowed keys using the
allowedKeys
prop, which accepts a regex pattern. - By default,
allowedKeys
is set to^[0-9]*$
(only numerical digits).
Controlled
React Hook Form
You can use InputOtp
with React Hook Form for form validation and submission handling.
Different Lengths & Validation
The InputOtp
component supports different lengths through the length
property. You can set the number of input segments by passing a number value to the length
prop. Common use cases include 4-digit PINs and 6-digit authentication codes.
Custom Styles
You can customize the styles of the InputOtp
component using the classNames
prop.
Slots
- base: InputOtp wrapper, it handles alignment, placement, and general appearance.
- wrapper: Wraps the underlying input-otp component. Sent as
containerClassName
prop to underlying input-otp component. - input: The input element.
- segmentWrapper: Wraps all the segment elements.
- segment: The segment element.
- caret: The caret represents the typing indicator of the input-otp component.
- passwordChar: The passwordChar represents the text styling when input-type is password.
- helperWrapper: Wraps the
description
and theerrorMessage
. - description: The description of the input-otp.
- errorMessage: The error message of the input-otp.
Data Attributes
InputOtp
has the following attributes on the base
element:
- data-invalid:
When the input-otp is invalid. Based on
isInvalid
prop. - data-required:
When the input-otp is required. Based on
isRequired
prop. - data-readonly:
When the input-otp is readonly. Based on
isReadOnly
prop. - data-filled: When the input-otp is completely filled.
- data-disabled:
When the input-otp is disabled. Based on
isDisabled
prop.
InputOtp
also has the following attributes on the segment
element:
- data-active: When the segment is active.
- data-focus: When the segment is focused.
- data-focus-visible: When the segment is focused visible.
- data-has-value: When the segment has value.
Accessibility
- Built on top of input-otp.
- Required and invalid states exposed to assistive technology via ARIA.
- Support for description and error message help text linked to the input-otp via ARIA.
- Keyboard navigation:
- Tab: Moves focus between input segments
- Arrow keys: Navigate between segments
- Backspace: Clears current segment and moves focus to previous segment
- ARIA attributes:
aria-invalid
: Indicates validation statearia-required
: Indicates if the input is required
API
InputOtp Props
Attribute | Type | Description | Default |
---|---|---|---|
length | number | The length of the input-otp. | 4 |
allowedKeys | regEx string | The allowed keys for the input-otp. | ^[0-9]*$ |
variant | flat | bordered | faded | underlined | The variant of the input-otp. | flat |
color | default | primary | secondary | success | warning | danger | The color of the input-otp. | default |
size | sm | md | lg | The size of the input-otp. | md |
radius | none | sm | md | lg | full | The radius of the input-otp. | - |
value | string | The current value of the input-otp (controlled). | - |
defaultValue | string | The default value of the input-otp (uncontrolled). | - |
description | ReactNode | Provides additional information or hints about the input requirements. | - |
errorMessage | ReactNode | ((v: ValidationResult) => ReactNode) | An error message for the input-otp. It is only shown when isInvalid is set to true | - |
fullWidth | boolean | When true, the InputOtp expands to fill its parent container width. | false |
isRequired | boolean | Marks the input as required for form submission. | false |
isReadOnly | boolean | When true, allows selection but prevents user modification. | false |
isDisabled | boolean | Whether the input-otp is disabled. | false |
isInvalid | boolean | Whether the input-otp is invalid. | false |
baseRef | RefObject<HTMLDivElement> | The ref to the base element. | - |
disableAnimation | boolean | Whether the input-otp should be animated. | false |
classNames | Record<"base"| "inputWrapper"| "input"| "segmentWrapper"| "segment" | "caret" | "passwordChar" | "helperWrapper" | "description" | "errorMessage", string> | Allows to set custom class names for the Input slots. | - |
textAlign | left | center | right | The text alignment of the input segments | center |
pushPasswordManagerStrategy | "none" | "hidden" | "input" | Strategy for password manager support | - |
pasteTransformer | (text: string) => string | A function to transform pasted text before it's inserted into the input. | - |
containerClassName | string | Class name for the input container | - |
noScriptCSSFallback | string | CSS fallback for when JavaScript is disabled | - |
InputOtp Events
Attribute | Type | Description |
---|---|---|
onChange | React.ChangeEvent<HTMLInputElement> | Handler that is called when the element's value changes. You can pull out the new value by accessing event.target.value (string). |
onValueChange | (value: string) => void | Handler that is called when the element's value changes. |
onComplete | (value: string) => void | Handler that is called when the element's value is completely filled. |