'use client' import { useId, useMemo, useState } from 'react' import { CheckIcon, EyeIcon, EyeOffIcon, XIcon } from 'lucide-react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { cn } from '@/lib/utils' // ✅ 화면에 보여줄 “허용 특수문자” export const ALLOWED_SPECIALS_TEXT = `! @ # $ % ^ & * ( ) _ + - = { } [ ] : ; " ' < > , . ? /` // ✅ “허용 문자만” 체크(공백/한글/기타 특수문자 차단) export const ONLY_ALLOWED_REGEX = /^[A-Za-z\d!@#$%^&*()_+\-={}\[\]:;"'<>,.?\/]+$/ // ✅ 최종 비밀번호 규칙(특수문자 필수 아님) // - 8~20자 // - 영문 1개 이상 + 숫자 1개 이상 // - 구성 문자는 허용 문자만 export const KRI_PW_REGEX = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d!@#$%^&*()_+\-={}\[\]:;"'<>,.?\/]{8,20}$/ const requirements = [ { regex: /[A-Za-z]/, text: '영문 1개 이상 (A–Z 또는 a–z)' }, { regex: /\d/, text: '숫자 1개 이상' }, { regex: /^.{10,}$/, text: '10자 이상' }, { regex: /[!@#$%^&*()_+\-={}\[\]:;"'<>,.?/]/, text: `특수문자 사용 (허용: ${ALLOWED_SPECIALS_TEXT})`, }, ] type KriPasswordProps = { value: string onChange: (value: string) => void id?: string label?: string placeholder?: string disabled?: boolean className?: string } const KriPassword = ({ value, onChange, id, label = '비밀번호', placeholder = '비밀번호를 입력하세요', disabled, className, }: KriPasswordProps) => { const [isVisible, setIsVisible] = useState(false) const autoId = useId() const inputId = id ?? autoId const toggleVisibility = () => setIsVisible((prev) => !prev) const isValid = useMemo(() => KRI_PW_REGEX.test(value), [value]) const hasOnlyAllowedChars = useMemo( () => (value ? ONLY_ALLOWED_REGEX.test(value) : true), [value] ) const checks = useMemo( () => requirements.map((req) => ({ met: req.regex.test(value), text: req.text, })), [value] ) const statusText = useMemo(() => { if (!value) return '비밀번호를 입력하세요' if (!hasOnlyAllowedChars) return '허용되지 않는 문자가 포함되어 있어요' return isValid ? '사용 가능한 비밀번호입니다' : '비밀번호 규칙을 만족하지 않아요' }, [value, isValid, hasOnlyAllowedChars]) const score = useMemo(() => checks.filter((c) => c.met).length, [checks]) const getBarColor = (s: number) => { if (s === 0) return 'bg-border' if (s === 1) return 'bg-destructive' if (s === 2) return 'bg-orange-500' if (s === 3) return 'bg-yellow-400' return 'bg-green-500' // 4 } const getStrengthText = (s: number) => { if (s === 0) return '비밀번호를 입력하세요' if (s === 1) return '약함' if (s === 2) return '보통' if (s === 3) return '강함' return '매우 강함' } return (
{statusText}
{getStrengthText(score)}.
포함 조건: