|
| 1 | +import Baseinput from './Baseinput'; |
| 2 | +import { FaEye, FaEyeSlash } from 'react-icons/fa6'; |
| 3 | +import { useState } from 'react'; |
| 4 | + |
| 5 | +function Passwordinput({ |
| 6 | + formValue = {}, |
| 7 | + setFormValue, |
| 8 | + fieldName, |
| 9 | + label, |
| 10 | + required = false, |
| 11 | + setisValidPassword, |
| 12 | + isValidPassword, |
| 13 | + pageType, |
| 14 | +}) { |
| 15 | + const [showPassword, setShowPassword] = useState(false); |
| 16 | + |
| 17 | + const upperCaseRegex = /[A-Z]/; |
| 18 | + const lowerCaseRegex = /[a-z]/; |
| 19 | + const numberRegex = /[0-9]/; |
| 20 | + const specialCharactersRegex = /[!@#$%^&*()]/; |
| 21 | + const commonWords = ['password', '1234', 'qwerty', 'letmein', 'abc123']; |
| 22 | + const [error, setError] = useState(''); |
| 23 | + let labelDynamicStyles = ''; |
| 24 | + let inputClassName; |
| 25 | + let errorMessage = ''; |
| 26 | + let valid = true; |
| 27 | + const togglePassword = () => setShowPassword((prev) => !prev); |
| 28 | + const handleChange = (e) => { |
| 29 | + const newPassword = e.target.value; |
| 30 | + |
| 31 | + if (!newPassword && required) { |
| 32 | + errorMessage += 'Password is required <br>'; |
| 33 | + valid = false; |
| 34 | + } else { |
| 35 | + setError(''); |
| 36 | + } |
| 37 | + |
| 38 | + if (newPassword && pageType === 'signup') { |
| 39 | + if (newPassword.length < 8) { |
| 40 | + errorMessage += 'Have at least 8 characters <br>'; |
| 41 | + valid = false; |
| 42 | + } |
| 43 | + if (!upperCaseRegex.test(newPassword)) { |
| 44 | + errorMessage += 'One uppercase letter <br>'; |
| 45 | + valid = false; |
| 46 | + } |
| 47 | + if (!lowerCaseRegex.test(newPassword)) { |
| 48 | + errorMessage += 'One lowercase letter<br>'; |
| 49 | + valid = false; |
| 50 | + } |
| 51 | + if ( |
| 52 | + !numberRegex.test(newPassword) && |
| 53 | + !specialCharactersRegex.test(newPassword) |
| 54 | + ) { |
| 55 | + errorMessage += 'One number or symbol ! @ # $ % ^ & *( )<br>'; |
| 56 | + valid = false; |
| 57 | + } |
| 58 | + const isCommonPhrase = commonWords.some((w) => |
| 59 | + newPassword.toLowerCase().includes(w) |
| 60 | + ); |
| 61 | + |
| 62 | + if (isCommonPhrase === true) { |
| 63 | + errorMessage += 'Avoid common phrases <br>'; |
| 64 | + valid = false; |
| 65 | + } |
| 66 | + } |
| 67 | + setError(errorMessage); |
| 68 | + setisValidPassword(valid); |
| 69 | + setFormValue(fieldName, newPassword); |
| 70 | + }; |
| 71 | + if (error) { |
| 72 | + inputClassName = |
| 73 | + 'border-sangria ring-2 ring-sangria focus:ring-sangria focus:outline-none'; |
| 74 | + } else if (isValidPassword) { |
| 75 | + inputClassName = |
| 76 | + 'border-olivegreen ring-2 ring-olivegreen focus:ring-olivegreen focus:outline-none'; |
| 77 | + } else { |
| 78 | + inputClassName = |
| 79 | + 'border-zinc-300 ring-2 ring-zinc-300 focus:ring-persianblue focus:outline-none'; |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <> |
| 84 | + <Baseinput |
| 85 | + id={fieldName} |
| 86 | + value={formValue[fieldName] || ''} |
| 87 | + onChange={handleChange} |
| 88 | + type={showPassword ? 'text' : 'password'} |
| 89 | + name={fieldName} |
| 90 | + required={required} |
| 91 | + label={label} |
| 92 | + inputClassName={inputClassName} |
| 93 | + labelClassName={labelDynamicStyles} |
| 94 | + ariaInvalid={isValidPassword === false ? true : undefined} |
| 95 | + ariaDescribedBy={error ? `${fieldName}-error` : undefined} |
| 96 | + /> |
| 97 | + <button |
| 98 | + type="button" |
| 99 | + onClick={togglePassword} |
| 100 | + aria-label={showPassword ? 'Hide password' : 'Show password'} |
| 101 | + aria-pressed={showPassword} |
| 102 | + className="toggle-btn absolute right-3 top-[46px] text-2xl text-black |
| 103 | + focus:outline-none focus:ring-2 focus:ring-persianblue focus:ring-offset-1 cursor-pointer" |
| 104 | + > |
| 105 | + {showPassword ? <FaEyeSlash /> : <FaEye />} |
| 106 | + </button> |
| 107 | + {error && ( |
| 108 | + <div |
| 109 | + id={`${fieldName}-error`} |
| 110 | + role="alert" |
| 111 | + className="text-sm mt-[8px]" |
| 112 | + > |
| 113 | + <p>Password must:</p> |
| 114 | + {error.split('<br>').map((message, index) => ( |
| 115 | + <p key={index}>{message}</p> |
| 116 | + ))} |
| 117 | + </div> |
| 118 | + )} |
| 119 | + </> |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +export default Passwordinput; |
0 commit comments