|
1 |
| - |
| 1 | + |
2 | 2 |
|
3 | 3 | # React Auth Code Input
|
4 | 4 |
|
5 |
| -> One-time password (OTP) React input component, uncontrolled, zero dependencies, fully tested. |
| 5 | +> One-time password (OTP) React component, zero dependencies, fully tested. |
6 | 6 |
|
7 | 7 | [](https://www.npmjs.com/package/react-auth-code-input) [](https://standardjs.com)
|
8 | 8 | [](LICENSE.md)
|
|
26 | 26 | yarn add react-auth-code-input
|
27 | 27 | ```
|
28 | 28 |
|
29 |
| -## Usage |
| 29 | +## Basic Usage |
30 | 30 |
|
31 |
| -```jsx |
| 31 | +```tsx |
32 | 32 | import React, { useState } from 'react';
|
33 | 33 | import AuthCode from 'react-auth-code-input';
|
34 | 34 |
|
35 | 35 | const App = () => {
|
36 |
| - const [result, setResult] = useState(''); |
| 36 | + const [result, setResult] = useState(); |
| 37 | + const handleOnChange = (res: string) => { |
| 38 | + setResult(res); |
| 39 | + }; |
| 40 | + |
| 41 | + return <AuthCode onChange={handleOnChange} />; |
| 42 | +}; |
| 43 | +``` |
| 44 | + |
| 45 | +## Mode |
| 46 | + |
| 47 | +By default you can type anything in the inputs as the `allowedCharacters` prop is defaulted to `alphanumeric` but you can also choose between allowing only letters or only numbers by setting the prop to `alpha` or `numeric` respectively. |
| 48 | + |
| 49 | +```tsx |
| 50 | +import React, { useState } from 'react'; |
| 51 | +import AuthCode from 'react-auth-code-input'; |
| 52 | + |
| 53 | +const App = () => { |
| 54 | + const [result, setResult] = useState(); |
| 55 | + const handleOnChange = (res: string) => { |
| 56 | + setResult(res); |
| 57 | + }; |
| 58 | + |
| 59 | + return <AuthCode allowedCharacters='numeric' onChange={handleOnChange} />; |
| 60 | +}; |
| 61 | +``` |
| 62 | + |
| 63 | +## Focus |
| 64 | + |
| 65 | +By default the first input is focused when the component is mounted, you can opt-out from this by setting the `autoFocus` prop to `false`, and then you can handle the focus manually by passing a reference. |
| 66 | + |
| 67 | +```tsx |
| 68 | +import React, { useRef, useState } from 'react'; |
| 69 | +import AuthCode, { AuthCodeRef } from 'react-auth-code-input'; |
| 70 | + |
| 71 | +const App = () => { |
| 72 | + const AuthInputRef = useRef<AuthCodeRef>(null); |
| 73 | + const [result, setResult] = useState(); |
| 74 | + const handleOnChange = (res: string) => { |
| 75 | + setResult(res); |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <> |
| 80 | + <AuthCode |
| 81 | + autoFocus={false} |
| 82 | + onChange={handleOnChange} |
| 83 | + ref={AuthInputRef} |
| 84 | + /> |
| 85 | + <button onClick={() => AuthInputRef.current?.focus()}>Focus</button> |
| 86 | + </> |
| 87 | + ); |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +## Clear |
| 92 | + |
| 93 | +You can clear all the inputs by passing a reference and then calling the `clear` method. |
| 94 | + |
| 95 | +```tsx |
| 96 | +import React, { useRef, useState } from 'react'; |
| 97 | +import AuthCode, { AuthCodeRef } from 'react-auth-code-input'; |
| 98 | + |
| 99 | +const App = () => { |
| 100 | + const AuthInputRef = useRef<AuthCodeRef>(null); |
| 101 | + const [result, setResult] = useState(); |
37 | 102 | const handleOnChange = (res: string) => {
|
38 | 103 | setResult(res);
|
39 | 104 | };
|
40 | 105 |
|
41 | 106 | return (
|
42 |
| - <AuthCode |
43 |
| - length={5} |
44 |
| - onChange={handleOnChange} |
45 |
| - containerClassName='container' |
46 |
| - inputClassName='input' |
47 |
| - /> |
| 107 | + <> |
| 108 | + <AuthCode onChange={handleOnChange} ref={AuthInputRef} /> |
| 109 | + <button onClick={() => AuthInputRef.current?.clear()}>Clear</button> |
| 110 | + </> |
48 | 111 | );
|
49 | 112 | };
|
50 | 113 | ```
|
51 | 114 |
|
| 115 | +## SMS Autofill |
| 116 | + |
| 117 | +This component supports autofill from SMS's received, tested on Safari and Chrome in iOS. |
| 118 | + |
52 | 119 | ## Props
|
53 | 120 |
|
54 |
| -| Prop | Type | Description | Default Value | Observations | |
55 |
| -| :------------------- | :---------------------- | :---------------------------------------------------------- | :------------- | :--------------------------------- | |
56 |
| -| `allowedCharacters` | String | Type of allowed characters for your code. | `alphanumeric` | `alpha`, `alphanumeric`, `numeric` | |
57 |
| -| `ariaLabel` | String | Accessibility. | | | |
58 |
| -| `length` | Number | The number of inputs to display. | 6 | | |
59 |
| -| `containerClassName` | String | The styles to be applied to the container. | | | |
60 |
| -| `inputClassName` | String | The styles to be applied to each input. | | | |
61 |
| -| `onChange` | Function(value: String) | Callback function called every time an input value changes. | | | |
62 |
| -| `isPassword` | Boolean | Whether to display the inputs as passwords or not. | false | | |
| 121 | +| Prop | Type | Description | Default Value | Observations | |
| 122 | +| :------------------- | :---------------------- | :---------------------------------------------------------- | :------------- | :----------------------------------------------- | |
| 123 | +| `allowedCharacters` | String | Type of allowed characters for your code. | `alphanumeric` | Valid values: `alpha`, `alphanumeric`, `numeric` | |
| 124 | +| `ariaLabel` | String | Accessibility. | | | |
| 125 | +| `autoFocus` | Boolean | Wether the first input is focused on mount or not.. | true | | |
| 126 | +| `length` | Number | The number of inputs to display. | 6 | | |
| 127 | +| `containerClassName` | String | The styles to be applied to the container. | | | |
| 128 | +| `inputClassName` | String | The styles to be applied to each input. | | | |
| 129 | +| `onChange` | Function(value: String) | Callback function called every time an input value changes. | | Required | |
| 130 | +| `isPassword` | Boolean | Whether to display the inputs as passwords or not. | false | | |
63 | 131 |
|
64 | 132 | ## Changelog
|
65 | 133 |
|
| 134 | +### 3.1.0 |
| 135 | + |
| 136 | +- Add `autoFocus` prop set to true by default to not break current usages. |
| 137 | +- Expose a `focus` method to handle the focus of the first input manually. |
| 138 | +- Expose a `clear` method to clear the input programmatically. |
| 139 | +- Add validations for when not using typescript. |
| 140 | +- Update React peerDependency to use any version 16+. |
| 141 | + |
66 | 142 | ### 3.0.0
|
67 | 143 |
|
68 | 144 | - Change the way the allowed characters are handled by using 3 predefined modes: alpha, alphanumeric, and numeric, allowing to have more control when validating the values introduced in the inputs.
|
@@ -98,22 +174,7 @@ const App = () => {
|
98 | 174 |
|
99 | 175 | ### 1.0.0
|
100 | 176 |
|
101 |
| -- Initial Version. |
102 |
| - |
103 |
| -## Props versions 1 and 2 |
104 |
| - |
105 |
| -| Prop | Type | Description | Default Value | Observations | |
106 |
| -| :------------------- | :---------------------- | :------------------------------------------------------------------------------ | :------------- | :----------------------------- | |
107 |
| -| `allowedCharacters` | String | Regex for allowed characters | `[A-Za-z0-9]+` | | |
108 |
| -| `ariaLabel` | String | Accessibility | | | |
109 |
| -| `characters` | Number | The number of inputs to display | 6 | | |
110 |
| -| `containerClassName` | String | The styles to be applied to the container | | | |
111 |
| -| `inputClassName` | String | The styles to be applied to each input | | | |
112 |
| -| `inputType` | String | The type of the inputs | text | text, number or password | |
113 |
| -| `onChange` | Function(value: String) | Callback function called every time an input value changes | | | |
114 |
| -| ~~`password`~~ | Boolean | If present changes the type of the input to password, by default is set to text | false | deprecated since version 2.0.0 | |
115 |
| -| ~~`inputStyle`~~ | Object | The styles to be applied to each input | | deprecated since version 1.2.0 | |
116 |
| -| ~~`containerStyle`~~ | Object | The styles to be applied to the container | | deprecated since version 1.2.0 | |
| 177 | +- Initial Version. | | deprecated since version 1.2.0 | |
117 | 178 |
|
118 | 179 | ## License
|
119 | 180 |
|
|
0 commit comments