MuiXMonthCalendarElement
Use the MUI X MonthCalendar with react-hook-form. MuiXMonthCalendarElement binds the selected month and its validation to a form field, no Controller needed.
Import
import { MuiXMonthCalendarElement } from '@piplup/rhf-adapters/mui-x-date-pickers';Usage
'use client';import { LocalizationProvider } from '@mui/x-date-pickers';import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';import { MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { MuiXMonthCalendarElement } 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: { month: dayjs() }, }); return ( <LocalizationProvider dateAdapter={AdapterDayjs}> <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}> <MuiXMonthCalendarElement control={control} name="month" /> <MuiButtonElement control={control} sx={{ mt: 2 }} type="submit" variant="contained"> Submit </MuiButtonElement> </form> </LocalizationProvider> );}Props
Prop
Type
MuiXMonthCalendarElement supports all standard props of the MUI X MonthCalendar component, along with additional adapter-specific props. For a complete list of available MonthCalendar properties, refer to the MUI X Date Pickers API documentation.
The
refis forwarded to the underlying<MonthCalendar />component.
useMuiXMonthCalendarAdapter
useMuiXMonthCalendarAdapter returns React Hook Form props for MUI X's MonthCalendar, a grid of the twelve months of one year. It's what MuiXMonthCalendarElement uses internally.
Use the hook instead of the element when you need to show the validation message, add a visible label for the grid, or keep month rules for something like a billing start in one component.
Import
import { useMuiXMonthCalendarAdapter } from '@piplup/rhf-adapters/mui-x-date-pickers';This BillingMonthField accepts the current month or a later one. Submit without choosing to see the required message under the grid.
'use client';import FormHelperText from '@mui/material/FormHelperText';import Stack from '@mui/material/Stack';import Typography from '@mui/material/Typography';import { LocalizationProvider, MonthCalendar } from '@mui/x-date-pickers';import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';import { MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { useMuiXMonthCalendarAdapter } from '@piplup/rhf-adapters/mui-x-date-pickers';import dayjs from 'dayjs';import { useForm } from 'react-hook-form';import type { Dayjs } from 'dayjs';import type { Control } from 'react-hook-form';type FormValues = { billingStart: Dayjs | null };// Compare by month, not by day. MonthCalendar stores the 1st of the month when// nothing is selected yet, so `disablePast` would reject the current month.function isPastMonth(month: Dayjs) { return month.isBefore(dayjs(), 'month');}function BillingMonthField({ control }: { control: Control<FormValues> }) { // Remove the props MonthCalendar does not accept; it would pass them to its root <div>. const { error, helperText, required, shouldDisableDate, shouldDisableYear, ...adapter } = useMuiXMonthCalendarAdapter<Dayjs, FormValues, 'billingStart', HTMLDivElement>({ composeHelperText: true, control, messages: { required: 'Choose the first month to bill', shouldDisableMonth: "Billing can't start in a past month", }, name: 'billingStart', required: true, shouldDisableMonth: isPastMonth, }); return ( <div> <Typography id="billing-start-label" variant="subtitle2"> First billing month ({dayjs().year()}) </Typography> <MonthCalendar gridLabelId="billing-start-label" {...adapter} /> <FormHelperText error={error}>{helperText}</FormHelperText> </div> );}export default function Page() { const { control, handleSubmit } = useForm<FormValues>({ defaultValues: { billingStart: null }, }); return ( <LocalizationProvider dateAdapter={AdapterDayjs}> <form onSubmit={handleSubmit((values) => alert(values.billingStart?.startOf('month').format('YYYY-MM-DD')), )} noValidate > <Stack direction="column" spacing={2}> <BillingMonthField control={control} /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Start subscription </MuiButtonElement> </div> </Stack> </form> </LocalizationProvider> );}How it works
The wiring is the same as useMuiXDateCalendarAdapter: there is no text field, so render the message yourself. MonthCalendar passes unknown props to its root <div>, and the result contains more of them than the DateCalendar one: remove error, helperText, required, shouldDisableDate and shouldDisableYear before spreading, as the example does. Otherwise React warns about unknown DOM props. The returned onBlur also lands on that root, so the field becomes touched when focus leaves a month button.
The value is still a full date. Selecting a month calls the date library's "set month" on the current value, so the day and time are kept. If the field is empty, MonthCalendar starts from the first day of the current month. In the example, picking March stores 1 March of this year. Normalize it on submit (the example calls startOf("month")) or with transform.output.
Validation compares days, not months. The hook runs the same date validation as useMuiXDatePickerAdapter, with its keys and default messages. disablePast, disableFuture, minDate and maxDate are checked to the day, while MonthCalendar enables and disables whole months.
With disablePast, picking the current month on an empty field stores the 1st of the month. Unless today is the 1st, that date is in the past and the form rejects a month the grid shows as enabled. A mid-month minDate has the same problem. Express month limits with shouldDisableMonth instead, comparing by month as the example does: (month) => month.isBefore(dayjs(), "month").
shouldDisableMonth is checked in both places: MonthCalendar uses it to disable buttons, and the hook uses it to validate the stored value with the shouldDisableMonth message. shouldDisableDate and shouldDisableYear are accepted and validated too, but MonthCalendar does not use them to disable buttons, which is why they are removed before spreading.
Props
Prop
Type
Return value
Prop
Type
See also
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.
MuiXMultiSectionDigitalClockElement
Use the MUI X MultiSectionDigitalClock with react-hook-form: bind the selected time and its validation to a form field without writing a Controller.