-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneInput.jsx
More file actions
54 lines (51 loc) · 1.42 KB
/
PhoneInput.jsx
File metadata and controls
54 lines (51 loc) · 1.42 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
import { useState } from 'react';
export const PhoneInput = ({ label="Phone Number", onChange, value, isRequire = true, type='', defaultValue='', }) => {
const [isValid, setIsValid] = useState(true);
const handleChange = (e) => {
if (!(e.target instanceof HTMLInputElement)) return;
const phone = e.target.value;
setIsValid(phone.length == 10 && /^[0-9]*$/.test(phone));
};
return (
<div>
<label
htmlFor="Phone Number"
className="mb-2.5 block text-sm font-medium text-black"
>
{label}
</label>
{type=='readOnly'
?
<input
disabled
readOnly
defaultValue = {defaultValue}
name="Phone Number"
id="Phone Number"
type="tel"
onInput={handleChange}
pattern="^[0-9]{10}$"
onChange={onChange}
value={value}
className="mb-2.5 block w-full rounded-md border-2 border-black p-2.5 text-sm "
placeholder="0123456789"
required={isRequire}
/>
:
<input
defaultValue = {defaultValue}
name="Phone Number"
id="Phone Number"
type="tel"
onInput={handleChange}
pattern="^[0-9]{10}$"
onChange={onChange}
value={value}
className="mb-2.5 block w-full rounded-md border-2 border-black p-2.5 text-sm "
placeholder="0123456789"
required={isRequire}
/>
}
</div>
);
};