129 lines
4.6 KiB
TypeScript
129 lines
4.6 KiB
TypeScript
'use client'
|
|
|
|
import { forwardRef, useId, useImperativeHandle, useMemo, useRef, 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'
|
|
import { KRI_PW_REGEX, ONLY_ALLOWED_PW_REGEX } from '@/const/validationConst'
|
|
|
|
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 >= 1 && value.length <= 20, text: '1~20자' },
|
|
{ test: (value: string) => !value || KRI_PW_REGEX.test(value), text: '영문, 숫자, 허용된 특수문자를 사용하여 1~20자로 입력해야 합니다.' },
|
|
]
|
|
|
|
export type KriPasswordRef = {
|
|
focus: () => void
|
|
}
|
|
|
|
export interface KriPasswordProps {
|
|
value: string
|
|
onChange: (value: string) => void
|
|
id?: string
|
|
label?: string
|
|
placeholder?: string
|
|
disabled?: boolean
|
|
className?: string
|
|
showStrength?: boolean
|
|
'aria-invalid'?: boolean
|
|
}
|
|
|
|
export const KriPassword = forwardRef<KriPasswordRef, KriPasswordProps>(function KriPassword({
|
|
value,
|
|
onChange,
|
|
id,
|
|
label = '비밀번호',
|
|
placeholder = '비밀번호를 입력하세요',
|
|
disabled,
|
|
className,
|
|
showStrength = true,
|
|
'aria-invalid': ariaInvalid,
|
|
}, forwardedRef) {
|
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
useImperativeHandle(forwardedRef, () => ({
|
|
focus: () => inputRef.current?.focus(),
|
|
}), [])
|
|
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_PW_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 ? '사용 가능한 비밀번호입니다' : '영문, 숫자, 허용된 특수문자를 사용하여 1~20자로 입력해야 합니다.'
|
|
}, [hasOnlyAllowedChars, isValid, value])
|
|
|
|
//const barClass = score <= 1 ? 'bg-destructive' : score === 2 ? 'bg-orange-500' : score === 3 ? 'bg-yellow-400' : 'bg-green-500'
|
|
const barClass = 'bg-green-500'
|
|
|
|
return (
|
|
<div className={cn('w-full space-y-2', className)}>
|
|
{label ? <Label htmlFor={inputId}>{label}</Label> : null}
|
|
<div className="relative">
|
|
<Input
|
|
ref={inputRef}
|
|
id={inputId}
|
|
type={isVisible ? 'text' : 'password'}
|
|
placeholder={placeholder}
|
|
value={value}
|
|
onChange={(event) => onChange(event.target.value)}
|
|
className="pr-9"
|
|
disabled={disabled}
|
|
autoComplete="new-password"
|
|
aria-invalid={ariaInvalid}
|
|
/>
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="icon"
|
|
onClick={() => setIsVisible((current) => !current)}
|
|
disabled={disabled}
|
|
className="text-muted-foreground absolute inset-y-0 right-0 rounded-l-none hover:bg-transparent"
|
|
>
|
|
{isVisible ? <EyeOffIcon /> : <EyeIcon />}
|
|
<span className="sr-only">{isVisible ? '숨기기' : '보기'}</span>
|
|
</Button>
|
|
</div>
|
|
|
|
{showStrength ? (
|
|
<>
|
|
<div className="flex h-1 w-full gap-1">
|
|
{requirements.map((item, index) => (
|
|
<span
|
|
key={item.text}
|
|
className={cn(
|
|
'h-full flex-1 rounded-full transition-colors',
|
|
index < score && value ? barClass : 'bg-border',
|
|
)}
|
|
/>
|
|
))}
|
|
</div>
|
|
<div className="flex items-start gap-2">
|
|
{value ? (
|
|
isValid ? (
|
|
<CheckIcon className="mt-0.5 size-4 text-green-600" />
|
|
) : (
|
|
<XIcon className="text-destructive mt-0.5 size-4" />
|
|
)
|
|
) : (
|
|
<span className="mt-0.5 size-4" />
|
|
)}
|
|
<p className={cn('text-xs', !value ? 'text-muted-foreground' : isValid ? 'text-green-600' : 'text-destructive')}>
|
|
{statusText}
|
|
</p>
|
|
</div>
|
|
</>
|
|
) : null}
|
|
</div>
|
|
)
|
|
})
|
|
|
|
export default KriPassword
|