Skip to content

Commit e8ea1ad

Browse files
committed
Clean up
1 parent f7a188c commit e8ea1ad

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/components/Buttons/NoteButton.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ export default function NoteButton(props: NoteButtonProps): ReactElement {
2121

2222
const handleOpen = (): void => {
2323
setNoteOpen(true);
24-
// Blur the button to prevent it from receiving focus when dialog closes
25-
if (document.activeElement instanceof HTMLElement) {
26-
document.activeElement.blur();
24+
25+
if (props.onExited) {
26+
// Allow custom focus handling after dialog closes
27+
if (document.activeElement instanceof HTMLElement) {
28+
// Blur the button to prevent it from receiving focus when dialog closes
29+
document.activeElement.blur();
30+
}
2731
}
2832
};
2933

src/components/DataEntry/DataEntryTable/NewEntry/tests/index.test.tsx

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import configureMockStore from "redux-mock-store";
88
import NewEntry, {
99
NewEntryId,
1010
} from "components/DataEntry/DataEntryTable/NewEntry";
11-
import { focusInput } from "components/DataEntry/utilities";
1211
import { newWritingSystem } from "types/writingSystem";
1312

1413
jest.mock("components/DataEntry/utilities.ts", () => ({
1514
...jest.requireActual("components/DataEntry/utilities.ts"),
16-
focusInput: jest.fn(),
15+
focusInput: () => mockFocusInput(),
1716
}));
1817
jest.mock("components/Pronunciations/PronunciationsFrontend", () => jest.fn());
1918

2019
const mockAddNewAudio = jest.fn();
2120
const mockAddNewEntry = jest.fn();
2221
const mockDelNewAudio = jest.fn();
22+
const mockFocusInput = jest.fn();
2323
const mockSetNewGloss = jest.fn();
2424
const mockSetNewNote = jest.fn();
2525
const mockSetNewVern = jest.fn();
@@ -81,7 +81,7 @@ const fireEnterOnActiveElement = async (): Promise<void> => {
8181
};
8282

8383
beforeEach(() => {
84-
jest.resetAllMocks();
84+
jest.clearAllMocks();
8585
});
8686

8787
afterEach(() => {
@@ -177,34 +177,29 @@ describe("NewEntry", () => {
177177
});
178178

179179
it("returns focus to gloss after closing note dialog", async () => {
180-
(focusInput as jest.Mock).mockClear();
181-
await renderNewEntry("", "", "");
180+
await renderNewEntry();
181+
mockFocusInput.mockClear();
182182

183183
// Click the note button to open the dialog
184-
const noteButton = screen.getByTestId(NewEntryId.ButtonNote);
185-
await userEvent.click(noteButton);
186-
187-
// Click cancel to close the dialog
188-
const cancelButton = screen.getByTestId("note-edit-cancel");
189-
await userEvent.click(cancelButton);
184+
await userEvent.click(screen.getByTestId(NewEntryId.ButtonNote));
185+
expect(mockFocusInput).not.toHaveBeenCalled();
190186

191-
// Verify that focusInput was called (which should focus the gloss field)
192-
expect(focusInput).toHaveBeenCalled();
187+
// Cancel and verify that focusInput was called
188+
await userEvent.click(screen.getByText(new RegExp("cancel")));
189+
expect(mockFocusInput).toHaveBeenCalled();
193190
});
194191

195192
it("returns focus to gloss after confirming note", async () => {
196-
(focusInput as jest.Mock).mockClear();
197-
await renderNewEntry("", "", "");
198-
199-
// Click the note button to open the dialog
200-
const noteButton = screen.getByTestId(NewEntryId.ButtonNote);
201-
await userEvent.click(noteButton);
193+
await renderNewEntry();
194+
mockFocusInput.mockClear();
202195

203-
// Click confirm to close the dialog
204-
const confirmButton = screen.getByTestId("note-edit-confirm");
205-
await userEvent.click(confirmButton);
196+
// Click the note button to open the dialog and type a note
197+
await userEvent.click(screen.getByTestId(NewEntryId.ButtonNote));
198+
await userEvent.type(document.activeElement!, "note text");
199+
expect(mockFocusInput).not.toHaveBeenCalled();
206200

207-
// Verify that focusInput was called (which should focus the gloss field)
208-
expect(focusInput).toHaveBeenCalled();
201+
// Confirm and verify that focusInput was called
202+
await userEvent.click(screen.getByText(new RegExp("confirm")));
203+
expect(mockFocusInput).toHaveBeenCalled();
209204
});
210205
});

src/components/Dialogs/EditTextDialog.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export default function EditTextDialog(
8989

9090
return (
9191
<Dialog
92-
disableRestoreFocus // Disable automatic focus restoration to allow custom focus handling
9392
open={props.open}
9493
onClose={escapeClose}
9594
slotProps={{ transition: { onExited: props.onExited } }}

0 commit comments

Comments
 (0)