# Pin Input

URL: https://ark-ui.com/docs/react/components/pin-input
Markdown: https://ark-ui.com/docs/react/components/pin-input.md

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

---

---
id: pin-input
title: Pin Input
description: For pin or verification codes with auto-focus transfer and masking options.
---



## Anatomy



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

## Examples

```tsx
import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const Basic = () => (
  <PinInput.Root className={styles.Root} count={3}>
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)
```

### Placeholder

Set `placeholder` to replace the default `○`.

```tsx
import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const CustomPlaceholder = () => (
  <PinInput.Root className={styles.Root} count={3} placeholder="*">
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)
```

### Blur on Complete

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

```tsx
import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const BlurOnComplete = () => (
  <PinInput.Root
    className={styles.Root}
    count={3}
    onValueComplete={() => (document.activeElement as HTMLElement | null)?.blur()}
  >
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)
```

### OTP Mode

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

```tsx
import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const OTPMode = () => (
  <PinInput.Root className={styles.Root} count={3} otp>
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)
```

### Masking

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

```tsx
import { PinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const Mask = () => (
  <PinInput.Root className={styles.Root} count={3} mask>
    <PinInput.Label className={styles.Label}>Label</PinInput.Label>
    <PinInput.Control className={styles.Control}>
      {[0, 1, 2].map((id, index) => (
        <PinInput.Input key={id} index={index} className={styles.Input} />
      ))}
    </PinInput.Control>
    <PinInput.HiddenInput />
  </PinInput.Root>
)
```

### 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.

```tsx
import { PinInput } from '@ark-ui/react/pin-input'
import { useState } from 'react'
import styles from 'styles/pin-input.module.css'

const expected = '1234'

export const VerifyOnComplete = () => {
  const [invalid, setInvalid] = useState(false)
  const [verified, setVerified] = useState(false)

  return (
    <div className="stack">
      <PinInput.Root
        className={styles.Root}
        count={4}
        otp
        invalid={invalid}
        onValueChange={() => {
          setInvalid(false)
          setVerified(false)
        }}
        onValueComplete={(details) => {
          const matches = details.valueAsString === expected
          setInvalid(!matches)
          setVerified(matches)
        }}
      >
        <PinInput.Label className={styles.Label}>Enter {expected} to verify</PinInput.Label>
        <PinInput.Control className={styles.Control}>
          {[0, 1, 2, 3].map((id, index) => (
            <PinInput.Input key={id} index={index} className={styles.Input} />
          ))}
        </PinInput.Control>
        <PinInput.HiddenInput />
      </PinInput.Root>
      {invalid && <p>Invalid code</p>}
      {verified && <p>Code verified</p>}
    </div>
  )
}
```

### Field

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

```tsx
import { Field } from '@ark-ui/react/field'
import { PinInput } from '@ark-ui/react/pin-input'
import fieldStyles from 'styles/field.module.css'
import styles from 'styles/pin-input.module.css'

export const WithField = () => (
  <Field.Root className={fieldStyles.Root}>
    <PinInput.Root className={styles.Root} count={3}>
      <PinInput.Label className={styles.Label}>Label</PinInput.Label>
      <PinInput.Control className={styles.Control}>
        {[0, 1, 2].map((id, index) => (
          <PinInput.Input key={id} index={index} className={styles.Input} />
        ))}
      </PinInput.Control>
      <PinInput.HiddenInput />
    </PinInput.Root>
    <Field.HelperText className={fieldStyles.HelperText}>Additional Info</Field.HelperText>
    <Field.ErrorText className={fieldStyles.ErrorText}>Error Info</Field.ErrorText>
  </Field.Root>
)
```

### Root Provider

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

```tsx
import { PinInput, usePinInput } from '@ark-ui/react/pin-input'
import styles from 'styles/pin-input.module.css'

export const RootProvider = () => {
  const pinInput = usePinInput({ count: 3, onValueComplete: (e) => alert(e.valueAsString) })

  return (
    <div className="stack">
      <button onClick={() => pinInput.focus()}>Focus</button>

      <PinInput.RootProvider value={pinInput} className={styles.Root}>
        <PinInput.Label className={styles.Label}>Label</PinInput.Label>
        <PinInput.Control className={styles.Control}>
          {[0, 1, 2].map((id, index) => (
            <PinInput.Input key={id} index={index} className={styles.Input} />
          ))}
        </PinInput.Control>
        <PinInput.HiddenInput />
      </PinInput.RootProvider>
    </div>
  )
}
```

## API Reference

### Props

### Root

#### Props

**`count`**
Type: `number`
Required: true
Default Value: `undefined`
Description: The number of pin input fields.

**`asChild`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: 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} />}`.

**`autoFocus`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: Whether to auto-focus the first input.

**`autoSubmit`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: Whether to auto-submit the owning form when all inputs are filled.

**`defaultValue`**
Type: `string[]`
Required: false
Default Value: `undefined`
Description: 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`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: Whether the inputs are disabled

**`form`**
Type: `string`
Required: false
Default Value: `undefined`
Description: The associate form of the underlying input element.

**`id`**
Type: `string`
Required: false
Default Value: `undefined`
Description: The unique identifier of the machine.

**`ids`**
Type: `ElementIds`
Required: false
Default Value: `undefined`
Description: The ids of the elements in the pin input. Useful for composition.

**`invalid`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: Whether the pin input is in the invalid state

**`mask`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: If `true`, the input's value will be masked just like `type=password`

**`name`**
Type: `string`
Required: false
Default Value: `undefined`
Description: The name of the input element. Useful for form submission.

**`normalizeValue`**
Type: `(value: string) => string`
Required: false
Default Value: `undefined`
Description: Function to normalize typed or pasted text, such as uppercasing it.

**`onValueChange`**
Type: `(details: ValueChangeDetails) => void`
Required: false
Default Value: `undefined`
Description: Function called on input change

**`onValueComplete`**
Type: `(details: ValueChangeDetails) => void`
Required: false
Default Value: `undefined`
Description: Function called when all inputs have valid values

**`onValueInvalid`**
Type: `(details: ValueInvalidDetails) => void`
Required: false
Default Value: `undefined`
Description: Function called when an invalid value is entered

**`otp`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: If `true`, the pin input component signals to its fields that they should
use `autocomplete="one-time-code"`.

**`pattern`**
Type: `string`
Required: false
Default Value: `undefined`
Description: The regular expression that the user-entered input value is checked against.

**`placeholder`**
Type: `string`
Required: false
Default Value: `"○"`
Description: The placeholder text for the input

**`readOnly`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: Whether the pin input is in the valid state

**`render`**
Type: `ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<PinInputRootState>`
Required: false
Default Value: `undefined`
Description: 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`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: Whether the pin input is required

**`translations`**
Type: `IntlTranslations`
Required: false
Default Value: `undefined`
Description: Specifies the localized strings that identifies the accessibility elements and their states

**`type`**
Type: `'numeric' | 'alphanumeric' | 'alphabetic'`
Required: false
Default Value: `"numeric"`
Description: The type of value the pin-input should allow

**`value`**
Type: `string[]`
Required: false
Default Value: `undefined`
Description: The controlled value of the the pin input.

#### Data Attributes

**`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

#### Props

**`asChild`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: 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} />}`.

**`render`**
Type: `ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>`
Required: false
Default Value: `undefined`
Description: 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

#### Props

**`asChild`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: 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} />}`.

**`render`**
Type: `ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>`
Required: false
Default Value: `undefined`
Description: 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

#### Props

**`index`**
Type: `number`
Required: true
Default Value: `undefined`
Description: undefined

**`asChild`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: 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} />}`.

**`render`**
Type: `ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<PinInputInputState>`
Required: false
Default Value: `undefined`
Description: 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.

#### Data Attributes

**`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

#### Props

**`asChild`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: 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} />}`.

**`render`**
Type: `ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<PinInputLabelState>`
Required: false
Default Value: `undefined`
Description: 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.

#### Data Attributes

**`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

#### Props

**`value`**
Type: `UsePinInputReturn`
Required: true
Default Value: `undefined`
Description: undefined

**`asChild`**
Type: `boolean`
Required: false
Default Value: `undefined`
Description: 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} />}`.

**`render`**
Type: `ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<PinInputRootProviderState>`
Required: false
Default Value: `undefined`
Description: 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 | Description |
|----------|------|-------------|
| `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

**`ArrowLeft`**
Description: Moves focus to the previous input

**`ArrowRight`**
Description: Moves focus to the next input

**`Home + ArrowUp + Control + ArrowLeft`**
Description: Moves focus to the first input

**`End + ArrowDown + Control + ArrowRight`**
Description: Moves focus to the last filled input

**`Backspace`**
Description: Deletes the value in the current input and moves focus to the previous input

**`Delete`**
Description: Deletes the value in the current input

**`Control + V`**
Description: Pastes the value into the input fields
