Skip to content

Commit

Permalink
Bugfix/combobox selecting autocompleted word is broken (#3110)
Browse files Browse the repository at this point in the history
* Add test case for selecting autocompleted word after first character typed, which is currently broken (selects first in list, not the autocompleted word)

* Select the correct option when pressing enter after autocompleting a word in Combobox

Fixes #3106

* Added changeset
  • Loading branch information
it-vegard authored Aug 28, 2024
1 parent 6220b9c commit aca5143
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-crabs-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---

Fixed bug in Combobox where the wrong option was selected on Enter if the autocompleted word was not the first in FilteredOptions
5 changes: 4 additions & 1 deletion @navikt/core/react/src/form/combobox/Input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ const Input = forwardRef<HTMLInputElement, InputProps>(
const selectedValue =
allowNewValues && isValueNew
? { label: value, value }
: filteredOptions[0];
: filteredOptionsUtil.getFirstValueStartingWith(
value,
filteredOptions,
) || filteredOptions[0];

if (!selectedValue) {
return;
Expand Down
56 changes: 43 additions & 13 deletions @navikt/core/react/src/form/combobox/__tests__/combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,32 @@ import { act, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React, { useId } from "react";
import { describe, expect, test, vi } from "vitest";
import { UNSAFE_Combobox } from "../index";
import { ComboboxProps, UNSAFE_Combobox } from "../index";

const options = [
"banana",
"apple",
"apple pie",
"tangerine",
"pear",
"banana",
"grape",
"grape fruit",
"kiwi",
"mango",
"passion fruit",
"pear",
"pineapple",
"strawberry",
"tangerine",
"watermelon",
"grape fruit",
];

const App = (props) => {
const App = ({
label = "Hva er dine favorittfrukter?",
...rest
}: Omit<ComboboxProps, "label"> & { label?: ComboboxProps["label"] }) => {
const id = useId();
return (
<div data-theme="light">
<UNSAFE_Combobox
label="Hva er dine favorittfrukter?"
size="medium"
variant="simple"
id={id}
{...props}
/>
<UNSAFE_Combobox label={label} size="medium" id={id} {...rest} />
</div>
);
};
Expand Down Expand Up @@ -260,5 +257,38 @@ describe("Render combobox", () => {
).toBe(true);
});
});

test("and pressing enter to select autocompleted word will select the correct word", async () => {
const onToggleSelected = vi.fn();
render(
<App
onToggleSelected={onToggleSelected}
options={options}
shouldAutocomplete
/>,
);

const combobox = screen.getByRole("combobox", {
name: "Hva er dine favorittfrukter?",
});

await act(async () => {
await userEvent.click(combobox);

await userEvent.type(combobox, "p");
});

expect(combobox.getAttribute("value")).toBe("passion fruit");

await act(async () => {
await userEvent.keyboard("{Enter}");
});

expect(onToggleSelected).toHaveBeenCalledWith(
"passion fruit",
true,
false,
);
});
});
});

0 comments on commit aca5143

Please sign in to comment.