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(FilterableMultiSelect): call onMenuChange when mouse click outside #17136

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,12 @@ const FilterableMultiSelect = React.forwardRef(function FilterableMultiSelect<
const nextIsOpen = forceIsOpen ?? !isOpen;
setIsOpen(nextIsOpen);
validateHighlightFocus();
if (onMenuChange) {
onMenuChange(nextIsOpen);
}
}

useEffect(() => {
onMenuChange?.(isOpen);
}, [isOpen, onMenuChange]);

const {
getToggleButtonProps,
getLabelProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ Filterable.argTypes = {
onChange: {
action: 'onChange',
},
onMenuChange: {
action: 'onMenuChange',
},
};

export const WithLayerMultiSelect = () => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,36 @@ describe('FilterableMultiSelect', () => {
expect(screen.getAllByRole('option').length).toBe(mockProps.items.length);
});

it('should call `onMenuChange` when the user clicks on the combobox', async () => {
render(<FilterableMultiSelect {...mockProps} />);
await waitForPosition();

await userEvent.click(screen.getByRole('combobox'));
expect(mockProps.onMenuChange).toHaveBeenCalledWith(true);
});

it('should call `onMenuChange` when the user clicks on the screen', async () => {
render(<FilterableMultiSelect {...mockProps} open />);
await waitForPosition();

await userEvent.click(document.body);
expect(mockProps.onMenuChange).toHaveBeenCalledWith(false);
});

it('should initially have the menu open when open prop is provided', async () => {
render(<FilterableMultiSelect {...mockProps} open />);
await waitForPosition();

assertMenuOpen(mockProps);
});

it('should call `onMenuChange` when open prop is provided', async () => {
render(<FilterableMultiSelect {...mockProps} open />);
await waitForPosition();

expect(mockProps.onMenuChange).toHaveBeenCalledWith(true);
});

it('should open the menu with a down arrow', async () => {
render(<FilterableMultiSelect {...mockProps} />);
await waitForPosition();
Expand All @@ -64,6 +87,15 @@ describe('FilterableMultiSelect', () => {
expect(screen.getAllByRole('option').length).toBe(mockProps.items.length);
});

it('should call `onMenuChange` when the user types a down arrow', async () => {
render(<FilterableMultiSelect {...mockProps} />);
await waitForPosition();

const menuIconNode = findMenuIconNode();
await userEvent.type(menuIconNode, '{arrowdown}');
expect(mockProps.onMenuChange).toHaveBeenCalledWith(true);
});

it('should let the user toggle the menu by the menu icon', async () => {
render(<FilterableMultiSelect {...mockProps} />);
await waitForPosition();
Expand All @@ -76,6 +108,17 @@ describe('FilterableMultiSelect', () => {
assertMenuClosed();
});

it('should call `onMenuChange` when the user toggles the menu by the menu icon', async () => {
render(<FilterableMultiSelect {...mockProps} />);
await waitForPosition();

await userEvent.click(findMenuIconNode());
expect(mockProps.onMenuChange).toHaveBeenCalledWith(true);

await userEvent.click(findMenuIconNode());
expect(mockProps.onMenuChange).toHaveBeenCalledWith(false);
});

it('should not close the menu after a user makes a selection', async () => {
render(<FilterableMultiSelect {...mockProps} />);
await waitForPosition();
Expand Down
Loading