-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.jsx
202 lines (159 loc) · 5.01 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { useState,useCallback,useEffect,useRef } from 'react'
function App() {
const [length, setlength] = useState(8)
const [numberAllowed,setNumberAllowed] =useState(false)
const [charAllowed,setcharAllowed]=useState(false)
const [password,setPassword]=useState("")
const passwordRef=useRef(null)
const passwordGenerator=useCallback(()=>{
let pass=""
let str="ABCDEfghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
if(numberAllowed) str+="0123456789"
if(charAllowed) str+="!@#$%^&*_+{}[]~"
for(let i=1;i<=length;i++){
let char=Math.floor(Math.random() * str.length +1)
pass += str.charAt(char)
}
setPassword(pass)
},[length,numberAllowed,charAllowed,setPassword])
const copyPasswordtoclipboard=useCallback(()=>{
passwordRef.current?.select();
passwordRef.current?.setSelectionRange(0,100)
window.navigator.clipboard.writeText(password)
},[password])
useEffect(()=>{
passwordGenerator()
},[length,numberAllowed,charAllowed,passwordGenerator])
return (
<>
<div className= 'w-full max-w-md mx-auto rounded-lg px-4 my-8 text-black bg-gray-500 '> Password Generator
<div className='text-orange-700 flex rounded-lg overflow-hidden mb-4'>
<input
type="text"
value={password}
className='outline-none w-full py-1 px-3'
placeholder='password'
readOnly
ref={passwordRef}
/>
<button onClick={copyPasswordtoclipboard} className='hover:bg-purple-400 hover:text-black outline-none bg-blue-700 text-white px-3 py-0.5 shrink-0'>copy</button>
</div>
<div className='flex text-sm gap-x-2'>
-
<div className='flex items-center gap-x-1'>
<input
type="range"
min={6}
max={100}
value={length}
className='cursor-pointer'
onChange={(e)=>{setlength(e.target.value)}}
/>
<label>length:{length}</label>
</div>
<div className='flex items-center ga-x-1'>
<input
type="checkbox"
defaultChecked={numberAllowed}
id="numberInput"
onChange={()=>{
setNumberAllowed((prev) => !prev)
}}
/>
<label htmlFor='numberInput'>Numbers</label>
</div>
<div className='flex items-center ga-x-1'>
<input
type="checkbox"
defaultChecked={charAllowed}
id="characterInput"
onChange={()=>{
setcharAllowed((prev) => !prev)
}}
/>
<label htmlFor='characterinput'>characters</label>
</div>
</div>
</div>
</>
)
}
export default App
// import {useCallback, useState, useEffect,useRef} from 'react'
// export default function() {
// const [length,setLength]=useState(8)
// const [password,setPassword]=useState("")
// const [number,setNumber]=useState(false)
// const [char,setChar]=useState(false)
// const passwordGenerator=useCallback(()=>{
// let pass=""
// let str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
// if(number) str+="0123456789"
// if(char) str+="!@#$%^^&*()"
// for(let i=1;i<=length;i++){
// let char=Math.floor(Math.random() * str.length+1)
// pass +=str.charAt(char)
// }
// setPassword(pass)
// }
// ,[length,setPassword,number,char])
// const passRef=useRef(null)
// useEffect(()=>{
// passwordGenerator()
// }
// ,
// [length,setNumber,setChar,passwordGenerator])
// const copyPasswordtoclipboard=useCallback(()=>{
// passRef.current?.select()
// passRef.current?.setSelectionRange(0,100)
// window.navigator.clipboard.writeText(password)
// },[password])
// return (
// <>
// <div className='bg-white w-max h-30 rounded-xl justify-center text-center'>
// Password Generator
// <div className='gap-x-1 mx-20'>
// <input
// type="length"
// value={password}
// placeholder="Password"
// className='w-auto h-max bg-gray-400 rounded text-black border-black'
// readOnly
// ref={passRef}
// />
// <button onClick={copyPasswordtoclipboard}
// className='bg-blue-400 text-white text-center rounded hover:bg-purple-600 hover:text-black' >copy</button>
// </div>
// <div>
// <input
// type="range"
// min={6}
// max={100}
// className='cursor-pointer gap-x-1'
// value={length}
// onChange={(e)=>{setLength(e.target.value)}}
// />
// <label > length:{length}</label>
// <input
// type="checkbox"
// defaultChecked={number}
// id="numberInput"
// onChange={()=>
// {setNumber((prev)=>!(prev))}
// }
// />
// <label htmlFor='numberInput'>Number</label>
// <input
// type="checkbox"
// defaultChecked={char}
// id="charInput"
// onChange={()=>{
// setChar((prev)=>!(prev))
// }}
// />
// <label htmlFor='charInput'>Character</label>
// </div>
// </div>
// </>
// )
// }