From 13efaeea886ae62b03dc5378bd386a4d849b4863 Mon Sep 17 00:00:00 2001 From: Bram Kaashoek Date: Mon, 17 Jun 2024 09:55:13 +0200 Subject: [PATCH] fix: empty suggestions list after selecting --- .changeset/three-clouds-rush.md | 5 +++++ src/index.test.tsx | 13 +++++++++---- src/index.tsx | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 .changeset/three-clouds-rush.md diff --git a/.changeset/three-clouds-rush.md b/.changeset/three-clouds-rush.md new file mode 100644 index 0000000..10597f0 --- /dev/null +++ b/.changeset/three-clouds-rush.md @@ -0,0 +1,5 @@ +--- +"react-loqate": patch +--- + +fix: empty the suggestions list when a selection is made diff --git a/src/index.test.tsx b/src/index.test.tsx index 3739391..90b94f4 100644 --- a/src/index.test.tsx +++ b/src/index.test.tsx @@ -29,7 +29,7 @@ it('renders default', async () => { expect(baseElement).toMatchSnapshot(); }); -it('calls the onSelect function', async () => { +it('calls the onSelect function and closes the suggestion list after selecting an item', async () => { const onSelectFn = vi.fn(); render( @@ -44,14 +44,19 @@ it('calls the onSelect function', async () => { const input = screen.getByRole('textbox'); fireEvent.change(input, { target: { value: 'a' } }); - const listItems = await screen.findAllByRole('listitem'); - const firstListItem = listItems[0]; - fireEvent.click(firstListItem); + const list = await screen.findByRole('list'); + expect(list.childNodes).toHaveLength(10); + expect(list.hidden).toBe(false); + + fireEvent.click(list.firstChild as Element); await waitFor(() => { expect(onSelectFn.mock.calls.length).toBe(1); expect(onSelectFn).toHaveBeenCalledWith(selection.Items[0]); }); + + expect(list.childNodes).toHaveLength(0); + expect(list.hidden).toBe(true); }); it('allows for alternative url', async () => { diff --git a/src/index.tsx b/src/index.tsx index 0d1ff6a..76ce821 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -180,6 +180,7 @@ function AddressSearch(props: Props): JSX.Element { } onSelect(Items[0] as unknown as Address); + setSuggestions([]); return; }