PinInput
PinInput
is a component used to capture pin codes or OTP (One-Time Password) inputs.
Import
import { PinInput, PinInputField } from "@yamada-ui/react"
PinInput
: A wrapper component that controls the child elements (input fields).PinInputField
: A component that displays the input field.
PinInputField
can be omitted.
Usage
Editable example
<PinInput />
Change Variant
Editable example
<VStack> <PinInput variant="outline" /> <PinInput variant="filled" /> <PinInput variant="flushed" /> <PinInput variant="unstyled" /> </VStack>
Change Size
Editable example
<VStack> <PinInput size="xs" /> <PinInput size="sm" /> <PinInput size="md" /> <PinInput size="lg" /> </VStack>
Set Default Value
To set a default value, set a string or number to defaultValue
.
Editable example
<VStack> <PinInput defaultValue="1234" /> <PinInput defaultValue="123" /> </VStack>
Change Number of Fields
To change the number of fields, set a number to items
.
Editable example
<VStack> <PinInput items={3} /> <PinInput items={4} /> <PinInput items={5} /> <PinInput items={6} /> </VStack>
Change Border Color
To change the border color, use the focusBorderColor
or errorBorderColor
properties.
Editable example
<VStack> <PinInput focusBorderColor="green.500" /> <PinInput isInvalid errorBorderColor="orange.500" /> </VStack>
Change Placeholder
To change the placeholder, set a string to placeholder
.
Editable example
<PinInput placeholder="💩" />
Change Type
By default, only numeric input is allowed. To support alphanumeric, set type
to alphanumeric
.
Editable example
<PinInput type="alphanumeric" />
Set Action on Completion
To set the action on completion, use the onComplete
property.
Editable example
const { page } = useLoading() return <PinInput onComplete={() => page.start({ duration: 5000 })} />
Use as One-Time Password
To use as a one-time password (autocomplete="one-time-code"
), set otp
to true
.
Editable example
<PinInput otp />
Mask Entered Value
To mask the entered value, set mask
to true
.
Editable example
<PinInput mask />
Use Custom Field
To use a custom field, use PinInputField
.
Editable example
<PinInput> <PinInputField /> <PinInputField /> <PinInputField /> <PinInputField /> </PinInput>
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
.
Editable example
<PinInput manageFocus={false} />
Disable
To disable, set isDisabled
to true
.
Editable example
<VStack> <PinInput isDisabled /> <PinInput> <PinInputField isDisabled /> <PinInputField isDisabled /> <PinInputField isDisabled /> <PinInputField isDisabled /> </PinInput> <FormControl isDisabled label="Please one-time password" helperMessage="Just sent you a one-time password to your e-mail address." errorMessage="one-time password is required." > <PinInput /> </FormControl> </VStack>
Make Read-Only
To make read-only, set isReadOnly
to true
.
Editable example
<VStack> <PinInput isReadOnly /> <PinInput> <PinInputField isReadOnly /> <PinInputField isReadOnly /> <PinInputField isReadOnly /> <PinInputField isReadOnly /> </PinInput> <FormControl isReadOnly label="Please one-time password" helperMessage="Just sent you a one-time password to your e-mail address." errorMessage="one-time password is required." > <PinInput /> </FormControl> </VStack>
Make Input Invalid
To make the input invalid, set isInvalid
to true
.
Editable example
<VStack> <PinInput isInvalid /> <PinInput> <PinInputField isInvalid /> <PinInputField isInvalid /> <PinInputField isInvalid /> <PinInputField isInvalid /> </PinInput> <FormControl isInvalid label="Please one-time password" helperMessage="Just sent you a one-time password to your e-mail address." errorMessage="one-time password is required." > <PinInput /> </FormControl> </VStack>
Control
Editable example
const { page } = useLoading() const [value, onChange] = useState("") const onComplete = () => page.start({ duration: 5000 }) return <PinInput value={value} onChange={onChange} onComplete={onComplete} />
Use React Hook Form
Editable example
type Data = { pinInput: string } const { control, handleSubmit, watch, formState: { errors }, } = useForm<Data>() const onSubmit: SubmitHandler<Data> = (data) => console.log("submit:", data) console.log("watch:", watch()) return ( <VStack as="form" onSubmit={handleSubmit(onSubmit)}> <FormControl isInvalid={!!errors.pinInput} label="Token" errorMessage={errors.pinInput ? errors.pinInput.message : undefined} > <Controller name="pinInput" control={control} rules={{ required: { value: true, message: "This is required." }, minLength: { value: 4, message: "This is required." }, }} render={({ field }) => <PinInput {...field} />} /> </FormControl> <Button type="submit" alignSelf="flex-end"> Submit </Button> </VStack> )
Edit this page on GitHub