Skip to main content

EmailField

Description

An email input field component with automatic email format validation. Integrates with React Hook Form and includes support for icons and helper text.

Import

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

Usage

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

function ContactForm() {
return (
<Form action="/api/contact">
<EmailField
name="email"
label="Email Address"
required
/>
</Form>
);
}

Props

NameTypeDefaultDescription
namestring-Field name (required)
labelstring-Label text displayed above the input
requiredbooleanfalseMakes the field required
defaultValuestring-Default email value
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: Basic Usage

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

function SubscribeForm() {
return (
<Form action="/api/subscribe">
<EmailField
name="email"
label="Your Email"
placeholder="you@example.com"
required
helperText="We'll never share your email with anyone"
/>
</Form>
);
}

Example: With Icon

import { Form } from '@components/common/form/Form';
import { EmailField } from '@components/common/form/EmailField';
import { Mail } from 'lucide-react';

function LoginForm() {
return (
<Form action="/api/login">
<EmailField
name="email"
label="Email"
placeholder="Enter your email"
required
prefixIcon={<Mail size={16} />}
/>
</Form>
);
}

Features

  • Automatic Email Validation: Built-in regex pattern for email format
  • Icon Support: Add prefix or suffix icons
  • Auto-cleanup: Automatically unregisters on unmount
  • Type Safety: Uses HTML email input type

Validation

The component includes a default email validation pattern:

/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i

You can override this pattern by providing a custom validation rule.