|
1
|
import { ComponentExample } from "@/components/ComponentExample" |
|
2
|
import { ComponentReference } from "@/components/ComponentReference" |
|
3
|
|
|
4
|
export const metadata = { |
|
5
|
title: `OTP Input`, |
|
6
|
} |
|
7
|
|
|
8
|
# {metadata.title} |
|
9
|
|
|
10
|
A component that renders a form input field for one-time passwords (OTP) using Medusa's design system. |
|
11
|
|
|
12
|
The `OtpInput` component is useful for scenarios where you need to input a code sent via email or SMS for verification purposes. |
|
13
|
|
|
14
|
In this guide, you'll learn how to use the OTP Input component. |
|
15
|
|
|
16
|
<Note> |
|
17
|
|
|
18
|
The `OTPInput` component is available since v4.1.13 of `@medusajs/ui`. |
|
19
|
|
|
20
|
</Note> |
|
21
|
|
|
22
|
<ComponentExample name="otp-input-demo" /> |
|
23
|
|
|
24
|
## Usage |
|
25
|
|
|
26
|
```tsx |
|
27
|
import { OtpInput } from "@medusajs/ui" |
|
28
|
``` |
|
29
|
|
|
30
|
```tsx |
|
31
|
const [otp, setOtp] = useState("") |
|
32
|
|
|
33
|
return <OtpInput value={otp} onChange={setOtp} /> |
|
34
|
``` |
|
35
|
|
|
36
|
--- |
|
37
|
|
|
38
|
## API Reference |
|
39
|
|
|
40
|
<ComponentReference mainComponent="OtpInput" /> |
|
41
|
|
|
42
|
--- |
|
43
|
|
|
44
|
## Examples |
|
45
|
|
|
46
|
### Handle OTP Completion |
|
47
|
|
|
48
|
Use the `onComplete` callback to react when the user fills in the full code. This is useful to automatically submit the code for verification once it's complete: |
|
49
|
|
|
50
|
<ComponentExample name="otp-input-on-complete" /> |
|
51
|
|
|
52
|
### 4-Digit PIN |
|
53
|
|
|
54
|
By default, the OTP input renders a 6-digit code. Use the `length` and `groupSize` props to render a shorter code without a separator: |
|
55
|
|
|
56
|
<ComponentExample name="otp-input-pin" /> |
|
57
|
|
|
58
|
### Custom Group Size |
|
59
|
|
|
60
|
By default, the OTP input groups digits in sets of `3`. Use the `groupSize` prop to control how many digits are rendered between each separator: |
|
61
|
|
|
62
|
<ComponentExample name="otp-input-group-size" /> |
|
63
|
|
|
64
|
### Custom Separator |
|
65
|
|
|
66
|
By default, the OTP input uses `-` as the separator. You can change it by setting the `separator` prop. It can be a string or a React node: |
|
67
|
|
|
68
|
<ComponentExample name="otp-input-custom-separator" /> |
|
69
|
|
|
70
|
### Error State |
|
71
|
|
|
72
|
You can leverage the native `aria-invalid` property to show an error state on the OTP input: |
|
73
|
|
|
74
|
<ComponentExample name="otp-input-error" hideFeedback /> |