Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Clear autocomplete input on selection accept
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkr committed Jun 27, 2024
1 parent b449dc8 commit 3ee4ecd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/structures/AutocompleteInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export const AutocompleteInput: React.FC<AutocompleteInputProps> = ({

onSelectionChange(newSelection);
focusEditor();
setQuery("");
setSuggestions([]);
};

const removeSelection = (completion: ICompletion): void => {
Expand Down
30 changes: 30 additions & 0 deletions test/components/structures/AutocompleteInput-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,36 @@ describe("AutocompleteInput", () => {
expect(onSelectionChangeMock).toHaveBeenCalledWith([mockCompletion[0]]);
});

it("should clear text field and suggestions when a suggestion is accepted", async () => {
const mockProvider = constructMockProvider(mockCompletion);
const onSelectionChangeMock = jest.fn();

const { container } = render(
<AutocompleteInput
provider={mockProvider}
placeholder="Search ..."
selection={[]}
onSelectionChange={onSelectionChangeMock}
/>,
);

const input = getEditorInput();

act(() => {
fireEvent.focus(input);
fireEvent.change(input, { target: { value: "user" } });
});

const suggestions = await within(container).findAllByTestId("autocomplete-suggestion-item", { exact: false });

act(() => {
fireEvent.mouseDown(suggestions[0]);
});

expect(input).toHaveValue("");
expect(within(container).queryAllByTestId("autocomplete-suggestion-item", { exact: false })).toHaveLength(0);
});

afterAll(() => {
jest.clearAllMocks();
jest.resetModules();
Expand Down

0 comments on commit 3ee4ecd

Please sign in to comment.