Skip to content

Commit a4d2762

Browse files
update readme
1 parent 1e1c214 commit a4d2762

File tree

1 file changed

+9
-190
lines changed

1 file changed

+9
-190
lines changed

readme.md

Lines changed: 9 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
<div align="center">
22
<img width="300" src="https://raw.githubusercontent.com/supercoder123/react-otp-input-hook/main/images/lib-logo.svg" alt="React OTP Input Hook" />
33

4+
5+
[![npm](https://img.shields.io/npm/v/react-otp-input-hook)](https://www.npmjs.com/package/react-otp-input-hook)
6+
[![npm](https://img.shields.io/npm/dm/react-otp-input-hook)](https://www.npmjs.com/package/react-otp-input-hook)
7+
[![NPM](https://img.shields.io/npm/l/react-otp-input-hook)](https://github.com/supercoder123/react-otp-input-hook/blob/main/LICENSE.md)
48
[![Coverage Status](https://coveralls.io/repos/github/supercoder123/react-otp-input-hook/badge.svg?branch=main)](https://coveralls.io/github/supercoder123/react-otp-input-hook?branch=main)
59
[![Build Status](https://app.travis-ci.com/supercoder123/react-otp-input-hook.svg?branch=main)](https://app.travis-ci.com/supercoder123/react-otp-input-hook)
10+
611
</div>
712

813

914

1015
A simple react hook to create otp inputs with ease. Inspired by libraries like react-hook-form, downshift-js ...etc
1116

17+
18+
1219
## Features
1320
- No dependencies (only needs react as peer dependency)
1421
- Small size
@@ -23,199 +30,11 @@ A simple react hook to create otp inputs with ease. Inspired by libraries like r
2330
npm i react-otp-input-hook
2431
```
2532

26-
## Basic Usage
27-
28-
```
29-
import { useOtpInput } from "react-otp-input-hook";
30-
31-
const BasicOTPComponent = ({ onChange }: { onChange: (val: string) => void }) => {
32-
const { register } = useOtpInput({
33-
onInputValueChange: onChange,
34-
});
35-
36-
const defaultOptions = { required: true };
37-
38-
return (
39-
<div style={{ padding: '10px' }}>
40-
<input {...register("digit-1", defaultOptions)} />
41-
<input {...register("digit-2", defaultOptions)} />
42-
<input {...register("digit-3", defaultOptions)} />
43-
<input {...register("digit-4", defaultOptions)} />
44-
<input {...register("digit-5", defaultOptions)} />
45-
</div>
46-
);
47-
};
48-
```
49-
50-
OR
51-
52-
```
53-
import { useOtpInput } from "react-otp-input-hook";
54-
55-
const BasicOTPComponent = ({ onChange }: { onChange: (val: string) => void }) => {
56-
const { inputs } = useOtpInput({
57-
numberOfInputs: 5,
58-
onInputValueChange: onChange,
59-
});
60-
61-
return (
62-
<div>
63-
{inputs.map((input, i) => {
64-
return <input required key={i} {...input} />;
65-
})}
66-
</div>
67-
);
68-
};
69-
```
70-
71-
## Use Inside any form
72-
```
73-
function App() {
74-
const [value, setValue] = useState("");
75-
76-
return (
77-
<div>
78-
<form
79-
onSubmit={(e) => {
80-
e.preventDefault();
81-
console.log('Value', value);
82-
}}
83-
>
84-
85-
<BasicOTPComponent
86-
onChange={(value: any) => {
87-
console.log(value);
88-
setValue(value);
89-
}}
90-
/>
91-
92-
<button
93-
type="submit"
94-
>
95-
Submit
96-
</button>
97-
</form>
98-
99-
<div>{value}</div>
100-
</div>
101-
);
102-
}
103-
```
104-
105-
```
106-
// styles
107-
108-
form {
109-
display: flex;
110-
flex-direction: column;
111-
}
112-
form input {
113-
height: 40px;
114-
font-size: 20px;
115-
padding: 10px;
116-
margin-right: 10px;
117-
margin-left: 10px;
118-
width: 40px;
119-
text-align: center;
120-
font-family: sans-serif;
121-
}
122-
button {
123-
border-radius: 8px;
124-
border: 1px solid transparent;
125-
padding: 0.6em 1.2em;
126-
font-size: 1em;
127-
font-weight: 500;
128-
font-family: inherit;
129-
background-color: #1a1a1a;
130-
cursor: pointer;
131-
transition: border-color 0.25s;
132-
}
133-
```
134-
135-
## Output
136-
13733
<center>
13834
<img width="100%" alt="Basic component output" src="https://raw.githubusercontent.com/supercoder123/react-otp-input-hook/main/images/basic-component.gif" />
13935
</center>
14036

141-
## Options
142-
```
143-
const {
144-
register,
145-
setValue,
146-
setDisabled,
147-
clear,
148-
value,
149-
inputs,
150-
error,
151-
setError
152-
} = useOtpInput<T>(options);
153-
154-
// T can be HTMLTextAreaElement or HTMLInputElement;
155-
156-
options = {
157-
type?: "numeric" | "alphanumeric" | "password" | "password-numeric";
158-
159-
// change listener to listen to the latest value
160-
onInputValueChange?: (val: string) => void;
161-
162-
// spacebar will be allowed
163-
blankAllowed?: boolean;
164-
165-
// focuses the first input on load
166-
focusOnLoad?: boolean;
167-
168-
// any css style object
169-
defaultInlineStyles?: CSSProperties;
170-
171-
// class will be added to all the inputs
172-
defaultClassName?: string;
173-
174-
// on or off
175-
autoCompleteAttribute?: string | undefined;
176-
177-
// will cycle back to first input if key is pressed on the last input
178-
cycle?: boolean;
179-
180-
placeholder?: string;
181-
182-
// need to provide this option in order to use the inputs return value
183-
numberOfInputs?: number;
184-
}
185-
186-
187-
interface InputOptions {
188-
required?: boolean;
189-
maxLength?: number;
190-
}
191-
192-
// Returned values
193-
194-
// used to register an input, can be provided maxLength and required as props
195-
register: (name: string, options?: InputOptions) => RegisterReturn<T>;
196-
197-
// used to set the value for all inputs
198-
setValue: (val: string | number) => void;
199-
200-
// disables all the inputs
201-
setDisabled: (disabled: boolean) => void;
202-
203-
// clears all inputs
204-
clear: () => void;
205-
206-
// provides the value of the otp input
207-
value: string | number;
208-
209-
// Returns the inputs based on the value provided in numberOfInputs.
210-
// Can be used to set the inputs in a loop instead of registering it one by one
211-
readonly inputs: RegisterReturn<T>[];
212-
213-
// get an error string
214-
error: string;
215-
216-
// set an error string
217-
setError: React.Dispatch<React.SetStateAction<string>>;
218-
219-
```
22037

38+
#### Docs: https://supercoder123.github.io/react-otp-input-docs/
22139

40+
<br>

0 commit comments

Comments
 (0)