-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.jsx
116 lines (99 loc) · 2.78 KB
/
App.jsx
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
import { useState,useCallback,useEffect} from "react"
import { useRef } from "react"
function App() {
const [password, setPassword] = useState('')
const [length, setLength] = useState(5)
const [charAllowed,setCharAllowed] = useState(false)
const [numAllowed,setNumAllowed] = useState(false)
const [upperAllowed,setUpperAllowed] = useState(false)
const passwordref = useRef()
const copy = ()=>{
passwordref.current?.select()
navigator.clipboard.writeText(password)
}
const password_generator = useCallback(() =>{
let str1 = "abcdefghijklmnopqrstuvwxyz"
let str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let str3 = "0123456789"
let str = "!@#$%^&*()_+-={}:<>?,./"
let pass = ""
if(numAllowed){
str1 += str3
}
if(charAllowed){
str1 += str
}
if(upperAllowed){
str1 += str2
}
for(let i = 0;i<=length-1;i++){
pass += str1.charAt(Math.floor(Math.random()*str1.length))
}
setPassword(pass)
},[charAllowed,numAllowed,upperAllowed,length,setPassword])
useEffect(()=>{
password_generator()
},[charAllowed,numAllowed,upperAllowed,length])
console.log(password)
return(
<>
<div className="h-screen bg-[#FFA07A]">
<div className="flex flex-col items-center bg-[#F08080] ">
<h1 className="">Password Generator</h1>
<div className="flex ">
<label htmlFor="pass">Password:</label>
<input
type = 'text'
id = 'pass'
value = {password}
onChange={(e)=>e.target.value}
className=""
placeholder="Password..."
readOnly
ref={passwordref}
/>
<button onClick={copy}
className="bg-black text-white py-1 px-2 rounded-lg ml-2 shadow-lg hover:bg-gray-500 "
>
Copy
</button>
</div>
<div>
<label htmlFor="length">Length:</label>
<input
type = 'range'
id = 'length'
min={5}
max={100}
value = {length}
onChange={(e)=>setLength(e.target.value)}
/>
<p>{`${length}`}</p>
<label htmlFor="char">Characters:</label>
<input
type="checkbox"
id="char"
checked={charAllowed}
onChange={()=>setCharAllowed((prev)=>!prev)}
/>
<label htmlFor="num">Numebers:</label>
<input
type="checkbox"
id="num"
checked={numAllowed}
onChange={()=>setNumAllowed((prev)=>!prev)}
/>
<label htmlFor="upper">UpperCase:</label>
<input
type = 'checkbox'
id = 'upper'
checked={upperAllowed}
onChange = {()=> setUpperAllowed((prev) => !prev)}
/>
</div>
</div>
</div>
</>
)
}
export default App