Skip to content

Commit f8d0938

Browse files
Enable enter to open someday events (#587)
* feat(web): open someday form on enter key * fix(web): stop event propagation on Enter key press to prevent unintended submit * chore(web): update SomedayEventContainer tests to use proper draggable props and types
1 parent 85b6d03 commit f8d0938

File tree

3 files changed

+105
-7
lines changed

3 files changed

+105
-7
lines changed

packages/web/src/views/Calendar/Calendar.form.test.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ describe("Event Form", () => {
2121
const user = userEvent.setup();
2222

2323
await act(async () => {
24-
await user.click(screen.getByRole("button", { name: CLIMB.title }));
24+
const climbBtn = document.querySelector(
25+
`[data-event-id="${CLIMB._id}"]`,
26+
) as HTMLElement;
27+
await user.click(climbBtn);
2528
});
2629

2730
await act(async () => {
@@ -64,11 +67,10 @@ describe("Event Form", () => {
6467
const { container } = render(<CalendarView />, { state: preloadedState });
6568

6669
await act(async () => {
67-
await user.click(
68-
screen.getByRole("button", {
69-
name: /climb/i,
70-
}),
71-
);
70+
const climbBtn = document.querySelector(
71+
`[data-event-id="${CLIMB._id}"]`,
72+
) as HTMLElement;
73+
await user.click(climbBtn);
7274
});
7375

7476
expect(container.getElementsByClassName("startDatePicker")).toHaveLength(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import React from "react";
2+
import { DraggableProvided, DraggableStateSnapshot } from "@hello-pangea/dnd";
3+
import "@testing-library/jest-dom";
4+
import { LEARN_CHINESE } from "@core/__mocks__/v1/events/events.misc";
5+
import { Categories_Event } from "@core/types/event.types";
6+
import { fireEvent, render } from "@web/__tests__/__mocks__/mock.render";
7+
import { SidebarDraftContext } from "../../../../Draft/sidebar/context/SidebarDraftContext";
8+
import { SomedayEventContainer } from "./SomedayEventContainer";
9+
10+
jest.mock(
11+
"@web/views/Calendar/components/Draft/hooks/state/useDraftForm",
12+
() => ({
13+
useDraftForm: () => ({
14+
context: {},
15+
refs: { setFloating: jest.fn(), setReference: jest.fn() },
16+
strategy: "absolute",
17+
x: 0,
18+
y: 0,
19+
getReferenceProps: () => ({}),
20+
getFloatingProps: () => ({}),
21+
}),
22+
}),
23+
);
24+
25+
describe("SomedayEventContainer keyboard interactions", () => {
26+
it("opens the form when Enter is pressed", async () => {
27+
const onDraft = jest.fn();
28+
const contextValue = {
29+
state: {
30+
draft: null,
31+
isDrafting: false,
32+
isOverGrid: false,
33+
isSomedayFormOpen: false,
34+
} as any,
35+
setters: {
36+
setIsSomedayFormOpen: jest.fn(),
37+
} as any,
38+
actions: {
39+
onDraft,
40+
onMigrate: jest.fn(),
41+
discard: jest.fn(),
42+
reset: jest.fn(),
43+
closeForm: jest.fn(),
44+
close: jest.fn(),
45+
} as any,
46+
};
47+
48+
render(
49+
<SidebarDraftContext.Provider value={contextValue as any}>
50+
<SomedayEventContainer
51+
category={Categories_Event.SOMEDAY_WEEK}
52+
event={LEARN_CHINESE}
53+
isDrafting={false}
54+
isDragging={false}
55+
isOverGrid={false}
56+
onSubmit={jest.fn()}
57+
provided={
58+
{
59+
draggableProps: {
60+
"data-rfd-draggable-context-id": "mock-context",
61+
"data-rfd-draggable-id": "mock-id",
62+
style: {},
63+
},
64+
dragHandleProps: null,
65+
innerRef: jest.fn(),
66+
} as DraggableProvided
67+
}
68+
snapshot={{ isDragging: false } as DraggableStateSnapshot}
69+
setEvent={jest.fn()}
70+
weekViewRange={{ startDate: "2020-01-01", endDate: "2020-01-07" }}
71+
/>
72+
</SidebarDraftContext.Provider>,
73+
);
74+
75+
const btn = document.querySelector(
76+
`[data-event-id="${LEARN_CHINESE._id}"]`,
77+
)! as HTMLElement;
78+
btn.focus();
79+
fireEvent.keyDown(btn, { key: "Enter" });
80+
81+
expect(onDraft).toHaveBeenCalledTimes(1);
82+
expect(onDraft).toHaveBeenCalledWith(
83+
LEARN_CHINESE,
84+
Categories_Event.SOMEDAY_WEEK,
85+
);
86+
});
87+
});

packages/web/src/views/Calendar/components/Sidebar/SomedayTab/SomedayEvents/SomedayEventContainer/SomedayEventContainer.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState } from "react";
2+
import { Key } from "ts-key-enum";
23
import { FloatingFocusManager, FloatingPortal } from "@floating-ui/react";
34
import { DraggableProvided, DraggableStateSnapshot } from "@hello-pangea/dnd";
45
import { Priorities } from "@core/constants/core.constants";
@@ -51,6 +52,14 @@ export const SomedayEventContainer = ({
5152

5253
const [isFocused, setIsFocused] = useState(false);
5354

55+
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
56+
if (e.key === Key.Enter) {
57+
e.preventDefault();
58+
e.stopPropagation();
59+
actions.onDraft(event, category);
60+
}
61+
};
62+
5463
const isDraftingThisEvent =
5564
state.isDrafting && state.draft?._id === event._id;
5665

@@ -68,7 +77,7 @@ export const SomedayEventContainer = ({
6877
actions.onDraft(event, category);
6978
}}
7079
onFocus={() => setIsFocused(true)}
71-
onKeyDown={() => console.log("onKeyDown")}
80+
onKeyDown={handleKeyDown}
7281
priority={event.priority || Priorities.UNASSIGNED}
7382
provided={provided}
7483
snapshot={snapshot}

0 commit comments

Comments
 (0)