Gathering detailed insights and metrics for antd-input-otp
Gathering detailed insights and metrics for antd-input-otp
Gathering detailed insights and metrics for antd-input-otp
Gathering detailed insights and metrics for antd-input-otp
react-otp-input
A fully customizable, one-time password input component for the web built with React
input-otp
One-time password input component for React.
ng-otp-input
A fully customizable, one-time password input component for the web built with Angular.
mui-one-time-password-input
A One-Time Password input designed for the React library MUI
An OTP Input Component based on Ant Design Component Library for React.
npm install antd-input-otp
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
19 Stars
22 Commits
5 Forks
1 Watching
2 Branches
1 Contributors
Updated on 26 Mar 2024
TypeScript (80.6%)
JavaScript (17.86%)
Shell (0.96%)
CSS (0.57%)
Cumulative downloads
Total Downloads
Last day
5.7%
745
Compared to previous day
Last week
9.6%
3,579
Compared to previous week
Last month
5.8%
13,361
Compared to previous month
Last year
723.5%
139,978
Compared to previous year
43
A custom input component for OTP (One Time Password) based on Ant Design Input for React.
npm install antd-input-otp
yarn add antd-input-otp
Simply import like other antd's components as usual and call it. You can use it uncontrolled with Form
from Ant Design Form or use it controlled with React useState.
Keep in mind this component will return undefined or array of string.
1import { Button, Form } from 'antd'; 2import { InputOTP } from 'antd-input-otp'; // Don't forget to import this too! 3 4const InputOTPPage = () => { 5 const [form] = Form.useForm(); 6 7 const handleFinish = (values) => { 8 // Your logic 9 }; 10 11 return ( 12 <Form onFinish={handleFinish} form={form}> 13 <Form.Item label="OTP" name="otp"> 14 <InputOTP autoSubmit={form} inputType="numeric" /> 15 </Form.Item> 16 17 <Form.Item> 18 <Button htmlType="submit">Submit</Button> 19 </Form.Item> 20 </Form> 21 ); 22};
1import { useState } from 'react'; 2import { Button } from 'antd'; 3import { InputOTP } from 'antd-input-otp'; // Don't forget to import this too! 4 5const InputOTPPage = () => { 6 const [value, setValue] = useState([]); // Since the value will be array of string, the default value of state is empty array. 7 8 const handleFinish = (otp) => { 9 const payload = otp || value; // Since useState work asynchronously, we shall add the field value from the autoSubmit. 10 // Your logic with state 11 }; 12 13 return ( 14 <div> 15 <InputOTP onChange={setValue} value={value} autoSubmit={handleFinish} /> 16 17 <Button onClick={() => handleFinish()}>Submit</Button> 18 </div> 19 ); 20};
Keep in mind, the props will be extended to antd InputProps
, which means properties that are not listed below can be seen on Ant Design Input.
Property | Type | Default Value | Description |
---|---|---|---|
autoFocus | boolean | false | Autofocus for the first field of OTP. If you want to make the second or third or even the last field autofocused, use `inputRef. |
autoSubmit | FormInstance | (value: string[]) => void | null | null | Autosubmit when the value is fully filled. |
disabled | boolean | false | |
inputClassName | string | Classes for styling input field. | |
inputRef | Mutable Reference Object (InputRef[], null[], null) | null | Reference for the input fields. Inside of the current should be array of InputRef from antd. |
inputRegex | RegExp or string when inputType set as custom .Other than that is never | If you choose custom as inputType , inputRegex will be mandatory.Wrote your validation with regex here. | |
inputStyle | CSSProperties | Inline style input field. | |
inputType | all | alphabet | alphabet-numeric | alphabet-symbol | numeric | numeric-symbol | symbol | custom | all | custom validation will be requiring your own regex on inputType .Selecting all as the value will invalidate the field and every field can be filled with anything. |
length | number | 6 | Determine the total of your fields. Keep in mind the minimum value is 2 and the maximum value is 16 . The length will stay on the limit if you fill it outside the limit. |
isPreservedFocus | boolean | false | Determine whether the input is still focused or not when every field is filled. |
onChange | (value: string[]) => void | ||
placeholder | string | Placeholder for each field. When the value is only one character It will apply to all fields with the same value. For example, if you put "x" , all fields will have x as placeholder.If you want to keep it unique for each field, you must input the characters with the length same as the field. For example, if you have 6 fields and want to keep the placeholder unique to each field, the value should be "x_x_x_" . | |
value | string[] | null | ||
wrapperClassName | string | Classes for styling input wrapper. | |
wrapperStyle | CSSProperties | Inline style input wrapper. |
autoSubmit
on controlled field, why my value always late 1 step?A: You see, React useState works asynchronously, so when autoSubmit is triggered, the setState is not running yet. The solution so far is by using the value from the argument of the functions. You can see this in the example above.
create-react-app
, why I always get an error Failed to parse source map from...
?A: To be honest, I am not quite sure about this, but the solution for now is to add this line of code inside your .env
file.
GENERATE_SOURCEMAP=false
A: YES! But you should update it to v1.1.0 first. Keep in mind, the value that you copy should be suitable with your inputType, for example if you have 030212
in your clipboard, and your inputType is numeric
, it will work. But it will be a different story when your value is A023@c!
, it won't work.
A: This component will return an array of strings if erased or already filled, or undefined if this component remains untouched.
A: Ofcourse, the best part is you don't need to setup anything. Just write and go!
A: You bet! Use it like other input with onChange and value.
A: Interesting, first of all, add a class on your InputOTP's Form.Item. then add text-align: center
on .ant-form-item .ant-form-item-explain-error
.
If you are using antd v5, be sure to wrap .ant-form-item
with :where()
, so it will become like this..
1.your-classname:where(.ant-form-item) .ant-form-item-explain-error { 2 text-align: center; 3}
Having another question? Ask me on the github issue!
No vulnerabilities found.
No security vulnerabilities found.