Skip to content

Commit 959e52d

Browse files
authored
Merge pull request #40 from labd/emptySuggestions
fix: empty suggestions list after selecting
2 parents 99e8322 + 13efaee commit 959e52d

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

.changeset/three-clouds-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-loqate": patch
3+
---
4+
5+
fix: empty the suggestions list when a selection is made

src/index.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ it('renders default', async () => {
2929
expect(baseElement).toMatchSnapshot();
3030
});
3131

32-
it('calls the onSelect function', async () => {
32+
it('calls the onSelect function and closes the suggestion list after selecting an item', async () => {
3333
const onSelectFn = vi.fn();
3434

3535
render(
@@ -44,14 +44,19 @@ it('calls the onSelect function', async () => {
4444
const input = screen.getByRole('textbox');
4545
fireEvent.change(input, { target: { value: 'a' } });
4646

47-
const listItems = await screen.findAllByRole('listitem');
48-
const firstListItem = listItems[0];
49-
fireEvent.click(firstListItem);
47+
const list = await screen.findByRole('list');
48+
expect(list.childNodes).toHaveLength(10);
49+
expect(list.hidden).toBe(false);
50+
51+
fireEvent.click(list.firstChild as Element);
5052

5153
await waitFor(() => {
5254
expect(onSelectFn.mock.calls.length).toBe(1);
5355
expect(onSelectFn).toHaveBeenCalledWith(selection.Items[0]);
5456
});
57+
58+
expect(list.childNodes).toHaveLength(0);
59+
expect(list.hidden).toBe(true);
5560
});
5661

5762
it('allows for alternative url', async () => {

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ function AddressSearch(props: Props): JSX.Element {
180180
}
181181

182182
onSelect(Items[0] as unknown as Address);
183+
setSuggestions([]);
183184
return;
184185
}
185186

0 commit comments

Comments
 (0)