Skip to content

Commit

Permalink
fix(DatePicker, react-datepicker-compat): Make year picker react to g…
Browse files Browse the repository at this point in the history
…o to today button (#28907)

* fix(DatePicker): Make year picker react to go to today.

* fix: Make year picker react to go to today.

* change fileS

* Update packages/react/src/components/Calendar/CalendarYear/CalendarYear.base.tsx

Co-authored-by: Makoto Morimoto <[email protected]>

* Update packages/react-components/react-datepicker-compat/src/components/CalendarYear/CalendarYear.tsx

Co-authored-by: Makoto Morimoto <[email protected]>

---------

Co-authored-by: Makoto Morimoto <[email protected]>
  • Loading branch information
sopranopillow and khmakoto authored Aug 17, 2023
1 parent 63595d2 commit d73590f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(DatePicker): Make year picker react to go to today.",
"packageName": "@fluentui/react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: Make year picker react to go to today.",
"packageName": "@fluentui/react-datepicker-compat",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export const CalendarMonth: React.FunctionComponent<CalendarMonthProps> = props
selectedYear={
selectedDate ? selectedDate.getFullYear() : navigatedDate ? navigatedDate.getFullYear() : undefined
}
navigatedYear={navigatedDate.getFullYear()}
onRenderYear={onRenderYear}
strings={yearStrings}
componentRef={calendarYearRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,26 +377,26 @@ function useAnimateBackwards({ selectedYear, navigatedYear }: CalendarYearProps)
}
}

const NavigationDirection = {
Previous: 0 as const,
Next: 1 as const,
};

function useYearRangeState({ selectedYear, navigatedYear }: CalendarYearProps) {
const [fromYear, navigate] = React.useReducer(
(state: number, action: (typeof NavigationDirection)[keyof typeof NavigationDirection]): number => {
return state + (action === NavigationDirection.Next ? CELL_COUNT : -CELL_COUNT);
},
undefined,
() => {
const rangeYear = selectedYear || navigatedYear || new Date().getFullYear();
return Math.floor(rangeYear / 10) * 10;
},
);
const toYear = fromYear + CELL_COUNT - 1;
const rangeYear = React.useMemo(() => {
return selectedYear || navigatedYear || Math.floor(new Date().getFullYear() / 10) * 10;
}, [navigatedYear, selectedYear]);

const onNavNext = () => navigate(NavigationDirection.Next);
const onNavPrevious = () => navigate(NavigationDirection.Previous);
const [fromYear, setFromYear] = React.useState<number>(rangeYear);

const onNavNext = () => {
setFromYear(year => year + CELL_COUNT);
};

const onNavPrevious = () => {
setFromYear(year => year - CELL_COUNT);
};

React.useEffect(() => {
setFromYear(rangeYear);
}, [rangeYear]);

const toYear = fromYear + CELL_COUNT - 1;

return [fromYear, toYear, onNavNext, onNavPrevious] as const;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export const CalendarMonthBase: React.FunctionComponent<ICalendarMonthProps> = p
selectedYear={
selectedDate ? selectedDate.getFullYear() : navigatedDate ? navigatedDate.getFullYear() : undefined
}
navigatedYear={navigatedDate.getFullYear()}
onRenderYear={onRenderYear}
strings={yearStrings}
componentRef={calendarYearRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,26 +398,26 @@ function useAnimateBackwards({ selectedYear, navigatedYear }: ICalendarYearProps
}
}

const enum NavigationDirection {
Previous,
Next,
}

function useYearRangeState({ selectedYear, navigatedYear }: ICalendarYearProps) {
const [fromYear, navigate] = React.useReducer(
(state: number, action: NavigationDirection): number => {
return state + (action === NavigationDirection.Next ? CELL_COUNT : -CELL_COUNT);
},
undefined,
() => {
const rangeYear = selectedYear || navigatedYear || new Date().getFullYear();
return Math.floor(rangeYear / 10) * 10;
},
);
const toYear = fromYear + CELL_COUNT - 1;
const rangeYear = React.useMemo(() => {
return selectedYear || navigatedYear || Math.floor(new Date().getFullYear() / 10) * 10;
}, [navigatedYear, selectedYear]);

const onNavNext = () => navigate(NavigationDirection.Next);
const onNavPrevious = () => navigate(NavigationDirection.Previous);
const [fromYear, setFromYear] = React.useState<number>(rangeYear);

const onNavNext = () => {
setFromYear(year => year + CELL_COUNT);
};

const onNavPrevious = () => {
setFromYear(year => year - CELL_COUNT);
};

React.useEffect(() => {
setFromYear(rangeYear);
}, [rangeYear]);

const toYear = fromYear + CELL_COUNT - 1;

return [fromYear, toYear, onNavNext, onNavPrevious] as const;
}
Expand Down

0 comments on commit d73590f

Please sign in to comment.