Pin Input

For pin or verification codes with auto-focus transfer and masking options.

Loading...

Anatomy

<PinInput.Root>
  <PinInput.Label />
  <PinInput.Control>
    <PinInput.Input />
  </PinInput.Control>
  <PinInput.HiddenInput />
</PinInput.Root>

Examples

Placeholder

Set placeholder to replace the default ○.

Blur on Complete

The last input keeps focus when filled. To blur it on completion, blur the focused element in onValueComplete.

OTP Mode

Set otp to true to add autocomplete="one-time-code", which triggers OTP auto-suggestion on phones.

Masking

Set mask to true to hide sensitive values, like <input type="password"/>.

Change Events

  • onValueChange: the value changed.
  • onValueComplete: all fields are filled, by typing or pasting.
  • onValueInvalid: the entered value doesn't match the type.

Verify on Complete

Verify the code in onValueComplete. If it's wrong, set invalid and the control shakes.

Field

Field wires up the label, helper text, error text and ARIA attributes.

Root Provider

Use RootProvider with the usePinInput hook to access the state and methods from outside the component.

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
count
number

The number of pin input fields.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
autoFocus
boolean

Whether to auto-focus the first input.

autoSubmit
boolean

Whether to auto-submit the owning form when all inputs are filled.

defaultValue
string[]

The initial value of the the pin input when rendered. Use when you don't need to control the value of the pin input.

disabled
boolean

Whether the inputs are disabled

form
string

The associate form of the underlying input element.

id
string

The unique identifier of the machine.

ids
ElementIds

The ids of the elements in the pin input. Useful for composition.

invalid
boolean

Whether the pin input is in the invalid state

mask
boolean

If `true`, the input's value will be masked just like `type=password`

name
string

The name of the input element. Useful for form submission.

normalizeValue
(value: string) => string

Function to normalize typed or pasted text, such as uppercasing it.

onValueChange
(details: ValueChangeDetails) => void

Function called on input change

onValueComplete
(details: ValueChangeDetails) => void

Function called when all inputs have valid values

onValueInvalid
(details: ValueInvalidDetails) => void

Function called when an invalid value is entered

otp
boolean

If `true`, the pin input component signals to its fields that they should use `autocomplete="one-time-code"`.

pattern
string

The regular expression that the user-entered input value is checked against.

placeholder'○'
string

The placeholder text for the input

readOnly
boolean

Whether the pin input is in the valid state

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<PinInputRootState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

required
boolean

Whether the pin input is required

translations
IntlTranslations

Specifies the localized strings that identifies the accessibility elements and their states

type'numeric'
'numeric' | 'alphanumeric' | 'alphabetic'

The type of value the pin-input should allow

value
string[]

The controlled value of the the pin input.

AttributeDescription
[data-pin-input-root]<uid>
[data-invalid]Present when invalid
[data-disabled]Present when disabled
[data-complete]Present when the pin-input value is complete
[data-readonly]Present when read-only

Control

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

HiddenInput

Defines the <input> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Input

Defines the <input> element.

AttributeDefaultType
index
number

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<PinInputInputState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-pin-input-input]<uid>
[data-disabled]Present when disabled
[data-complete]Present when the input value is complete
[data-filled]
[data-index]The index of the item
[data-invalid]Present when invalid

Label

Defines the <label> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<PinInputLabelState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-pin-input-label]<uid>
[data-invalid]Present when invalid
[data-disabled]Present when disabled
[data-complete]Present when the label value is complete
[data-required]Present when required
[data-readonly]Present when read-only

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UsePinInputReturn

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<PinInputRootProviderState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Context

API

PropertyType
value
string[]

The value of the input as an array of strings.

valueAsString
string

The value of the input as a string.

complete
boolean

Whether all inputs are filled.

count
number

The number of inputs to render

items
number[]

The array of input values.

setValue
(value: string[]) => void

Function to set the value of the inputs.

clearValue
VoidFunction

Function to clear the value of the inputs.

setValueAtIndex
(index: number, value: string) => void

Function to set the value of the input at a specific index.

focus
VoidFunction

Function to focus the pin-input. This will focus the first input.

getRootState
() => RootState

Returns the state of the root.

getInputState
(props: InputProps) => InputState

Returns the state of a given input.

Accessibility

Keyboard Support

KeyDescription
ArrowLeft
Moves focus to the previous input
ArrowRight
Moves focus to the next input
HomeArrowUpControl + ArrowLeft
Moves focus to the first input
EndArrowDownControl + ArrowRight
Moves focus to the last filled input
Backspace
Deletes the value in the current input and moves focus to the previous input
Delete
Deletes the value in the current input
Control + V
Pastes the value into the input fields