Skip to content

Commit

Permalink
feat(date): fix focus loss when using navbar with keyboard
Browse files Browse the repository at this point in the history
The Navbar of the internal date picker component has been memoized to ensure that it doesn't
rerender unless it absolutely needs to (which should be never as it's just two buttons; the month
name isn't part of the navbar, it's just styled that way)

fix #7158
  • Loading branch information
damienrobson-sage committed Jan 21, 2025
1 parent fcf8d1b commit 4f4d19d
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, {
useMemo,
useRef,
useState,
memo,
} from "react";
import {
DayPicker,
Expand Down Expand Up @@ -72,6 +73,17 @@ const popoverMiddleware = [
}),
];

/** The Navbar has been lifted out of the main component to ensure that it can be properly
* memoized and not re-rendered on every update of the DatePicker component.
*
* Resolves https://jira.sage.com/browse/FE-7047
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Nav = (props: any) => {
return <Navbar {...props} />;
};
const MemoizedNav = memo(Nav);

export const DatePicker = ({
inputElement,
minDate,
Expand Down Expand Up @@ -250,16 +262,17 @@ export const DatePicker = ({
weekdaysShort,
},
}}
selected={focusedMonth}
selected={selectedDays}

Check failure on line 265 in src/components/date/__internal__/date-picker/date-picker.component.tsx

View workflow job for this annotation

GitHub Actions / Lint

Type 'Date | undefined' is not assignable to type 'DateRange | undefined'.
month={focusedMonth || /* istanbul ignore next */ new Date()}
onDayClick={(d, _, e) => {
const date = d as Date;
handleDayClick(date, e);
}}
components={{
Nav: (props) => {
return <Navbar {...props} />;
},
// TODO: Fix this. It's a horrible approach to take.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Type 'MemoizedNav' is not assignable to type 'ComponentType<NavbarProps>'.
Nav: MemoizedNav,
Weekday: (props) => {
const fixedDays = {
Sunday: 0,
Expand Down

0 comments on commit 4f4d19d

Please sign in to comment.