Skip to Content
Piplup
@piplup/rhf-adaptersmui-x-date-pickersComponents

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.

Import

import { MuiXMultiSectionDigitalClockElement } from '@piplup/rhf-adapters/mui-x-date-pickers';

Usage

  • 12
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 00
  • 05
  • 10
  • 15
  • 20
  • 25
  • 30
  • 35
  • 40
  • 45
  • 50
  • 55
  • AM
  • PM
'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 { MuiXMultiSectionDigitalClockElement } 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: { multi: dayjs() },  });  return (    <LocalizationProvider dateAdapter={AdapterDayjs}>      <form onSubmit={handleSubmit((values) => alert(JSON.stringify(values, null, 2)))}>        <Stack direction="column" spacing={2}>          <MuiXMultiSectionDigitalClockElement control={control} name="multi" />          <div>            <MuiButtonElement control={control} type="submit" variant="contained">              Submit            </MuiButtonElement>          </div>        </Stack>      </form>    </LocalizationProvider>  );}

Props

Prop

Type

MuiXMultiSectionDigitalClockElement supports all standard props of the MUI X MultiSectionDigitalClock component, along with additional adapter-specific props. For a complete list of available MultiSectionDigitalClock properties, refer to the MUI X Date Pickers API documentation.

The ref is forwarded to the underlying <MultiSectionDigitalClock /> component.

useMuiXMultiSectionDigitalClockAdapter

useMuiXMultiSectionDigitalClockAdapter connects a field to React Hook Form and returns props you spread onto MUI X's MultiSectionDigitalClock, which shows one scrollable column per section (hours, minutes, seconds, AM/PM). It's what MuiXMultiSectionDigitalClockElement uses internally.

The element shows the columns only and drops the error message. Use the hook when the clock is inline in your form and you want a separate message for each column's rule, for example one for minutes and one for seconds.

Import

import { useMuiXMultiSectionDigitalClockAdapter } from '@piplup/rhf-adapters/mui-x-date-pickers';

This DepartureClock allows departures every 5 minutes, at :00 or :30 seconds. The columns only list valid values, so submit without picking a time to see the required error below the clock.

  • 00
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 00
  • 05
  • 10
  • 15
  • 20
  • 25
  • 30
  • 35
  • 40
  • 45
  • 50
  • 55
  • 00
  • 30

'use client';import FormHelperText from '@mui/material/FormHelperText';import Stack from '@mui/material/Stack';import { LocalizationProvider, MultiSectionDigitalClock } from '@mui/x-date-pickers';import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';import { MuiButtonElement } from '@piplup/rhf-adapters/mui-material';import { useMuiXMultiSectionDigitalClockAdapter } from '@piplup/rhf-adapters/mui-x-date-pickers';import { useForm } from 'react-hook-form';import type { Dayjs } from 'dayjs';import type { Control } from 'react-hook-form';type FormValues = { departure: Dayjs | null };// Departures leave every 5 minutes, at :00 or :30 seconds past the minute.// Each column is checked separately, so each section gets its own message.function DepartureClock({ control }: { control: Control<FormValues> }) {  const { error, helperText, required, ...adapter } = useMuiXMultiSectionDigitalClockAdapter<    Dayjs,    FormValues,    'departure',    HTMLDivElement  >({    composeHelperText: true,    control,    messages: {      minutesStep: 'Departures leave every 5 minutes',      required: 'Pick a departure time',      'shouldDisableTime-seconds': 'Only :00 or :30 seconds',    },    minutesStep: 5,    name: 'departure',    required: true,    shouldDisableTime: (value, view) => view === 'seconds' && value.second() % 30 !== 0,  });  return (    <div>      <MultiSectionDigitalClock        ampm={false}        timeSteps={{ minutes: 5, seconds: 30 }}        views={['hours', 'minutes', 'seconds']}        {...adapter}      />      <FormHelperText error={error}>{helperText}</FormHelperText>    </div>  );}export default function Page() {  const { control, handleSubmit } = useForm<FormValues>({    defaultValues: { departure: null },  });  return (    <LocalizationProvider dateAdapter={AdapterDayjs}>      <form        onSubmit={handleSubmit((values) => alert(values.departure?.format('HH:mm:ss')))}        noValidate      >        <Stack direction="column" spacing={2}>          <DepartureClock control={control} />          <div>            <MuiButtonElement control={control} type="submit" variant="contained">              Save departure            </MuiButtonElement>          </div>        </Stack>      </form>    </LocalizationProvider>  );}

How it works

Validation and default messages are the same as useMuiXTimePickerAdapter. The result is shaped like the useMuiXTimeClockAdapter one: take error, helperText and required out before spreading, render the message yourself, and expect the field to become touched when focus leaves the columns.

One rule per column. MultiSectionDigitalClock checks each column with the matching view: hour options with shouldDisableTime(value, "hours"), minute options with "minutes" and second options with "seconds". Validation uses the same three views, so a rule written for one view disables options in that column and reports the matching key:

Rule in shouldDisableTimeDisables options inMessage key
view === "hours"hours columnshouldDisableTime-hours
view === "minutes"minutes columnshouldDisableTime-minutes
view === "seconds"seconds columnshouldDisableTime-seconds

Validation reports only the first failing check, in the order hours, minutes, seconds, then minutesStep. In the example, a value at 08:07:15 reports the seconds message, not the minutesStep one.

timeSteps and minutesStep are separate. timeSteps is a MultiSectionDigitalClock prop that only controls which options each column lists. minutesStep is a validation prop that the hook returns, so it also disables minute options. The example sets both so the minutes column and validation agree.

Props

Prop

Type

Return value

Prop

Type

See also

On this page