1

I'm using MUI v6 Time Pickers and I'm trying to use renderDigitalClockTimeView as the viewRenderer for my time picker component.

I set an initial value, and it correctly shows up in the input field. However, when I open the time picker pop-up, the corresponding time is not highlighted/auto-selected.

If I switch to renderMultiSectionDigitalClockTimeView, the same code works correctly and the initial time is auto-selected in the clock.

Here is a minimal example demonstrating the issue: https://codesandbox.io/p/sandbox/silly-night-yw2tsw?file=%2Fsrc%2FMultisectionTimePicker.js%3A7%2C10-7%2C48

Expected behavior: The initial time should be automatically highlighted when using renderDigitalClockTimeView, just like it works when using renderMultiSectionDigitalClockTimeView.

Actual behavior: The initial time is shown only in the input field, but the time picker UI does not show the selected value.

Question: Is this a known behavior difference between renderDigitalClockTimeView and renderMultiSectionDigitalClockTimeView? Is there a way to ensure the initial value is auto-selected/highlighted when using renderDigitalClockTimeView?

Image 1 properly highlighting intial time passed to it. Image 2 is not highlighting initial time passed to it

Image 1 properly highlighting intial time

Image 2 is not highlighting initial time passed to it

1 Answer 1

2

The DigitalClock handles selection of a single time instance in one step, just like a select component.The MultiSectionDigitalClock lets users select time using separate sections for separate views.

If your initial value has non-zero seconds or milliseconds, it won’t exactly match any list item, so it won’t be highlighted.

MultiSectionDigitalClockTimeView, on the other hand, compares each section (hour/minute/second) independently, so it appears to “auto-select” correctly.

Make sure your initial value is aligned with the clock’s step and has zero seconds and milliseconds:

import { DemoContainer } from "@mui/x-date-pickers/internals/demo";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
import { TimePicker } from "@mui/x-date-pickers/TimePicker";
import { useEffect, useState } from "react";
import dayjs from "dayjs";
import { renderDigitalClockTimeView } from "@mui/x-date-pickers";
export const DigitalClockTimePicker = () => {
  const [value, setValue] = useState(
    dayjs().set("minutes", 0).set("second", 0).set("millisecond", 0)
  );

  return (
    <LocalizationProvider dateAdapter={AdapterDayjs}>
      <DemoContainer components={["TimePicker"]}>
        <TimePicker
          sx={{
            width: "100px",
          }}
          value={value}
          onChange={(val) => {
            setValue(val);
          }}
          label="Digital clock time picker"
          viewRenderers={{
            hours: renderDigitalClockTimeView,
            minutes: renderDigitalClockTimeView,
          }}
        />
      </DemoContainer>
    </LocalizationProvider>
  );
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Navid Abedini for your answer. Understood the root cause. It definitely helped

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.