Skip to main content

PasswordField

Description

A password input field component with optional show/hide toggle functionality. Includes automatic minimum length validation and integrates with React Hook Form.

Import

import { PasswordField } from '@components/common/form/PasswordField';

Usage

import { Form } from '@components/common/form/Form';
import { PasswordField } from '@components/common/form/PasswordField';

function LoginForm() {
return (
<Form action="/api/login">
<PasswordField
name="password"
label="Password"
required
showToggle
/>
</Form>
);
}

Props

NameTypeDefaultDescription
namestring-Field name (required)
labelstring-Label text displayed above the input
showTogglebooleanfalseShow/hide password visibility toggle button
minLengthnumber6Minimum password length
requiredbooleanfalseMakes the field required
errorstring-Custom error message
helperTextstring-Helper text shown in a tooltip
validationRegisterOptions-React Hook Form validation rules
prefixIconReact.ReactNode-Icon displayed on the left inside the input
suffixIconReact.ReactNode-Icon displayed on the right inside the input
onChange(value: string) => void-Callback fired when the value changes
wrapperClassNamestring-CSS class for the wrapper div
...propsInputHTMLAttributes-All standard HTML input attributes

Example: With Toggle

import { Form } from '@components/common/form/Form';
import { PasswordField } from '@components/common/form/PasswordField';

function RegisterForm() {
return (
<Form action="/api/register">
<PasswordField
name="password"
label="Password"
placeholder="Enter your password"
required
showToggle
minLength={8}
/>

<PasswordField
name="confirmPassword"
label="Confirm Password"
placeholder="Confirm your password"
required
showToggle
validation={{
validate: (value, formValues) =>
value === formValues.password || 'Passwords do not match'
}}
/>
</Form>
);
}

Example: With Custom Validation

import { Form } from '@components/common/form/Form';
import { PasswordField } from '@components/common/form/PasswordField';

function SecurityForm() {
return (
<Form action="/api/security">
<PasswordField
name="newPassword"
label="New Password"
required
showToggle
minLength={10}
validation={{
pattern: {
value: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])/,
message: 'Password must contain uppercase, lowercase, number, and special character'
}
}}
helperText="Must be at least 10 characters with mixed case, numbers, and symbols"
/>
</Form>
);
}

Features

  • Visibility Toggle: Optional button to show/hide password
  • Automatic Validation: Built-in minimum length validation
  • Icon Support: Add prefix or suffix icons
  • Secure by Default: Uses password input type
  • Auto-cleanup: Automatically unregisters on unmount