Model answer
import React, { memo } from 'react';
import { useForm } from 'react-hook-form';
// Keep field definitions outside the component so their references stay stable.
const fields = [
{ name: 'fullName', label: 'Full name', required: 'Enter your full name' },
{ name: 'email', label: 'Email', required: 'Enter your email', type: 'email' },
{ name: 'phone', label: 'Phone', required: 'Enter your phone number', inputMode: 'tel' },
{
name: 'pan',
label: 'PAN',
required: 'Enter your PAN',
validate: (value) => /^[A-Z]{5}[0-9]{4}[A-Z]$/.test(value) || 'Enter a valid PAN',
autoCapitalize: 'characters',
},
{
name: 'aadhaar',
label: 'Aadhaar number',
required: 'Enter your Aadhaar number',
validate: (value) => /^\d{12}$/.test(value) || 'Enter 12 digits',
inputMode: 'numeric',
maxLength: 12,
},
{ name: 'address', label: 'Address', required: 'Enter your address' },
{ name: 'city', label: 'City', required: 'Enter your city' },
{ name: 'state', label: 'State', required: 'Enter your state' },
{ name: 'postalCode', label: 'Postal code', required: 'Enter your postal code' },
{ name: 'occupation', label: 'Occupation', required: 'Enter your occupation' },
{ name: 'annualIncome', label: 'Annual income', required: 'Enter your annual income' },
{ name: 'consent', label: 'I agree to the terms', type: 'checkbox', required: 'Consent is required' },
];
const FormField = memo(function FormField({ field, register, error }) {
const { name, label, type = 'text', ...inputProps } = field;
return (
<div>
<label htmlFor={name}>{label}</label>
<input
id={name}
type={type}
autoComplete="off"
aria-invalid={Boolean(error)}
aria-describedby={error ? `${name}-error` : undefined}
{...inputProps}
{...register(name, {
required: field.required,
validate: field.validate,
})}
/>
{error && <small id={`${name}-error`} role="alert">{error}</small>}
</div>
);
});
export default function KycForm() {
const {
register,
handleSubmit,
formState: { errors, isSubmitting, isSubmitSuccessful },
} = useForm({ mode: 'onBlur' });
async function submitForm(values) {
// Send values to an authenticated HTTPS endpoint; never log or persist identity data.
const response = await fetch('/api/kyc', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(values),
});
if (!response.ok) throw new Error('Unable to submit the form');
}
return (
<form onSubmit={handleSubmit(submitForm)} noValidate>
{fields.map((field) => (
<FormField
key={field.name}
field={field}
register={register}
error={errors[field.name]?.message}
/>
))}
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? 'Submitting…' : 'Submit'}
</button>
{isSubmitSuccessful && <p role="status">Form submitted successfully.</p>}
</form>
);
}
- React Hook Form keeps inputs uncontrolled by default, so typing in one field does not rerender the whole form.
mode: 'onBlur' also avoids validating on every keystroke. React.memo lets an unchanged field skip parent-driven rerenders; only fields whose error props change need to update. For cross-field UI, subscribe narrowly with useWatch rather than watching the entire form.- Treat PAN and Aadhaar as sensitive data: use TLS, avoid analytics/logging and browser persistence, and minimize server-side retention.
Complexity: O(n) to render the n fields initially; O(1) field UI updates per input change in the typical case. Space: O(n) for field definitions and form metadata.