-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathApp.tsx
More file actions
137 lines (127 loc) · 4.46 KB
/
App.tsx
File metadata and controls
137 lines (127 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React, { useState } from 'react';
import { TConfig } from './utils'
import OTPInput from '../../src';
function App() {
const [{ otp, numInputs, separator, minLength, maxLength, placeholder, inputType }, setConfig] = useState<TConfig>({
otp: '',
numInputs: 4,
separator: '-',
minLength: 0,
maxLength: 40,
placeholder: '',
inputType: 'text',
});
const handleOTPChange = (otp: string) => {
setConfig((prevConfig) => ({ ...prevConfig, otp }));
};
const handleChange = (event: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLSelectElement>) => {
const { name, value } = event.target;
setConfig((prevConfig) => ({ ...prevConfig, [name]: value }));
};
const handleNumInputsChange = (event: React.ChangeEvent<HTMLInputElement>) => {
let numInputs = Number(event.target.value);
if (numInputs < minLength || numInputs > maxLength) {
numInputs = 4;
console.error(`Please enter a value between ${minLength} and ${maxLength}`);
}
setConfig((prevConfig) => ({ ...prevConfig, numInputs }));
};
const clearOtp = () => {
setConfig((prevConfig) => ({ ...prevConfig, otp: '' }));
};
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert(otp);
};
return (
<div className="container">
<div className="side-bar">
<a href="https://github.com/devfolioco/react-otp-input" target="_blank" rel="noreferrer">
<div className="side-bar__segment side-bar__segment--header">
<h2>react-otp-input</h2>
</div>
</a>
<div className="side-bar__segment">
<label htmlFor="num-inputs">
numInputs
<input
id="num-inputs"
name="numInputs"
type="number"
value={numInputs}
onChange={handleNumInputsChange}
min={minLength}
max={maxLength}
/>
</label>
</div>
<div className="side-bar__segment">
<label htmlFor="separator">
separator
<input
id="separator"
maxLength={1}
name="separator"
type="text"
value={separator}
onChange={handleChange}
/>
</label>
</div>
<div className="side-bar__segment">
<label htmlFor="value">
value
<input id="value" maxLength={numInputs} name="otp" type="text" value={otp} onChange={handleChange} />
</label>
</div>
<div className="side-bar__segment">
<label htmlFor="placeholder">
placeholder
<input id="placeholder" name="placeholder" type="text" value={placeholder} onChange={handleChange} />
</label>
</div>
<div className="side-bar__segment">
<label htmlFor="inputType">inputType</label>
<select id="inputType" name="inputType" value={inputType} onChange={handleChange}>
<option value="text">text</option>
<option value="number">number</option>
<option value="password">password</option>
<option value="tel">tel</option>
</select>
</div>
<div className="side-bar__segment side-bar__segment--bottom">
<a href="https://github.com/devfolioco/react-otp-input">Documentation and Source</a>
</div>
</div>
<div className="view">
<div className="card">
<form onSubmit={handleSubmit}>
<p>Enter verification code</p>
<div className="margin-top--small">
<OTPInput
inputStyle="inputStyle"
numInputs={numInputs}
onChange={handleOTPChange}
renderSeparator={<span>{separator}</span>}
value={otp}
placeholder={placeholder}
inputType={inputType}
renderInput={(props) => <input {...props} />}
shouldAutoFocus
/>
</div>
<div className="btn-row">
<button className="btn margin-top--large" type="button" disabled={otp.trim() === ''} onClick={clearOtp}>
Clear
</button>
<button className="btn margin-top--large" disabled={otp.length < numInputs}>
Get OTP
</button>
</div>
</form>
</div>
</div>
</div>
);
}
export default App;