|
| 1 | +import { useState } from 'react'; |
| 2 | +import { |
| 3 | + validateAdminId, |
| 4 | + validateAdminPassword, |
| 5 | + type ValidationResult, |
| 6 | +} from '../../../utils/adminValidators'; |
| 7 | + |
| 8 | +interface AddAdminFormProps { |
| 9 | + onSubmit: (data: { name: string; userId: string; password: string }) => void; |
| 10 | +} |
| 11 | + |
| 12 | +const AddAdminForm: React.FC<AddAdminFormProps> = ({ onSubmit }) => { |
| 13 | + const [name, setName] = useState(''); |
| 14 | + const [userId, setUserId] = useState(''); |
| 15 | + const [password, setPassword] = useState(''); |
| 16 | + |
| 17 | + // 유효성 검증 결과 상태 |
| 18 | + const [userIdValidation, setUserIdValidation] = useState<ValidationResult>({ isValid: true }); |
| 19 | + const [passwordValidation, setPasswordValidation] = useState<ValidationResult>({ isValid: true }); |
| 20 | + const [koreanInputError, setKoreanInputError] = useState(false); |
| 21 | + |
| 22 | + // 한글 입력 방지 및 에러 메시지 표시 |
| 23 | + const handleKoreanInput = (value: string) => { |
| 24 | + const hasKorean = /[ㄱ-ㅎㅏ-ㅣ가-힣]/.test(value); |
| 25 | + // 한글이 있으면 에러 메시지 표시, 없으면 바로 숨김 |
| 26 | + setKoreanInputError(hasKorean); |
| 27 | + return hasKorean ? value.replace(/[ㄱ-ㅎㅏ-ㅣ가-힣]/g, '') : value; |
| 28 | + }; |
| 29 | + |
| 30 | + // 입력값 변경 핸들러 |
| 31 | + const handleUserIdChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 32 | + const value = handleKoreanInput(e.target.value); |
| 33 | + setUserId(value); |
| 34 | + setUserIdValidation(validateAdminId(value)); |
| 35 | + }; |
| 36 | + |
| 37 | + const handlePasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 38 | + const value = handleKoreanInput(e.target.value); |
| 39 | + setPassword(value); |
| 40 | + setPasswordValidation(validateAdminPassword(value)); |
| 41 | + }; |
| 42 | + |
| 43 | + const isFormValid = |
| 44 | + name.trim() && |
| 45 | + userId.trim() && |
| 46 | + password.trim() && |
| 47 | + userIdValidation.isValid && |
| 48 | + passwordValidation.isValid; |
| 49 | + |
| 50 | + const handleSubmit = (e: React.FormEvent) => { |
| 51 | + e.preventDefault(); |
| 52 | + if (isFormValid) { |
| 53 | + onSubmit({ name, userId, password }); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <form |
| 59 | + onSubmit={handleSubmit} |
| 60 | + className="text-white flex flex-col gap-10 max-w-[490px] w-full mx-auto" |
| 61 | + > |
| 62 | + {/* 이름 */} |
| 63 | + <div> |
| 64 | + <label className="heading-2-semibold block mb-8">이름</label> |
| 65 | + <input |
| 66 | + type="text" |
| 67 | + placeholder="이름을 입력하세요." |
| 68 | + value={name} |
| 69 | + onChange={(e) => setName(e.target.value)} |
| 70 | + className="w-full rounded-8 bg-grey-700 py-20 px-24 placeholder-grey-400 subhead-1-medium text-grey-50 outline-none focus:ring-1 focus:ring-grey-300" |
| 71 | + /> |
| 72 | + </div> |
| 73 | + |
| 74 | + {/* 계정 */} |
| 75 | + <div> |
| 76 | + <label className="heading-2-semibold block mb-8">계정</label> |
| 77 | + <div className="mb-12"> |
| 78 | + <input |
| 79 | + type="text" |
| 80 | + placeholder="아이디를 입력하세요." |
| 81 | + value={userId} |
| 82 | + onChange={handleUserIdChange} |
| 83 | + pattern="[A-Za-z0-9!@#$%^&*()_+\-=[\]{};':\\|,.<>/?]+" |
| 84 | + className={`w-full rounded-8 bg-grey-700 py-20 px-24 placeholder-grey-400 subhead-1-medium text-grey-50 outline-none focus:ring-1 focus:ring-grey-300 ${ |
| 85 | + !userIdValidation.isValid && userId ? 'ring-1 ring-status-error' : '' |
| 86 | + }`} |
| 87 | + /> |
| 88 | + {!userIdValidation.isValid && userId && ( |
| 89 | + <p className="mt-8 text-status-error body-2-medium">{userIdValidation.error}</p> |
| 90 | + )} |
| 91 | + {koreanInputError && ( |
| 92 | + <p className="mt-8 text-status-error body-2-medium"> |
| 93 | + 한글은 입력할 수 없습니다. 영문, 숫자, 특수문자만 사용 가능합니다. |
| 94 | + </p> |
| 95 | + )} |
| 96 | + </div> |
| 97 | + <div> |
| 98 | + <input |
| 99 | + type="password" |
| 100 | + placeholder="비밀번호를 입력하세요." |
| 101 | + value={password} |
| 102 | + onChange={handlePasswordChange} |
| 103 | + pattern="[A-Za-z0-9!@#$%^&*()_+\-=[\]{};':\\|,.<>/?]+" |
| 104 | + className={`w-full rounded-8 bg-grey-700 py-20 px-24 placeholder-grey-400 subhead-1-medium text-grey-50 outline-none focus:ring-1 focus:ring-grey-300 ${ |
| 105 | + !passwordValidation.isValid && password ? 'ring-1 ring-status-error' : '' |
| 106 | + }`} |
| 107 | + /> |
| 108 | + {!passwordValidation.isValid && password && ( |
| 109 | + <p className="mt-8 text-status-error body-2-medium">{passwordValidation.error}</p> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + |
| 114 | + {/* 버튼 */} |
| 115 | + <button |
| 116 | + type="submit" |
| 117 | + disabled={!isFormValid} |
| 118 | + className={`w-full mt-48 py-[18px] rounded-8 heading-3-semibold transition-all duration-300 |
| 119 | + ${ |
| 120 | + isFormValid |
| 121 | + ? 'text-black cursor-pointer bg-green-300 hover:[background-image:var(--gradient-graphic)]' |
| 122 | + : 'bg-grey-200 text-grey-700 cursor-not-allowed opacity-60' |
| 123 | + }`} |
| 124 | + > |
| 125 | + 관리자 아이디 추가하기 |
| 126 | + </button> |
| 127 | + </form> |
| 128 | + ); |
| 129 | +}; |
| 130 | + |
| 131 | +export default AddAdminForm; |
0 commit comments