2.1.0 • Published 2 months ago

antd-input-otp v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

antd-input-otp 📱

A custom input component for OTP (One Time Password) based on Ant Design Input for React.

antd-input-otp

Demo 🧪

Prerequisite ⭐

Install 💻

npm

npm install antd-input-otp

yarn

yarn add antd-input-otp

How to use 🪴

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.

Uncontrolled

import { Button, Form } from 'antd';
import { InputOTP } from 'antd-input-otp'; // Don't forget to import this too!

const InputOTPPage = () => {
  const [form] = Form.useForm();

  const handleFinish = (values) => {
    // Your logic
  };

  return (
    <Form onFinish={handleFinish} form={form}>
      <Form.Item label="OTP" name="otp">
        <InputOTP autoSubmit={form} inputType="numeric" />
      </Form.Item>

      <Form.Item>
        <Button htmlType="submit">Submit</Button>
      </Form.Item>
    </Form>
  );
};

Controlled

import { useState } from 'react';
import { Button } from 'antd';
import { InputOTP } from 'antd-input-otp'; // Don't forget to import this too!

const InputOTPPage = () => {
  const [value, setValue] = useState([]); // Since the value will be array of string, the default value of state is empty array.

  const handleFinish = (otp) => {
    const payload = otp || value; // Since useState work asynchronously, we shall add the field value from the autoSubmit.
    // Your logic with state
  };

  return (
    <div>
      <InputOTP onChange={setValue} value={value} autoSubmit={handleFinish} />

      <Button onClick={() => handleFinish()}>Submit</Button>
    </div>
  );
};

Props 🥠

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.

PropertyTypeDefault ValueDescription
autoFocusbooleanfalseAutofocus for the first field of OTP. If you want to make the second or third or even the last field autofocused, use `inputRef.
autoSubmitFormInstance | (value: string[]) => void | nullnullAutosubmit when the value is fully filled.
disabledbooleanfalse
inputClassNamestringClasses for styling input field.
inputRefMutable Reference Object (InputRef[], null[], null)nullReference for the input fields. Inside of the current should be array of InputRef from antd.
inputRegexRegExp or string when inputType set as custom. Other than that is neverIf you choose custom as inputType, inputRegex will be mandatory.Wrote your validation with regex here.
inputStyleCSSPropertiesInline style input field.
inputTypeall | alphabet | alphabet-numeric | alphabet-symbol | numeric | numeric-symbol | symbol | customallcustom 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.
lengthnumber6Determine 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.
isPreservedFocusbooleanfalseDetermine whether the input is still focused or not when every field is filled.
onChange(value: string[]) => void
placeholderstringPlaceholder 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_".
valuestring[] | null
wrapperClassNamestringClasses for styling input wrapper.
wrapperStyleCSSPropertiesInline style input wrapper.

FAQ ❓

Q: (NEW) When I use 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.

Q: (NEW) I am using Webpack / 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

Q: Can I paste the OTP to this component?

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.

Q: What will this component return?

A: This component will return an array of strings if erased or already filled, or undefined if this component remains untouched.

Q: Can I use it on Ant Design Form?

A: Ofcourse, the best part is you don't need to setup anything. Just write and go!

Q: Ant Design Form is not my cup of tea, can I use it with useState instead?

A: You bet! Use it like other input with onChange and value.

Q: So I'm using Ant Design Form and I want to add an error below it, but it's ugly. How to fix it?

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..

.your-classname:where(.ant-form-item) .ant-form-item-explain-error {
  text-align: center;
}

Having another question? Ask me on the github issue!

2.0.6

3 months ago

2.1.0

2 months ago

2.0.5

5 months ago

2.0.3

7 months ago

1.1.1

8 months ago

2.0.4

7 months ago

2.0.1

7 months ago

2.0.0

7 months ago

1.1.0

11 months ago

1.0.2

12 months ago

1.0.1

1 year ago

1.0.0

1 year ago