MuiXTimeClockElement
Use the MUI X TimeClock with react-hook-form. MuiXTimeClockElement binds the selected time and its validation to a form field, no Controller needed.
Import
import { MuiXTimeClockElement } 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 { MuiXTimeClockElement } 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: { time: dayjs() }, }); return ( <LocalizationProvider dateAdapter={AdapterDayjs}> <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}> <Stack direction="column" spacing={2}> <MuiXTimeClockElement control={control} name="time" /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Submit </MuiButtonElement> </div> </Stack> </form> </LocalizationProvider> );}Props
Prop
Type
MuiXTimeClockElement supports all standard props of the MUI X TimeClock component, along with additional adapter-specific props. For a complete list of available TimeClock properties, refer to the MUI X Date Pickers API documentation.
The
refis forwarded to the underlying<TimeClock />component.
useMuiXTimeClockAdapter
useMuiXTimeClockAdapter connects a field to React Hook Form and returns props you spread onto MUI X's analog TimeClock. It's what MuiXTimeClockElement uses internally.
The element shows the clock only and drops the error message. Use the hook when the clock sits inline in a form, such as an alarm or reminder setting, and you need to show why the stored value was rejected.
Import
import { useMuiXTimeClockAdapter } from '@piplup/rhf-adapters/mui-x-date-pickers';This WakeUpClock rejects times before 06:00. The form starts with a saved value of 05:30. The clock disables hours before 06:00, but the stored value is still checked: submit straight away to see the error, then pick a new time.
'use client';import FormHelperText from '@mui/material/FormHelperText';import Stack from '@mui/material/Stack';import { LocalizationProvider, TimeClock } from '@mui/x-date-pickers';import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';import { MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { useMuiXTimeClockAdapter } 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 = { wakeUp: Dayjs | null };const earliest = dayjs().hour(6).minute(0);// TimeClock has no text field and no `error` or `helperText` props, so they are// pulled out of the adapter and rendered below the clock instead.function WakeUpClock({ control }: { control: Control<FormValues> }) { const { error, helperText, required, ...adapter } = useMuiXTimeClockAdapter< Dayjs, FormValues, 'wakeUp', HTMLDivElement >({ composeHelperText: true, control, messages: { minTime: "Alarms can't be set before 06:00", required: 'Set a wake-up time', }, minTime: earliest, name: 'wakeUp', required: true, }); return ( <div> <TimeClock ampm={false} views={['hours', 'minutes']} {...adapter} /> <FormHelperText error={error}>{helperText}</FormHelperText> </div> );}export default function Page() { // The saved value (05:30) is earlier than `minTime`. The clock disables those // hours, but submitting still reports the stored value as invalid. const { control, handleSubmit } = useForm<FormValues>({ defaultValues: { wakeUp: dayjs().hour(5).minute(30) }, }); return ( <LocalizationProvider dateAdapter={AdapterDayjs}> <form onSubmit={handleSubmit((values) => alert(values.wakeUp?.format('HH:mm')))} noValidate> <Stack direction="column" spacing={2}> <WakeUpClock control={control} /> <div> <MuiButtonElement control={control} type="submit" variant="contained"> Set alarm </MuiButtonElement> </div> </Stack> </form> </LocalizationProvider> );}How it works
Validation and default messages are the same as useMuiXTimePickerAdapter. The clock disables the hours and minutes that minTime, maxTime, minutesStep and shouldDisableTime reject, because the hook returns those props. That only limits new picks. A value that is already in the form, from defaultValues, reset or setValue, is validated as-is.
The result is shaped for a component with no text field:
error,helperTextandrequiredare plain values.TimeClockdoes not accept these props and passes unknown props to its root element, so take them out of the result before spreading it. The element does the same.- With
composeHelperText: true,helperTextholds the error message while the field is invalid, and yourhelperTextprop otherwise. - There is no
slotProps,inputReforonClose. The hook's second argument is merged with React Hook Form's ref and returned asref, which ends up on the clock's root element.
Touched. The hook does not accept an onBlur prop. The returned onBlur is React Hook Form's own, and TimeClock puts it on its root element. React blur events bubble, so the field is marked as touched when focus leaves any element inside the clock, for example after picking an hour with the keyboard.
TimeClock updates the value at each step: moving the hour hand already sets a full time, before
the user reaches the minutes view. With mode: "onChange", an error can show while the user is
still picking minutes.
Props
Prop
Type
Return value
Prop
Type
See also
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.
MuiXTimeFieldElement
Use the MUI X TimeField with react-hook-form. MuiXTimeFieldElement binds the typed time and its validation to a form field, no Controller needed.