DateTimeLocalField
Description
A date and time picker input field component that integrates with React Hook Form. Provides a native datetime-local picker interface with min/max validation.
Import
import { DateTimeLocalField } from '@components/common/form/DateTimeLocalField';
Usage
import { Form } from '@components/common/form/Form';
import { DateTimeLocalField } from '@components/common/form/DateTimeLocalField';
function AppointmentForm() {
return (
<Form action="/api/appointments">
<DateTimeLocalField
name="appointmentTime"
label="Appointment Date & Time"
required
/>
</Form>
);
}
Props
| Name | Type | Default | Description |
|---|---|---|---|
| name | FieldPath<T> | - | Field name (required) |
| label | string | - | Label text displayed above the input |
| min | string | - | Minimum allowed datetime |
| max | string | - | Maximum allowed datetime |
| step | number | - | Step interval in seconds |
| required | boolean | false | Makes the field required |
| error | string | - | Custom error message |
| helperText | string | - | Helper text shown in a tooltip |
| validation | RegisterOptions<T> | - | React Hook Form validation rules |
| wrapperClassName | string | - | CSS class for the wrapper div |
| ...props | InputHTMLAttributes | - | All standard HTML input attributes |
Example: Meeting Scheduler
import { Form } from '@components/common/form/Form';
import { DateTimeLocalField } from '@components/common/form/DateTimeLocalField';
function MeetingForm() {
const now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
const minDateTime = now.toISOString().slice(0, 16);
return (
<Form action="/api/meetings">
<DateTimeLocalField
name="startTime"
label="Meeting Start Time"
min={minDateTime}
step={900} // 15 minute intervals
required
helperText="Select date and time for the meeting"
/>
</Form>
);
}
Example: Event Scheduling
import { Form } from '@components/common/form/Form';
import { DateTimeLocalField } from '@components/common/form/DateTimeLocalField';
function EventForm() {
return (
<Form action="/api/events">
<DateTimeLocalField
name="eventStart"
label="Event Start"
required
/>
<DateTimeLocalField
name="eventEnd"
label="Event End"
required
validation={{
validate: (value, formValues) => {
if (!value || !formValues.eventStart) return true;
return value > formValues.eventStart || 'End time must be after start time';
}
}}
/>
</Form>
);
}
Features
- Native Picker: Uses browser's native datetime-local picker
- Min/Max Validation: Built-in validation for datetime ranges
- Step Control: Control time interval precision
- Auto-cleanup: Automatically unregisters on unmount
- TypeScript Support: Full type safety with generics
Datetime Format
Values are in YYYY-MM-DDTHH:mm format (ISO 8601 local datetime string).