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.
Import
import { MuiXDateTimePickerElement } 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 { MuiXDateTimePickerElement } 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}> <MuiXDateTimePickerElement control={control} label="Appointment" name="appointment" /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> </LocalizationProvider> );}Desktop, mobile and static versions
MuiXDateTimePickerElement renders MUI X's DateTimePicker, 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 MuiXDateTimePickerElement.
| Element | Renders | Opens as |
|---|---|---|
MuiXDesktopDateTimePickerElement | DesktopDateTimePicker | A popover next to the field, on every device |
MuiXMobileDateTimePickerElement | MobileDateTimePicker | A dialog with action buttons, on every device |
MuiXStaticDateTimePickerElement | StaticDateTimePicker | 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 { MuiXDesktopDateTimePickerElement } 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: { startDatetime: dayjs() }, }); return ( <LocalizationProvider dateAdapter={AdapterDayjs}> <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}> <Stack direction="column" spacing={2}> <MuiXDesktopDateTimePickerElement control={control} label="Start datetime" name="startDatetime" /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> </LocalizationProvider> );}MuiXStaticDateTimePickerElement 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 useMuiXStaticDateTimePickerAdapter and render the error yourself.
Each version has its own hook for custom fields, documented on the useMuiXDateTimePickerAdapter page.
Props
Prop
Type
MuiXDateTimePickerElement supports all standard props of the MUI X DateTimePicker component, along with additional adapter-specific props. For a complete list of available DateTimePicker properties, refer to the MUI X Date Pickers API documentation.
The
refis forwarded to the underlying<DateTimePicker />component.
Notes
- For custom date-time pickers, build on
useMuiXDateTimePickerAdapter.
See also
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.
MuiXDigitalClockElement
Use the MUI X DigitalClock with react-hook-form. MuiXDigitalClockElement binds the selected time and its validation to a form field, no Controller needed.