The InputOTP component provides a one-time password input interface with individual slots for each digit. It's commonly used for two-factor authentication, verification codes, and secure input fields.
import { InputOTP } from '@dashflowx/core'export default function InputOTPExample() {return (<InputOTP />)}
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
maxLength | number | 6 | Maximum number of digits |
className | string | '' | Additional CSS classes for the container |
import { InputOTP } from '@dashflowx/core'export default function BasicInputOTP() {return (<InputOTP />)}
Preview:
import { InputOTP } from '@dashflowx/core'export default function FourDigitOTP() {return (<InputOTP maxLength={4} />)}
Preview:
import { InputOTP } from '@dashflowx/core'export default function EightDigitOTP() {return (<InputOTP maxLength={8} />)}
Preview:
import { InputOTP } from '@dashflowx/core'export default function CustomStyledOTP() {return (<InputOTPmaxLength={6}className="border-2 border-blue-500 rounded-xl p-4"/>)}
Preview:
import { InputOTP } from '@dashflowx/core'export default function LargeOTP() {return (<InputOTPmaxLength={6}className="scale-150"/>)}
Preview:
const twoFactorAuth = (<div className="space-y-4"><h3 className="text-lg font-semibold">Enter verification code</h3><p className="text-gray-600">We sent a 6-digit code to your phone</p><InputOTP maxLength={6} /><button className="w-full bg-blue-600 text-white py-2 px-4 rounded-md">Verify</button></div>);
const emailVerification = (<div className="space-y-4"><h3 className="text-lg font-semibold">Verify your email</h3><p className="text-gray-600">Enter the 4-digit code sent to your email</p><InputOTP maxLength={4} /><button className="w-full bg-green-600 text-white py-2 px-4 rounded-md">Confirm</button></div>);
const passwordReset = (<div className="space-y-4"><h3 className="text-lg font-semibold">Reset your password</h3><p className="text-gray-600">Enter the 8-digit reset code</p><InputOTP maxLength={8} /><button className="w-full bg-red-600 text-white py-2 px-4 rounded-md">Reset Password</button></div>);
const accountVerification = (<div className="space-y-4"><h3 className="text-lg font-semibold">Verify your account</h3><p className="text-gray-600">Enter the verification code from your authenticator app</p><InputOTP maxLength={6} /><button className="w-full bg-purple-600 text-white py-2 px-4 rounded-md">Verify Account</button></div>);
const transactionConfirmation = (<div className="space-y-4"><h3 className="text-lg font-semibold">Confirm transaction</h3><p className="text-gray-600">Enter the 6-digit confirmation code</p><InputOTP maxLength={6} /><button className="w-full bg-orange-600 text-white py-2 px-4 rounded-md">Confirm Transaction</button></div>);
Use appropriate OTP lengths for different use cases.
// ✅ Good - 6 digits for 2FA<InputOTP maxLength={6} />// ✅ Good - 4 digits for email verification<InputOTP maxLength={4} />// ❌ Avoid - Too short for security<InputOTP maxLength={2} />
Provide clear instructions for users.
// ✅ Good - Clear instructions<div><p>Enter the 6-digit code sent to your phone</p><InputOTP maxLength={6} /></div>// ❌ Avoid - Unclear instructions<div><p>Enter code</p><InputOTP maxLength={6} /></div>
Maintain consistent styling across your application.
// ✅ Good - Consistent styling<InputOTP className="standard-otp-style" />// ❌ Avoid - Inconsistent styling<InputOTP className="random-styles" />
Ensure OTP inputs are accessible to all users.
// ✅ Good - Accessible<InputOTP maxLength={6} />// ❌ Avoid - Hidden or inaccessible<InputOTP maxLength={6} className="sr-only" />
Provide clear error messages for invalid codes.
// ✅ Good - Error handling<div><InputOTP maxLength={6} />{error && <p className="text-red-500">Invalid code. Please try again.</p>}</div>
You can customize the OTP input appearance using CSS classes:
<InputOTPmaxLength={6}className="border-2 border-blue-500 rounded-xl p-4"/>
Use different themes for different contexts:
// Light theme<InputOTP className="bg-white border-gray-200" />// Dark theme<InputOTP className="bg-gray-900 border-gray-700 text-white" />// Brand theme<InputOTP className="bg-blue-50 border-blue-200" />
Use different sizes for different emphasis levels:
// Small OTP<InputOTP className="scale-75" />// Standard OTP<InputOTP />// Large OTP<InputOTP className="scale-150" />
Change OTP length based on requirements:
const [otpLength, setOtpLength] = useState(6);<InputOTP maxLength={otpLength} />
Automatically focus the first input:
const firstInputRef = useRef(null);useEffect(() => {if (firstInputRef.current) {firstInputRef.current.focus();}}, []);
Implement real-time validation:
const [otpValue, setOtpValue] = useState('');const [isValid, setIsValid] = useState(false);const validateOTP = (value) => {const isValid = value.length === 6 && /^\d+$/.test(value);setIsValid(isValid);};
Automatically submit when complete:
const handleOTPComplete = (value) => {if (value.length === 6) {// Auto-submitsubmitOTP(value);}};
- Input: For text input fields
- Form: For form management
- Button: For submission buttons
- Alert: For error messages
- Card: For OTP containers
For the complete API reference and advanced usage patterns, see the InputOTP API documentation.
