'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!@#$%^&*()_+\-={}\[\]:;"'<>,.?/]+$/ export const KRI_PW_REGEX = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d!@#$%^&*()_+\-={}\[\]:;"'<>,.?/]{8,20}$/ const requirements = [ { test: (value: string) => /[A-Za-z]/.test(value), text: '영문 1개 이상' }, { test: (value: string) => /\d/.test(value), text: '숫자 1개 이상' }, { test: (value: string) => value.length >= 8 && value.length <= 20, text: '8~20자' }, { test: (value: string) => !value || ONLY_ALLOWED_REGEX.test(value), text: '허용 문자만 사용' }, ] export interface KriPasswordProps { value: string onChange: (value: string) => void id?: string label?: string placeholder?: string disabled?: boolean className?: string showStrength?: boolean } export function KriPassword({ value, onChange, id, label = '비밀번호', placeholder = '비밀번호를 입력하세요', disabled, className, showStrength = true, }: KriPasswordProps) { const [isVisible, setIsVisible] = useState(false) const autoId = useId() const inputId = id ?? autoId 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((item) => ({ met: item.test(value), text: item.text })), [value]) const score = checks.filter((item) => item.met).length const statusText = useMemo(() => { if (!value) return '비밀번호를 입력하세요' if (!hasOnlyAllowedChars) return '허용되지 않는 문자가 포함되어 있어요' return isValid ? '사용 가능한 비밀번호입니다' : '영문과 숫자를 포함한 8~20자로 입력하세요' }, [hasOnlyAllowedChars, isValid, value]) const barClass = score <= 1 ? 'bg-destructive' : score === 2 ? 'bg-orange-500' : score === 3 ? 'bg-yellow-400' : 'bg-green-500' return (
{statusText}