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

feat(date): fix focus loss when using navbar with keyboard #7166

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions playwright/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ const mountedTheme = (theme: string) => {

// Setup required providers on mounted component before running test. See https://playwright.dev/docs/test-components#hooks
beforeMount<HooksConfig>(async ({ App, hooksConfig }) => {
const {
theme = "sage",
validationRedesignOptIn,
} = hooksConfig || {};
const { theme = "sage", validationRedesignOptIn } = hooksConfig || {};
return (
<CarbonProvider
theme={mountedTheme(theme)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const popoverMiddleware = [
}),
];

const Nav = Navbar;

export const DatePicker = ({
inputElement,
minDate,
Expand Down Expand Up @@ -250,16 +252,14 @@ export const DatePicker = ({
weekdaysShort,
},
}}
selected={focusedMonth}
selected={selectedDays}
month={focusedMonth || /* istanbul ignore next */ new Date()}
onDayClick={(d, _, e) => {
const date = d as Date;
handleDayClick(date, e);
}}
components={{
Nav: (props) => {
return <Navbar {...props} />;
},
Nav,
Weekday: (props) => {
const fixedDays = {
Sunday: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,31 @@ test("should correctly translate the month caption for the given locale (zh-CN)"
expect(monthCaption).toBeVisible();
expect(monthCaption).toHaveTextContent("三月 2019");
});

test("navigation buttons should maintain focus between month changes when using the keyboard", async () => {
const user = userEvent.setup();
render(
<DatePickerWithInput
setOpen={() => {}}
open
selectedDays={new Date(2019, 3, 4)}
/>,
);

const textbox = screen.getByRole("textbox");
const monthLabel = screen.getByText("April 2019");
const previousButton = screen.getByRole("button", { name: "Previous month" });
const nextButton = screen.getByRole("button", { name: "Next month" });

await user.click(textbox);
await user.tab();
expect(previousButton).toHaveFocus();
await user.keyboard("{enter}");
expect(monthLabel).toHaveTextContent("March 2019");
expect(previousButton).toHaveFocus();
await user.tab();
expect(nextButton).toHaveFocus();
await user.keyboard("{enter}");
expect(monthLabel).toHaveTextContent("April 2019");
expect(nextButton).toHaveFocus();
});
Loading