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

[NU-1661] Remove focus from input in a dialog on esc keyboard button click #7106

Merged
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
11 changes: 11 additions & 0 deletions designer/client/cypress/e2e/nodeWindow.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,15 @@ describe("Node window", () => {
.click();
cy.get("[data-testid=window]").its("length").should("eq", 0);
});

it("should remove input focus and close the window on double escape click", () => {
cy.intercept("POST", "/api/nodes/*/validation").as("inputValidation");
cy.visitNewProcess(NAME, "testProcess");
cy.getNode("enricher").dblclick();
cy.wait("@inputValidation");
cy.get("[data-testid=window]").should("be.visible");
cy.get("body").type("{esc}");
cy.get("body").type("{esc}");
cy.get("[data-testid=window]").should("not.exist");
});
});
2 changes: 0 additions & 2 deletions designer/client/src/components/FormElements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,3 @@ export function Button({ className, onClick, ...props }: ButtonProps): JSX.Eleme
/>
);
}

export const SelectNode = styled("select")(nodeInputCss);
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from "react";
import TestResultUtils, { NodeTestResults, StateForSelectTestResults } from "../../../../common/TestResultUtils";
import { SelectNode } from "../../../FormElements";
import { css, cx } from "@emotion/css";
import { Box, FormControl, FormLabel, useTheme } from "@mui/material";
import { FormControl, FormLabel, useTheme } from "@mui/material";
import { Option, TypeSelect } from "../fragment-input-definition/TypeSelect";

export interface TestResultsSelectProps {
Expand Down
29 changes: 27 additions & 2 deletions designer/client/src/windowManager/useWindows.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useWindowManager, WindowId, WindowType } from "@touk/window-manager";
import { defaults } from "lodash";
import { useCallback, useMemo } from "react";
import { useCallback, useEffect, useMemo } from "react";
import { useUserSettings } from "../common/userSettings";
import { ConfirmDialogData } from "../components/modals/GenericConfirmDialog";
import { InfoDialogData } from "../components/modals/GenericInfoDialog";
Expand Down Expand Up @@ -28,8 +28,33 @@ function mapModeToKind(mode: NodeViewMode): WindowKind {
return WindowKind.editNode;
}

const useRemoveFocusOnEscKey = (isWindowOpen: boolean) => {
useEffect(() => {
if (!isWindowOpen) {
return;
}

const handleKeyDown = (event: KeyboardEvent) => {
const activeElement = document.activeElement as HTMLElement;
const tagName = activeElement.tagName.toLowerCase();
const allowedTagNames = ["input", "textarea", "select"];

if (event.key === "Escape" && allowedTagNames.includes(tagName)) {
activeElement.blur(); // Removes focus from the current active element
}
};

document.addEventListener("keydown", handleKeyDown);

return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, [isWindowOpen]);
};

export function useWindows(parent?: WindowId) {
const { open: _open, closeAll } = useWindowManager(parent);
const { open: _open, closeAll, windows } = useWindowManager(parent);
useRemoveFocusOnEscKey(windows.length > 0);
const [settings] = useUserSettings();
const forceDisableModals = useMemo(() => settings["debug.forceDisableModals"], [settings]);

Expand Down
Loading