mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-25 14:25:02 +00:00
- Added @metabuilder/hooks workspace package at root
- Consolidated 30 React hooks from across codebase into single module
- Implemented conditional exports for tree-shaking support
- Added comprehensive package.json with build/lint/typecheck scripts
- Created README.md documenting hook categories and usage patterns
- Updated root package.json workspaces array to include hooks
- Supports multi-version peer dependencies (React 18/19, Redux 8/9)
Usage:
import { useDashboardLogic } from '@metabuilder/hooks'
import { useLoginLogic } from '@metabuilder/hooks/useLoginLogic'
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
/**
|
|
* usePasswordValidation Hook
|
|
* Password validation and strength calculation logic
|
|
*/
|
|
|
|
import { useState, useCallback } from 'react';
|
|
|
|
export interface PasswordValidationResult {
|
|
score: number;
|
|
message: string;
|
|
}
|
|
|
|
export interface UsePasswordValidationReturn {
|
|
passwordStrength: number;
|
|
validatePassword: (pwd: string) => PasswordValidationResult;
|
|
handlePasswordChange: (value: string) => void;
|
|
}
|
|
|
|
/**
|
|
* Custom hook for password validation
|
|
* Provides password strength scoring and validation rules
|
|
*/
|
|
export const usePasswordValidation = (): UsePasswordValidationReturn => {
|
|
const [passwordStrength, setPasswordStrength] = useState(0);
|
|
|
|
const validatePassword = useCallback((pwd: string): PasswordValidationResult => {
|
|
let score = 0;
|
|
let message = '';
|
|
|
|
if (pwd.length >= 8) score++;
|
|
if (/[a-z]/.test(pwd)) score++;
|
|
if (/[A-Z]/.test(pwd)) score++;
|
|
if (/\d/.test(pwd)) score++;
|
|
|
|
if (score === 0) message = 'Enter a password';
|
|
else if (score === 1) message = 'Weak';
|
|
else if (score === 2) message = 'Fair';
|
|
else if (score === 3) message = 'Good';
|
|
else message = 'Strong';
|
|
|
|
return { score, message };
|
|
}, []);
|
|
|
|
const handlePasswordChange = useCallback((value: string) => {
|
|
const { score } = validatePassword(value);
|
|
setPasswordStrength(score);
|
|
}, [validatePassword]);
|
|
|
|
return {
|
|
passwordStrength,
|
|
validatePassword,
|
|
handlePasswordChange
|
|
};
|
|
};
|