import { Text } from "@senvenli/react/display";
import { OtpField } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";
export default function Example() {
const [code, setCode] = useState<string | null>(null);
return (
<Stack space="space-150">
<OtpField
label="Verification code"
description="We sent a 6-digit code to ada@example.com."
onChange={() => setCode(null)}
onComplete={setCode}
/>
{code && <Text size="body-small" tone="subtle">Checking {code}…</Text>}
</Stack>
);
}import { OtpField } from "@senvenli/react/form";Examples
Four digits with resend
The field takes the code; a Button with `useCooldownAction` sends it again after a wait.import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { useCooldownAction } from "./use-cooldown-action";
import { OtpField } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
// The field takes the code; "Send again" is a Button with useCooldownAction, which waits after each send and starts
// no wait (and says so) when a send fails.
export default function Example() {
// The first code was just sent, so the 30-second wait is already running.
const resend = useCooldownAction(() => new Promise((done) => setTimeout(done, 800)), 30, 30);
return (
<Stack space="space-150">
<OtpField label="Code from your text message" length={4} description="Sent to the number ending in 0132." />
<Stack space="space-050">
<div>
<Button size="small" variant="subtle" isDisabled={resend.remaining > 0} isPending={resend.isPending} onPress={() => void resend.run()}>
{resend.remaining > 0 ? `Send again in 0:${String(resend.remaining).padStart(2, "0")}` : "Send again"}
</Button>
</div>
<span role="status">{resend.failed && <Text size="body-small" tone="danger">The code was not sent. Try again.</Text>}</span>
</Stack>
</Stack>
);
}A wrong code
The error says what to do next.import { OtpField } from "@senvenli/react/form";
export default function Example() {
return (
<OtpField
label="Verification code"
defaultValue="482917"
isInvalid
errorMessage="Wrong code. Enter the latest code from your email."
/>
);
}Guidelines
When to use it
- Entering a code sent by text message or email.
Use something else when
- The code is long, mixed or pasted from elsewhere, with
type="number"or text. Input
Accessibility
Built in
- One input under the boxes, so autofill and screen readers see a single field.
- The resend countdown is announced.
You provide
- A label that says where the code was sent.
API reference
OtpField
| Prop | Type | Default | Description |
|---|---|---|---|
labelRequired | React.ReactNode | – | |
length | 4 | 6 | – | 4 or 6 digits; 6 splits 3 + 3 with a dash. |
description | React.ReactNode | – | |
errorMessage | React.ReactNode | – | Say what to do: "Wrong code. Enter the latest code from your phone." |
onComplete | (code: string) => void | – | Called once the last digit is entered (submit there). |
requiredValidation | RequiredValidation | – | Who says an empty required field is missing: |
motion | ComponentMotion | – |
|
labelRequiredReact.ReactNodelength4 | 64 or 6 digits; 6 splits 3 + 3 with a dash.
descriptionReact.ReactNodeerrorMessageReact.ReactNodeSay what to do: "Wrong code. Enter the latest code from your phone."
onComplete(code: string) => voidCalled once the last digit is entered (submit there).
requiredValidationRequiredValidationWho says an empty required field is missing:
field(default) orapp. Withappa DS Form'sonSubmitruns with the field empty, and the app's own error must clear on change. See {@link RequiredValidation} for the whole contract.motionComponentMotionauto(follow the device),reduceornone, for this component; the provider's setting otherwise.