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

Combobox: Pressing Escape in while focusing a Combobox inside a Modal will close the Modal #3053

5 changes: 5 additions & 0 deletions .changeset/silly-walls-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Prevent Escape in open Combobox from closing Modals
17 changes: 10 additions & 7 deletions @navikt/core/react/src/form/combobox/Input/Input.tsx
it-vegard marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ const Input = forwardRef<HTMLInputElement, InputProps>(
const handleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {
e.preventDefault();
switch (e.key) {
case "Escape":
clearInput(e);
toggleIsListOpen(false);
break;
case "Enter":
case "Accept":
onEnter(e);
Expand Down Expand Up @@ -143,6 +139,12 @@ const Input = forwardRef<HTMLInputElement, InputProps>(
if (activeDecendantId || value) {
e.preventDefault();
}
} else if (e.key === "Escape") {
if (isListOpen || value) {
e.preventDefault(); // Prevents closing an encasing Modal, as Combobox reacts on keyup.
clearInput(e);
HalvorHaugan marked this conversation as resolved.
Show resolved Hide resolved
toggleIsListOpen(false);
}
} else if (["ArrowLeft", "ArrowRight"].includes(e.key)) {
/**
* In case user has an active selection and 'completes' the selection with ArrowLeft or ArrowRight
Expand Down Expand Up @@ -177,17 +179,18 @@ const Input = forwardRef<HTMLInputElement, InputProps>(
}
},
[
setIsMouseLastUsedInputDevice,
value,
selectedOptions,
removeSelectedOption,
isListOpen,
activeDecendantId,
setIsMouseLastUsedInputDevice,
clearInput,
toggleIsListOpen,
onChange,
virtualFocus,
isListOpen,
setValue,
searchTerm,
toggleIsListOpen,
],
);

Expand Down
36 changes: 35 additions & 1 deletion @navikt/core/react/src/form/combobox/combobox.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Meta, StoryFn } from "@storybook/react";
import React, { useMemo, useRef, useState } from "react";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { Button } from "../../button";
import { Chips } from "../../chips";
import { VStack } from "../../layout/stack";
import { Modal } from "../../modal";
import { TextField } from "../textfield";
import { UNSAFE_Combobox } from "./index";

Expand Down Expand Up @@ -409,6 +411,38 @@ export const WithError: StoryFn = () => {
);
};

export const InModal: StoryFn = () => {
const modalRef = useRef<HTMLDialogElement>(null);

useEffect(() => {
modalRef.current?.showModal();
}, []);

return (
<>
<Button onClick={() => modalRef.current?.showModal()}>Åpne modal</Button>
<Modal
ref={modalRef}
header={{ heading: "Overskrift" }}
width="medium"
style={{ height: "auto" }}
>
<Modal.Body style={{ height: "100% " }}>
<p>
Modalen skal ikke lukke seg om man trykker Escape mens virtuelt
fokus er i Combobox sin nedtrekksliste eller om inputfeltet
inneholder tekst.
</p>
<UNSAFE_Combobox
options={options}
label="Hva er dine favorittfrukter?"
/>
</Modal.Body>
</Modal>
</>
);
};

export const Disabled: StoryFn = () => {
return (
<UNSAFE_Combobox
Expand Down
Loading