Pin Input
For pin or verification codes with auto-focus transfer and masking options.
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 thetype.
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.
| Attribute | Default | Type |
|---|---|---|
count | numberThe number of pin input fields. | |
asChild | booleanUse 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 | booleanWhether to auto-focus the first input. | |
autoSubmit | booleanWhether 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 | booleanWhether the inputs are disabled | |
form | stringThe associate form of the underlying input element. | |
id | stringThe unique identifier of the machine. | |
ids | ElementIdsThe ids of the elements in the pin input. Useful for composition. | |
invalid | booleanWhether the pin input is in the invalid state | |
mask | booleanIf `true`, the input's value will be masked just like `type=password` | |
name | stringThe name of the input element. Useful for form submission. | |
normalizeValue | (value: string) => stringFunction to normalize typed or pasted text, such as uppercasing it. | |
onValueChange | (details: ValueChangeDetails) => voidFunction called on input change | |
onValueComplete | (details: ValueChangeDetails) => voidFunction called when all inputs have valid values | |
onValueInvalid | (details: ValueInvalidDetails) => voidFunction called when an invalid value is entered | |
otp | booleanIf `true`, the pin input component signals to its fields that they should use `autocomplete="one-time-code"`. | |
pattern | stringThe regular expression that the user-entered input value is checked against. | |
placeholder | '○' | stringThe placeholder text for the input |
readOnly | booleanWhether 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 | booleanWhether the pin input is required | |
translations | IntlTranslationsSpecifies 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. |
| Attribute | Description |
|---|---|
[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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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.
| Attribute | Default | Type |
|---|---|---|
index | number | |
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[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.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse 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. |
| Attribute | Description |
|---|---|
[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.
| Attribute | Default | Type |
|---|---|---|
value | UsePinInputReturn | |
asChild | booleanUse 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
| Property | Type |
|---|---|
value | string[]The value of the input as an array of strings. |
valueAsString | stringThe value of the input as a string. |
complete | booleanWhether all inputs are filled. |
count | numberThe number of inputs to render |
items | number[]The array of input values. |
setValue | (value: string[]) => voidFunction to set the value of the inputs. |
clearValue | VoidFunctionFunction to clear the value of the inputs. |
setValueAtIndex | (index: number, value: string) => voidFunction to set the value of the input at a specific index. |
focus | VoidFunctionFunction to focus the pin-input. This will focus the first input. |
getRootState | () => RootStateReturns the state of the root. |
getInputState | (props: InputProps) => InputStateReturns the state of a given input. |
Accessibility
Keyboard Support
| Key | Description |
|---|---|
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 |