Skip to content

Commit

Permalink
[pickers] Fix referenceDate day calendar focus (#12136)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy authored Feb 19, 2024
1 parent 48556d0 commit 4afe88f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,17 @@ describe('<DateCalendar />', () => {
render(
<DateCalendar
onChange={onChange}
referenceDate={adapterToUse.date(new Date(2018, 0, 1, 12, 30))}
referenceDate={adapterToUse.date(new Date(2022, 3, 17, 12, 30))}
view="day"
/>,
);

// should make the reference day firstly focusable
expect(screen.getByRole('gridcell', { name: '17' })).to.have.attribute('tabindex', '0');

userEvent.mousePress(screen.getByRole('gridcell', { name: '2' }));
expect(onChange.callCount).to.equal(1);
expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2018, 0, 2, 12, 30));
expect(onChange.lastCall.firstArg).toEqualDateTime(new Date(2022, 3, 2, 12, 30));
});

it('should not use `referenceDate` when a value is defined', () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/x-date-pickers/src/DateCalendar/useCalendarState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import useEventCallback from '@mui/utils/useEventCallback';
import { SlideDirection } from './PickersSlideTransition';
import { useIsDateDisabled } from './useIsDateDisabled';
import { useUtils, useNow } from '../internals/hooks/useUtils';
import { useUtils } from '../internals/hooks/useUtils';
import { MuiPickersAdapter, PickersTimezone } from '../models';
import { DateCalendarDefaultizedProps } from './DateCalendar.types';
import { singleItemValueManager } from '../internals/utils/valueManagers';
Expand Down Expand Up @@ -127,7 +127,6 @@ export const useCalendarState = <TDate extends unknown>(params: UseCalendarState
timezone,
} = params;

const now = useNow<TDate>(timezone);
const utils = useUtils<TDate>();

const reducerFn = React.useRef(
Expand Down Expand Up @@ -162,7 +161,7 @@ export const useCalendarState = <TDate extends unknown>(params: UseCalendarState

const [calendarState, dispatch] = React.useReducer(reducerFn, {
isMonthSwitchingAnimating: false,
focusedDay: utils.isValid(value) ? value! : now,
focusedDay: referenceDate,
currentMonth: utils.startOfMonth(referenceDate),
slideDirection: 'left',
});
Expand Down

0 comments on commit 4afe88f

Please sign in to comment.