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

fix(autocomplete): controlled state logic #2969

Merged
merged 4 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/proud-moles-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/autocomplete": patch
---

Fix autocomplete controlled state (#2955)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {render, renderHook, act} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {useForm} from "react-hook-form";

import {Autocomplete, AutocompleteItem, AutocompleteSection} from "../src";
import {Autocomplete, AutocompleteItem, AutocompleteProps, AutocompleteSection} from "../src";
import {Modal, ModalContent, ModalBody, ModalHeader, ModalFooter} from "../../modal/src";

type Item = {
Expand Down Expand Up @@ -48,6 +48,20 @@ const itemsSectionData = [
},
];

const ControlledAutocomplete = <T = object,>(props: AutocompleteProps<T>) => {
jrgarciadev marked this conversation as resolved.
Show resolved Hide resolved
const [selectedKey, setSelectedKey] = React.useState<React.Key>("cat");

return (
<Autocomplete
{...props}
aria-label="Favorite Animal"
label="Favorite Animal"
selectedKey={selectedKey}
onSelectionChange={setSelectedKey}
/>
);
};

describe("Autocomplete", () => {
it("should render correctly", () => {
const wrapper = render(
Expand Down Expand Up @@ -220,6 +234,60 @@ describe("Autocomplete", () => {
// assert that the autocomplete dropdown is closed
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
});

it("should work when key equals textValue", async () => {
const wrapper = render(
<Autocomplete
aria-label="Favorite Animal"
data-testid="when-key-equals-textValue"
defaultSelectedKey="cat"
items={itemsData}
label="Favorite Animal"
>
{(item) => <AutocompleteItem key={item.value}>{item.value}</AutocompleteItem>}
</Autocomplete>,
);

const autocomplete = wrapper.getByTestId("when-key-equals-textValue");

const user = userEvent.setup();

await user.click(autocomplete);

expect(autocomplete).toHaveValue("cat");
expect(autocomplete).toHaveAttribute("aria-expanded", "true");

let listboxItems = wrapper.getAllByRole("option");

await user.click(listboxItems[1]);

expect(autocomplete).toHaveValue("dog");
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
});

it("should work when key equals textValue (controlled)", async () => {
const wrapper = render(
<ControlledAutocomplete data-testid="when-key-equals-textValue" items={itemsData}>
{(item) => <AutocompleteItem key={item.value}>{item.value}</AutocompleteItem>}
</ControlledAutocomplete>,
);

const autocomplete = wrapper.getByTestId("when-key-equals-textValue");

const user = userEvent.setup();

await user.click(autocomplete);

expect(autocomplete).toHaveValue("cat");
expect(autocomplete).toHaveAttribute("aria-expanded", "true");

let listboxItems = wrapper.getAllByRole("option");

await user.click(listboxItems[1]);

expect(autocomplete).toHaveValue("dog");
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
});
});

describe("Autocomplete with React Hook Form", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/autocomplete/src/use-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
const key = inputRef.current.value;
const item = state.collection.getItem(key);

if (item) {
if (item && state.inputValue !== item.textValue) {
state.setSelectedKey(key);
state.setInputValue(item.textValue);
}
Expand Down
Loading