PinInput
PinInput is a component used to capture pin codes or OTP (One-Time Password) inputs.
<PinInput.Root />
Usage
import { PinInput } from "@yamada-ui/react"
import { PinInput } from "@/components/ui"
import { PinInput } from "@workspaces/ui"
<PinInput.Root />
Change Variant
<VStack>
<For each={["outline", "filled", "flushed"]}>
{(variant) => <PinInput.Root key={variant} variant={variant} />}
</For>
</VStack>
Change Size
<VStack>
<For each={["xs", "sm", "md", "lg", "xl"]}>
{(size) => <PinInput.Root key={size} size={size} />}
</For>
</VStack>
Change Color Scheme
<VStack>
<For each={["green", "orange"]}>
{(colorScheme) => (
<PinInput.Root key={colorScheme} colorScheme={colorScheme} />
)}
</For>
</VStack>
Set Default Value
To set a default value, set a string to defaultValue.
<VStack>
<For each={["1234", "123"]}>
{(defaultValue) => (
<PinInput.Root key={defaultValue} defaultValue={defaultValue} />
)}
</For>
</VStack>
Change Placeholder
To change the placeholder, set a string to placeholder.
<PinInput.Root placeholder="💩" />
Change Number of Fields
To change the number of fields, set a number to items.
<VStack>
<For each={[3, 4, 5, 6]}>
{(items) => <PinInput.Root key={items} items={items} />}
</For>
</VStack>
Change Type
By default, only numeric input is allowed. To support alphanumeric, set type to alphanumeric.
<PinInput.Root type="alphanumeric" />
Use as One-Time Password
To use as a one-time password (autocomplete="one-time-code"), set otp to true.
<PinInput.Root otp />
Mask Entered Value
To mask the entered value, set mask to true.
<PinInput.Root mask />
Disabled
To disable the input, set disabled to true.
<PinInput.Root>
<For each={[0, 1, 2, 3]}>
{(index) => <PinInput.Field key={index} index={index} disabled />}
</For>
</PinInput.Root>
Read-Only
To make read-only, set readOnly to true.
<PinInput.Root>
<For each={[0, 1, 2, 3]}>
{(index) => <PinInput.Field key={index} index={index} readOnly />}
</For>
</PinInput.Root>
Invalid
To make invalid, set invalid to true.
<PinInput.Root>
<For each={[0, 1, 2, 3]}>
{(index) => <PinInput.Field key={index} index={index} invalid />}
</For>
</PinInput.Root>
Change Border Color
To change the border color, set focusBorderColor or errorBorderColor to the color.
<VStack>
<PinInput.Root focusBorderColor="green.500" />
<PinInput.Root errorBorderColor="orange.500" invalid />
</VStack>
Set Action on Completion
To set the action on completion, use the onComplete property.
const { page } = useLoading()
return <PinInput.Root onComplete={() => page.start({ duration: 5000 })} />
"use client" to the top of the file.Disable Focus Management
By default, when a field is entered, the focus automatically moves to the next field. Also, when the BackSpace key is pressed, the focus moves to the previous field. To disable this control, set manageFocus to false.
<PinInput.Root manageFocus={false} />
Customize Field
<PinInput.Root>
<For each={[0, 1, 2, 3]}>
{(index) => (
<PinInput.Field
key={index}
index={index}
_placeholder={{ color: "blue.500" }}
/>
)}
</For>
</PinInput.Root>
Control
const { page } = useLoading()
const [value, onChange] = useState("")
const onComplete = () => page.start({ duration: 5000 })
return (
<PinInput.Root value={value} onChange={onChange} onComplete={onComplete} />
)
"use client" to the top of the file.Props
Accessibility
If you are not using Field.Root, set aria-label or aria-labelledby to PinInput.Root.
<PinInput.Root aria-label="Please one-time password" />
<VStack gap="sm">
<Text as="h3" id="label">
Please one-time password
</Text>
<PinInput.Root aria-labelledby="label" />
</VStack>
Keyboard Navigation
| Key | Description | State |
|---|---|---|
Tab | Move the focus to the next item. | - |
ArrowRight | Move the focus to the next item. | - |
ArrowLeft | Move the focus to the previous item. | - |
ARIA Roles and Attributes
| Component | Role and Attributes | Usage |
|---|---|---|
PinInput.Root | role="group" | Indicates that this is a group. |
PinInput.Field | aria-invalid | Set to "true" if invalid is set. |
aria-disabled | Set to "true" if disabled is set. | |
aria-describedby | If PinInput.Field is within a Field.Root and Field.Root has an errorMessage, helperMessage, or a Field.ErrorMessage, Field.HelperMessage, sets its id. | |
aria-readonly | Set to "true" if readOnly is set. | |
aria-required | Set to "true" if required is set. |
Similar Components
Editable
Editable is a component used to obtain inline editable text input.
Input
Input is a component used to obtain text input from the user.
NumberInput
NumberInput is a component used to obtain numeric input from the user.
PasswordInput
PasswordInput is a component that allows users to input passwords with a visibility toggle.
Textarea
Textarea is a component used to obtain multi-line text input.
FileInput
FileInput is a component used for users to select files.
Autocomplete
Autocomplete is a component used to display suggestions in response to user text input.
Checkbox
Checkbox is a component used for allowing users to select multiple values from multiple options.