Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pickers] Fix DigitalClock time options on a DST switch day #10793

Merged
merged 11 commits into from
Oct 24, 2024
16 changes: 8 additions & 8 deletions packages/x-date-pickers/src/DigitalClock/DigitalClock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,14 @@ export const DigitalClock = React.forwardRef(function DigitalClock<TDate extends
);

const timeOptions = React.useMemo(() => {
const result: TDate[] = [];
const startOfDay = utils.startOfDay(valueOrReferenceDate);
return [
startOfDay,
...Array.from({ length: Math.ceil((24 * 60) / timeStep) - 1 }, (_, index) =>
utils.addMinutes(startOfDay, timeStep * (index + 1)),
),
];
let nextTimeStepOption = startOfDay;
while (utils.isSameDay(valueOrReferenceDate, nextTimeStepOption)) {
result.push(nextTimeStepOption);
nextTimeStepOption = utils.addMinutes(nextTimeStepOption, timeStep);
}
return result;
}, [valueOrReferenceDate, timeStep, utils]);

const focusedOptionIndex = timeOptions.findIndex((option) =>
Expand Down Expand Up @@ -348,10 +349,9 @@ export const DigitalClock = React.forwardRef(function DigitalClock<TDate extends
const formattedValue = utils.format(option, ampm ? 'fullTime12h' : 'fullTime24h');
const tabIndex =
focusedOptionIndex === index || (focusedOptionIndex === -1 && index === 0) ? 0 : -1;

return (
<ClockItem
key={formattedValue}
key={`${option.valueOf()}-${formattedValue}`}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only using option.valueOf() fails on AdapterDayjs as it handles DST in a strange manner—they don't have duplicate options for the same hour, they format the duplicate offset time differently, but that's why we need to include the formattedValue in the key to ensure uniqueness. 🙈
Screenshot 2024-10-24 at 12 01 22

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 That is weird indeed

We could also format with the day I guess, but it's probably not worth the computation time that is higher than .valueOf +a formatted value that we are also using elsewhere.

onClick={() => !readOnly && handleItemSelect(option)}
selected={isSelected}
disabled={disabled || isTimeDisabled(option)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,71 @@ describe('<DigitalClock /> - Timezone', () => {
expect(actualDate).toEqualDateTime(expectedDate);
});

it('should render correct time options when fall back DST occurs', () => {
render(
<DigitalClock
referenceDate={adapter.date('2023-11-05T12:00:00', 'America/New_York')}
timezone="America/New_York"
timeStep={30}
/>,
);
const oneAM = adapter.setMinutes(adapter.setHours(adapter.date(undefined, 'default'), 1), 0);
const elevenPM = adapter.setMinutes(
adapter.setHours(adapter.date(undefined, 'default'), 23),
0,
);
expect(
screen.getAllByText(
adapter.format(
oneAM,
adapter.is12HourCycleInCurrentLocale() ? 'fullTime12h' : 'fullTime24h',
),
),
).to.have.length(adapter.lib === 'dayjs' ? 1 : 2);
expect(
screen.getAllByText(
adapter.format(
elevenPM,
adapter.is12HourCycleInCurrentLocale() ? 'fullTime12h' : 'fullTime24h',
),
),
).to.have.length(1);
});

it('should contain time options until the end of day when spring forward DST occurs', () => {
render(
<DigitalClock
referenceDate={adapter.date('2024-03-10T12:00:00', 'America/New_York')}
timezone="America/New_York"
timeStep={30}
/>,
);
const startOfDay = adapter.setMinutes(
adapter.setHours(adapter.date(undefined, 'default'), 0),
0,
);
const eleven30PM = adapter.setMinutes(
adapter.setHours(adapter.date(undefined, 'default'), 23),
30,
);
expect(
screen.getAllByText(
adapter.format(
startOfDay,
adapter.is12HourCycleInCurrentLocale() ? 'fullTime12h' : 'fullTime24h',
),
),
).to.have.length(1);
expect(
screen.getAllByText(
adapter.format(
eleven30PM,
adapter.is12HourCycleInCurrentLocale() ? 'fullTime12h' : 'fullTime24h',
),
),
).to.have.length(1);
});

TIMEZONE_TO_TEST.forEach((timezone) => {
describe(`Timezone: ${timezone}`, () => {
it('should use timezone prop for onChange when no value is provided', () => {
Expand Down
Loading