From be829317aebd27f7d3d8ed50c0dfe4ce5b59a9d6 Mon Sep 17 00:00:00 2001 From: Vegard Haugstvedt Date: Wed, 3 Apr 2024 15:24:36 +0200 Subject: [PATCH 01/16] Prevent "Enter" key in Combobox to... --- @navikt/core/react/src/form/combobox/Input/Input.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/@navikt/core/react/src/form/combobox/Input/Input.tsx b/@navikt/core/react/src/form/combobox/Input/Input.tsx index b85dcd6754..2050eaffde 100644 --- a/@navikt/core/react/src/form/combobox/Input/Input.tsx +++ b/@navikt/core/react/src/form/combobox/Input/Input.tsx @@ -128,6 +128,8 @@ const Input = forwardRef( removeSelectedOption(lastSelectedOption); } } + } else if (e.key === "Enter" || e.key === "Accept") { + e.preventDefault(); } else if (e.key === "ArrowDown") { // Check that cursor position is at the end of the input field, // so we don't interfere with text editing From a28f0c9340807a850947729b5e21b37bc8ad9c3b Mon Sep 17 00:00:00 2001 From: Vegard Haugstvedt Date: Wed, 17 Apr 2024 09:20:49 +0200 Subject: [PATCH 02/16] Added test for Enter not submitting form --- .../src/form/combobox/combobox.stories.tsx | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/@navikt/core/react/src/form/combobox/combobox.stories.tsx b/@navikt/core/react/src/form/combobox/combobox.stories.tsx index 3e74897466..1b6845f42d 100644 --- a/@navikt/core/react/src/form/combobox/combobox.stories.tsx +++ b/@navikt/core/react/src/form/combobox/combobox.stories.tsx @@ -624,3 +624,46 @@ export const TestHoverAndFocusSwitching: StoryObject = { ); }, }; + +export const TestEnterNotSubmittingForm: StoryObj<{ + onSubmit: ReturnType; +}> = { + args: { + onSubmit: fn(), + }, + render: ({ onSubmit }) => { + return ( +
+ + + ); + }, + play: async ({ canvasElement, args }) => { + args.onSubmit.mockClear(); + const canvas = within(canvasElement); + + await sleep(500); + + const getInput = () => + canvas.getByRole("combobox", { + name: "Hva er dine favorittfrukter?", + }); + + userEvent.click(getInput()); + await sleep(250); + + userEvent.keyboard("{ArrowDown}"); + await sleep(250); + const bananaOption = canvas.getByRole("option", { name: "banana" }); + expect(getInput().getAttribute("aria-activedescendant")).toBe( + bananaOption.getAttribute("id"), + ); + + userEvent.keyboard("{Enter}"); + await sleep(250); + expect(args.onSubmit).not.toHaveBeenCalled(); + }, +}; From c6de1dd4043bf1b44389e57a835990502019764d Mon Sep 17 00:00:00 2001 From: Vegard Haugstvedt Date: Wed, 17 Apr 2024 09:25:08 +0200 Subject: [PATCH 03/16] Added changeset --- .changeset/rotten-windows-cover.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/rotten-windows-cover.md diff --git a/.changeset/rotten-windows-cover.md b/.changeset/rotten-windows-cover.md new file mode 100644 index 0000000000..9b4b6d7d97 --- /dev/null +++ b/.changeset/rotten-windows-cover.md @@ -0,0 +1,5 @@ +--- +"@navikt/ds-react": patch +--- + +Preventing "enter" while Combobox is focused from submitting form From d6c11baed7915fb5491e2c1f2eaa1e51ccf0bf41 Mon Sep 17 00:00:00 2001 From: Vegard Haugstvedt Date: Wed, 17 Apr 2024 09:53:28 +0200 Subject: [PATCH 04/16] Add test for un-selecting a selected option using "enter" on a Chip not submitting the form --- .../form/combobox/SelectedOptions/SelectedOptions.tsx | 8 +++++++- .../core/react/src/form/combobox/combobox.stories.tsx | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/@navikt/core/react/src/form/combobox/SelectedOptions/SelectedOptions.tsx b/@navikt/core/react/src/form/combobox/SelectedOptions/SelectedOptions.tsx index 7e3a7bcfcd..70ccba700e 100644 --- a/@navikt/core/react/src/form/combobox/SelectedOptions/SelectedOptions.tsx +++ b/@navikt/core/react/src/form/combobox/SelectedOptions/SelectedOptions.tsx @@ -1,5 +1,6 @@ import React from "react"; import { Chips } from "../../../chips"; +import { useFilteredOptionsContext } from "../FilteredOptions/filteredOptionsContext"; import { useInputContext } from "../Input/inputContext"; import { ComboboxOption } from "../types"; import { useSelectedOptionsContext } from "./selectedOptionsContext"; @@ -36,8 +37,13 @@ const SelectedOptions: React.FC = ({ size, children, }) => { + const { toggleIsListOpen } = useFilteredOptionsContext(); return ( - + toggleIsListOpen(false)} + size={size} + > {selectedOptions.length ? selectedOptions.map((option, i) => (