Skip to content

Commit

Permalink
call onSearch if the value changes and the user is not searching (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdaniellewis authored Jun 19, 2024
1 parent fae7170 commit 9c2c3cc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- If the value is updated for a combo-box and the listbox is visible and there is no user entered search, onSearch will be triggered
- React 18.3 support
- Remove default props in favour of default parameters
- Fix warnings on spreading `key`
Expand Down
7 changes: 7 additions & 0 deletions src/components/combo_box.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ export const ComboBox = memo(
dispatch(onValueChanged());
});

// If the value changes and the list box is active and no search is set trigger onSearch with the new label
useEffect(() => {
if (search === null && showListBox) {
onSearch?.(value?.label);
}
}, [value?.identity]); // eslint-disable-line react-hooks/exhaustive-deps

// Do not show the list box is the only option is the currently selected option
const showListBox = useMemo(
() =>
Expand Down
62 changes: 62 additions & 0 deletions src/components/combo_box.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,47 @@ describe('value', () => {
expectToHaveFocusedOption(screen.getByRole('option', { name: 'Banana' }));
});

it('calls onSearch for an open list box with no search', async () => {
const onSearch = jest.fn();
const { rerender } = render(
<ComboBoxWrapper
options={options}
onSearch={onSearch}
/>,
);
await userEvent.tab();
await userEvent.keyboard('{ArrowDown}');
rerender(
<ComboBoxWrapper
options={options}
onSearch={onSearch}
value="Banana"
/>,
);
expect(onSearch).toHaveBeenCalledWith('Banana');
});

it('does not call onSearch for an open list box with a search', async () => {
const onSearch = jest.fn();
const { rerender } = render(
<ComboBoxWrapper
options={options}
onSearch={onSearch}
/>,
);
await userEvent.tab();
await userEvent.type(screen.getByRole('combobox'), 'foo');
onSearch.mockClear();
rerender(
<ComboBoxWrapper
options={options}
onSearch={onSearch}
value="Banana"
/>,
);
expect(onSearch).not.toHaveBeenCalled();
});

it('changes the value of a closed listbox', async () => {
const { rerender } = render(<ComboBoxWrapper options={options} />);
await userEvent.tab();
Expand All @@ -1593,6 +1634,27 @@ describe('value', () => {
await userEvent.keyboard('{ArrowDown}');
expectToHaveFocusedOption(screen.getByRole('option', { name: 'Banana' }));
});

it('does not call onSearch for an closed list box with no search', async () => {
const onSearch = jest.fn();
const { rerender } = render(
<ComboBoxWrapper
options={options}
onSearch={onSearch}
/>,
);
await userEvent.tab();
await userEvent.keyboard('{ArrowDown}{Enter}');
onSearch.mockClear();
rerender(
<ComboBoxWrapper
options={options}
onSearch={onSearch}
value="Banana"
/>,
);
expect(onSearch).not.toHaveBeenCalled();
});
});
});

Expand Down

0 comments on commit 9c2c3cc

Please sign in to comment.