All files / src/components password-input.tsx

100% Statements 31/31
92.5% Branches 37/40
100% Functions 5/5
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173                                          3x 9x   7x 7x 7x 7x 7x 7x   7x                 7x     3x                         54x   54x 54x       54x 2x                                                               52x                           3x                                                                 3x     3x     51x 2x         2x                       49x       3x  
import { useState, forwardRef } from 'react';
import type { Control, FieldPath, FieldValues } from 'react-hook-form';
import { Controller } from 'react-hook-form';
import type { BaseInputProps } from './input';
import { BaseInput } from './input';
import { Eye, EyeOff } from 'lucide-react';
import { cn } from '@/lib/utils';
 
export interface BasePasswordInputProps extends Omit<BaseInputProps, 'type'> {
  showToggle?: boolean;
  showStrengthIndicator?: boolean;
}
 
// Enhanced PasswordInput props that can work with React Hook Form
export interface PasswordInputProps<T extends FieldValues = FieldValues>
  extends BasePasswordInputProps {
  name?: FieldPath<T>;
  control?: Control<T>;
}
 
// Password strength calculation utility
const getPasswordStrength = (password: string) => {
  if (!password) return { strength: 0, label: '', color: '' };
 
  let score = 0;
  if (password.length >= 8) score++;
  if (/[A-Z]/.test(password)) score++;
  if (/[a-z]/.test(password)) score++;
  if (/\d/.test(password)) score++;
  if (/[^A-Za-z0-9]/.test(password)) score++;
 
  const strengthMap = [
    { strength: 0, label: 'Very Weak', color: 'bg-red-500' },
    { strength: 1, label: 'Weak', color: 'bg-red-400' },
    { strength: 2, label: 'Fair', color: 'bg-yellow-500' },
    { strength: 3, label: 'Good', color: 'bg-blue-500' },
    { strength: 4, label: 'Strong', color: 'bg-green-500' },
    { strength: 5, label: 'Very Strong', color: 'bg-green-600' },
  ];
 
  return strengthMap[score];
};
 
const BasePasswordInput = forwardRef<HTMLInputElement, BasePasswordInputProps>(
  (
    {
      className,
      label,
      error,
      showToggle = true,
      showStrengthIndicator = false,
      value,
      ...props
    },
    ref
  ) => {
    const [showPassword, setShowPassword] = useState(false);
 
    const passwordValue = (value as string) || '';
    const passwordStrength = showStrengthIndicator
      ? getPasswordStrength(passwordValue)
      : null;
 
    if (!showToggle) {
      return (
        <div className="space-y-2">
          <BaseInput
            type="password"
            label={label}
            error={error}
            className={className}
            ref={ref}
            value={value}
            {...props}
          />
          {showStrengthIndicator && passwordValue && passwordStrength && (
            <div className="space-y-1">
              <div className="flex items-center space-x-2">
                <div className="flex-1 h-2 bg-gray-200 rounded-full overflow-hidden">
                  <div
                    className={`h-full transition-all duration-300 ${passwordStrength.color}`}
                    style={{
                      width: `${(passwordStrength.strength / 5) * 100}%`,
                    }}
                  />
                </div>
                <span className="text-xs text-muted-foreground">
                  {passwordStrength.label}
                </span>
              </div>
            </div>
          )}
        </div>
      );
    }
 
    return (
      <div className="space-y-2">
        <div className="relative">
          <BaseInput
            type={showPassword ? 'text' : 'password'}
            label={label}
            error={error}
            className={cn('pr-12', className)}
            ref={ref}
            value={value}
            {...props}
          />
          <button
            type="button"
            onClick={() => setShowPassword(!showPassword)}
            className="absolute right-3 top-[45px] text-muted-foreground hover:text-foreground"
            tabIndex={-1}
            aria-label={showPassword ? 'Hide password' : 'Show password'}
          >
            {showPassword ? (
              <EyeOff className="h-4 w-4" />
            ) : (
              <Eye className="h-4 w-4" />
            )}
          </button>
        </div>
        {showStrengthIndicator && passwordValue && passwordStrength && (
          <div className="space-y-1">
            <div className="flex items-center space-x-2">
              <div className="flex-1 h-2 bg-gray-200 rounded-full overflow-hidden">
                <div
                  className={`h-full transition-all duration-300 ${passwordStrength.color}`}
                  style={{ width: `${(passwordStrength.strength / 5) * 100}%` }}
                  data-testid="strength-bar"
                />
              </div>
              <span className="text-xs text-muted-foreground">
                {passwordStrength.label}
              </span>
            </div>
          </div>
        )}
      </div>
    );
  }
);
 
BasePasswordInput.displayName = 'BasePasswordInput';
 
// Smart PasswordInput component that works with or without React Hook Form
export const PasswordInput = forwardRef<HTMLInputElement, PasswordInputProps>(
  ({ name, control, ...props }, ref) => {
    // If control and name are provided, use Controller
    if (control && name) {
      return (
        <Controller
          name={name as any}
          control={control as any}
          render={({ field, fieldState: { error } }) => (
            <BasePasswordInput
              {...field}
              {...props}
              error={error?.message || props.error}
              ref={ref}
            />
          )}
        />
      );
    }
 
    // Otherwise, use as regular password input
    return <BasePasswordInput {...props} ref={ref} />;
  }
);
 
PasswordInput.displayName = 'PasswordInput';