MuiXDatePickerElement
Use the MUI X DatePicker with react-hook-form. MuiXDatePickerElement binds the picked date and its validation to a form field, no Controller needed.
MuiXDatePickerElement is the MUI X DatePicker connected to React Hook Form. It stores the
picked date, turns the picker's own limits, such as minDate and disablePast, into form
validation with readable messages, and shows errors as the picker's helper text. New to the
adapters? Start with Using MUI with React Hook Form.
Import
import { MuiXDatePickerElement } from '@piplup/rhf-adapters/mui-x-date-pickers';Usage
'use client';import Stack from '@mui/material/Stack';import { LocalizationProvider } from '@mui/x-date-pickers';import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';import { MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { MuiXDatePickerElement } from '@piplup/rhf-adapters/mui-x-date-pickers';import dayjs from 'dayjs';import { useForm } from 'react-hook-form';export default function Page() { const { control, handleSubmit } = useForm({ defaultValues: { appointment: dayjs() }, }); return ( <LocalizationProvider dateAdapter={AdapterDayjs}> <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}> <Stack direction="column" spacing={2}> <MuiXDatePickerElement control={control} label="Appointment" name="appointment" /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> </LocalizationProvider> );}Like every MUI X picker, the element needs a LocalizationProvider above it with a date library
adapter. The examples on this page use Dayjs.
What gets stored
The field holds a date object from your adapter: a Dayjs object with AdapterDayjs, a Date with
AdapterDateFns, and so on. An empty picker stores null, so use null in defaultValues.
Most APIs want a string, so format the date when you submit rather than storing strings in the form. Keeping a date object lets the picker validate and compare dates.
Date limits are validation too
minDate, maxDate, disablePast, disableFuture and the shouldDisable* functions do two
things on this element: they limit what the user can pick, and they become React Hook Form
validation. A date typed by hand outside the range fails on submit with its own message. This
booking form requires a check-in date that isn't in the past, and a check-out after it:
import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { MuiXDatePickerElement } from '@piplup/rhf-adapters/mui-x-date-pickers';
import type { Dayjs } from 'dayjs';
import { useForm } from 'react-hook-form';
type Booking = { checkIn: Dayjs | null; checkOut: Dayjs | null };
export function BookingForm() {
const { control, handleSubmit, watch } = useForm<Booking>({
defaultValues: { checkIn: null, checkOut: null },
});
const checkIn = watch('checkIn');
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<form
noValidate
onSubmit={handleSubmit((values) =>
console.log({
checkIn: values.checkIn?.format('YYYY-MM-DD'),
checkOut: values.checkOut?.format('YYYY-MM-DD'),
}),
)}
>
<MuiXDatePickerElement
control={control}
name="checkIn"
label="Check-in"
required
disablePast
messages={{
required: 'Pick a check-in date',
disablePast: "Check-in can't be in the past",
}}
/>
<MuiXDatePickerElement
control={control}
name="checkOut"
label="Check-out"
required
minDate={checkIn ?? undefined}
messages={{ minDate: 'Check-out must be after check-in' }}
/>
</form>
</LocalizationProvider>
);
}Each limit has a default message that messages can replace:
| Limit | Default message |
|---|---|
minDate | Date must be later than the minimum allowed date |
maxDate | Date should be earlier than the maximum allowed date |
disablePast | Past date is not allowed |
disableFuture | Future date is not allowed |
shouldDisableDate | Date is not allowed |
shouldDisableMonth | Month is not allowed |
shouldDisableYear | Year is not allowed |
| An unparseable typed date | Date is not a valid date (messages.invalidDate) |
An empty picker passes these checks, so add required when a date is mandatory. Your own
rules.validate functions still run alongside them.
Desktop, mobile and static versions
MuiXDatePickerElement renders MUI X's DatePicker, which picks its layout from the device: a popover when the pointer is precise, such as a mouse, and a dialog on touch screens. To fix the layout, use one of these elements instead. They come from the same import path and take the same props, validation and messages as MuiXDatePickerElement.
| Element | Renders | Opens as |
|---|---|---|
MuiXDesktopDatePickerElement | DesktopDatePicker | A popover next to the field, on every device |
MuiXMobileDatePickerElement | MobileDatePicker | A dialog with action buttons, on every device |
MuiXStaticDatePickerElement | StaticDatePicker | Always visible on the page, with no text field |
'use client';import Stack from '@mui/material/Stack';import { LocalizationProvider } from '@mui/x-date-pickers';import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';import { MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { MuiXDesktopDatePickerElement } from '@piplup/rhf-adapters/mui-x-date-pickers';import dayjs from 'dayjs';import { useForm } from 'react-hook-form';export default function Page() { const { control, handleSubmit } = useForm({ defaultValues: { startDate: dayjs() }, }); return ( <LocalizationProvider dateAdapter={AdapterDayjs}> <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}> <Stack direction="column" spacing={2}> <MuiXDesktopDatePickerElement control={control} label="Start date" name="startDate" /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> </LocalizationProvider> );}MuiXStaticDatePickerElement validates the field but shows no error message, because a static picker has no text field to put it in. It takes no slotProps, inputRef or onClose, and focus can't move to it when validation fails. To show the message, build the field with useMuiXStaticDatePickerAdapter and render the error yourself.
Each version has its own hook for custom fields, documented on the useMuiXDatePickerAdapter page.
Props
Prop
Type
MuiXDatePickerElement supports all standard props of the MUI X DatePicker component, along with additional adapter-specific props. For a complete list of available DatePicker properties, refer to the MUI X Date Pickers API documentation.
The
refis forwarded to the underlying<DatePicker />component.
Notes
- For custom picker UIs, build on
useMuiXDatePickerAdapter. - The error state and message go to the picker's text field through
slotProps.textField. Your ownslotProps.textFieldstill applies; a validation error takes precedence over itshelperText. timezoneis passed to both the picker and its validation.
FAQ
How do I use the MUI X DatePicker with React Hook Form?
Wrap the form in MUI X's LocalizationProvider with a date adapter such as AdapterDayjs, then render MuiXDatePickerElement with control and a field name. The picked date is stored in the form, and validation errors show as the picker's helper text.
What type is the date stored in the form?
A date object from the adapter you gave LocalizationProvider: a Dayjs object with AdapterDayjs, a Date with AdapterDateFns, and so on. An empty picker stores null. Format it when you submit, for example values.date?.format("YYYY-MM-DD") with Dayjs.
Do minDate, maxDate and disablePast validate the form?
Yes. Besides limiting what the user can pick, they become react-hook-form validation, so a date typed outside the range fails on submit with its own message, such as "Past date is not allowed". Change the messages with the messages prop.
How do I make the end date come after the start date?
Watch the start date and pass it as the end picker's minDate, with a messages.minDate of your own. The picker then greys out earlier days, and an earlier date typed by hand fails validation with your message.
What happens when the user types an invalid date?
The field fails validation with "Date is not a valid date", which you can change with messages.invalidDate. An empty picker isn't invalid; use required to make the date mandatory.
What is the difference between DatePicker, DesktopDatePicker, MobileDatePicker and StaticDatePicker?
DatePicker opens a popover when the pointer is precise, such as a mouse, and a dialog on touch screens. DesktopDatePicker always opens a popover, MobileDatePicker always opens a dialog, and StaticDatePicker is always visible with no text field. Each has an element, such as MuiXMobileDatePickerElement, with the same props and validation.
See also
MuiXDateFieldElement
Use the MUI X DateField with react-hook-form. MuiXDateFieldElement binds the typed date and its validation to a form field, no Controller needed.
MuiXDateTimePickerElement
Use the MUI X DateTimePicker with react-hook-form: bind the picked date and time and its validation to a form field without writing a Controller.