Skip to content

Commit 9ef63cd

Browse files
authored
Merge pull request #1935 from dxc-technology/Mil4n0r/dialog_interaction_esc
Dialog popups interaction (Escape) fix
2 parents 9f6fced + 621cc05 commit 9ef63cd

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

lib/src/date-input/DateInput.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ const DxcDateInput = React.forwardRef<RefType, DateInputPropsType>(
143143
setIsOpen(false);
144144
};
145145

146-
const handleDatePickerEscKeydown = (event) => {
147-
event.preventDefault();
148-
closeCalendar();
149-
dateRef?.current.getElementsByTagName("input")[0].focus();
146+
const handleDatePickerEscKeydown = (event: React.KeyboardEvent) => {
147+
if (event.key === "Escape") {
148+
event.preventDefault();
149+
isOpen && event.stopPropagation();
150+
closeCalendar();
151+
dateRef?.current.getElementsByTagName("input")[0].focus();
152+
}
150153
};
151154
const handleDatePickerOnBlur = (event) => {
152155
if (!event?.currentTarget.contains(event.relatedTarget)) closeCalendar();
@@ -156,7 +159,7 @@ const DxcDateInput = React.forwardRef<RefType, DateInputPropsType>(
156159
<ThemeProvider theme={colorsTheme}>
157160
<div ref={ref}>
158161
<Popover.Root open={isOpen}>
159-
<Popover.Trigger asChild aria-controls={undefined} >
162+
<Popover.Trigger asChild aria-controls={undefined}>
160163
<DxcTextInput
161164
label={label}
162165
name={name}
@@ -189,7 +192,7 @@ const DxcDateInput = React.forwardRef<RefType, DateInputPropsType>(
189192
align="end"
190193
aria-modal={true}
191194
onBlur={handleDatePickerOnBlur}
192-
onEscapeKeyDown={handleDatePickerEscKeydown}
195+
onKeyDown={handleDatePickerEscKeydown}
193196
avoidCollisions={false}
194197
>
195198
<DxcDatePicker id={calendarId} onDateSelect={handleCalendarOnClick} date={dayjsDate} />

lib/src/dialog/Dialog.stories.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import DxcHeading from "../heading/Heading";
1111
import DxcParagraph from "../paragraph/Paragraph";
1212
import DxcAlert from "../alert/Alert";
1313
import { userEvent, within } from "@storybook/testing-library";
14-
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
14+
import { INITIAL_VIEWPORTS } from "@storybook/addon-viewport";
1515

1616
export default {
1717
title: "Dialog",
@@ -368,4 +368,3 @@ ScrollDialog.play = async ({ canvasElement }) => {
368368
await userEvent.tab();
369369
await userEvent.tab();
370370
};
371-

lib/src/dialog/Dialog.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import DxcCard from "../card/Card.tsx";
1212
import DxcRadioGroup from "../radio-group/RadioGroup.tsx";
1313
import DxcSlider from "../slider/Slider.tsx";
1414
import DxcSwitch from "../switch/Switch.tsx";
15+
import DxcDateInput from "../date-input/DateInput.tsx";
16+
17+
// Mocking DOMRect for Radix Primitive Popover
18+
global.globalThis = global;
19+
global.DOMRect = {
20+
fromRect: () => ({ top: 0, left: 0, bottom: 0, right: 0, width: 0, height: 0 }),
21+
};
22+
global.ResizeObserver = class ResizeObserver {
23+
observe() {}
24+
unobserve() {}
25+
disconnect() {}
26+
};
1527

1628
const options = [
1729
{ label: "Female", value: "female" },
@@ -57,6 +69,19 @@ describe("Dialog component tests", () => {
5769
fireEvent.keyDown(getByRole("dialog"), { key: "Escape", code: "Escape", keyCode: 27, charCode: 27 });
5870
expect(onCloseClick).toHaveBeenCalled();
5971
});
72+
73+
test("Does not call function onCloseClick when 'Escape' key is pressed while a child popover is opened", async () => {
74+
const onCloseClick = jest.fn();
75+
const { getByRole } = render(
76+
<DxcDialog onCloseClick={onCloseClick}>
77+
<DxcDateInput label="With format M-dd-yyyy" format="M-dd-yyyy" />
78+
</DxcDialog>
79+
);
80+
const calendarAction = getByRole("combobox");
81+
await userEvent.click(calendarAction);
82+
fireEvent.keyDown(document.activeElement, { key: "Escape", code: "Escape", keyCode: 27, charCode: 27 });
83+
expect(onCloseClick).not.toHaveBeenCalled();
84+
});
6085
});
6186

6287
describe("Dialog component: Focus lock tests", () => {

lib/src/dropdown/Dropdown.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ const DxcDropdown = ({
127127
case "Esc":
128128
case "Escape":
129129
event.preventDefault();
130+
isOpen && event.stopPropagation();
130131
handleOnCloseMenu();
131132
triggerRef.current?.focus();
132133
break;
@@ -186,7 +187,11 @@ const DxcDropdown = ({
186187
<DropdownTriggerContent>
187188
{label && iconPosition === "after" && <DropdownTriggerLabel>{label}</DropdownTriggerLabel>}
188189
{icon && (
189-
<DropdownTriggerIcon disabled={disabled} role={typeof icon === "string" ? undefined : "img"} aria-hidden>
190+
<DropdownTriggerIcon
191+
disabled={disabled}
192+
role={typeof icon === "string" ? undefined : "img"}
193+
aria-hidden
194+
>
190195
{typeof icon === "string" ? <DxcIcon icon={icon} /> : icon}
191196
</DropdownTriggerIcon>
192197
)}

lib/src/select/Select.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
168168
): JSX.Element => {
169169
const selectId = `select-${useId()}`;
170170
const selectLabelId = `label-${selectId}`;
171-
const searchableInputId = `searchable-input-${selectId}`
171+
const searchableInputId = `searchable-input-${selectId}`;
172172
const errorId = `error-${selectId}`;
173173
const optionsListId = `${selectId}-listbox`;
174174
const [innerValue, setInnerValue] = useState(defaultValue ?? (multiple ? [] : ""));
@@ -282,6 +282,7 @@ const DxcSelect = React.forwardRef<RefType, SelectPropsType>(
282282
case "Esc":
283283
case "Escape":
284284
event.preventDefault();
285+
isOpen && event.stopPropagation();
285286
closeOptions();
286287
setSearchValue("");
287288
break;

lib/src/text-input/TextInput.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ const DxcTextInput = React.forwardRef<RefType, TextInputPropsType>(
269269
case "Esc":
270270
case "Escape":
271271
event.preventDefault();
272+
isOpen && event.stopPropagation();
272273
if (hasSuggestions(suggestions)) {
273274
changeValue("");
274275
isOpen && closeSuggestions();

0 commit comments

Comments
 (0)