/* eslint-disable @typescript-eslint/no-explicit-any */ import { useEffect } from 'react'; import { createComponent } from '~/utils'; import { usePasswordStrength } from '../../hooks/usePasswordStrength'; import type { PasswordDictionary } from '../../utils/constants'; import { Flex } from '../Box/Flex'; import { Stack } from '../Box/Stack'; import { Heading } from '../Heading'; import { Icon } from '../Icon'; import type { PopoverProps } from '../Popover'; import { Popover } from '../Popover'; import { Text } from '../Text'; import { StrengthIndicator } from './StrengthIndicator'; import { styles } from './styles'; export type PasswordStrengthProps = { password: string; minLength?: number; unsafeList: string[]; onChangeStrength?: (strength: keyof typeof PasswordDictionary) => void; } & Omit; type ObjProps = { Indicator: typeof StrengthIndicator; }; export const PasswordStrength = createComponent< PasswordStrengthProps, ObjProps >( ({ password, children, minLength = 6, unsafeList, onChangeStrength, ...props }) => { const { strength, label, checker: { casingChecker, lengthChecker, symbolsAndDigitsChecker, commonChecker, }, } = usePasswordStrength({ password, minLength, unsafeList, }); useEffect(() => { onChangeStrength?.(strength); }, [strength, onChangeStrength]); const popoverContent = ( {label} A secure password should have: ) : ( ) } /> } fontSize="xs" > Min. {minLength} characteres ) : ( ) } /> } fontSize="xs" > Upper & lower case letters ) : ( ) } /> } fontSize="xs" > Numbers & Symbols ) : ( ) } /> } fontSize="xs" > Not common or insecure ); return ( e.preventDefault(), onCloseAutoFocus: (e: any) => e.preventDefault(), }} {...props} > {children} ); }, ); PasswordStrength.Indicator = StrengthIndicator; PasswordStrength.defaultProps = { minLength: 6, };