Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/multiselect-toggle-reopen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/fuselage': patch
---

fix(fuselage): `MultiSelect` dropdown reopening when its toggle is clicked while open
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { composeStories } from '@storybook/react-webpack5';
import { screen } from '@testing-library/react';
import { act, fireEvent, screen } from '@testing-library/react';
import { axe } from 'jest-axe';
import { withResizeObserverMock } from 'testing-utils/mocks/withResizeObserverMock';

Expand Down Expand Up @@ -32,6 +32,44 @@ test.each(testCases)(
},
);

test('stays closed when the toggle is clicked while open (regression for #1869)', () => {
// The visibility state is debounced (10ms). When the open dropdown is
// clicked, the anchor blurs first (`onBlur: hide`) and that debounced hide
// flushes before the trailing `click` fires. Previously `handleClick` read
// the already-HIDDEN `visible` state and re-opened the dropdown. Reproduce
// that exact ordering with controlled timers.
jest.useFakeTimers();

try {
const { Default } = composeStories(stories);
render(<Default />);

const combobox = screen.getByRole('combobox');
const container = combobox.closest('.rcx-select') as HTMLElement;

// Open the dropdown.
fireEvent.click(container);
act(() => {
jest.advanceTimersByTime(10);
});
expect(combobox).toHaveAttribute('aria-expanded', 'true');

// Clicking the toggle: blur closes it, then the click must keep it closed.
fireEvent.blur(combobox);
act(() => {
jest.advanceTimersByTime(10);
});
fireEvent.click(container);
act(() => {
jest.advanceTimersByTime(10);
});

expect(combobox).toHaveAttribute('aria-expanded', 'false');
} finally {
jest.useRealTimers();
}
});

test('MultiSelectFiltered prevents Chrome autocomplete overlay (regression)', () => {
const { WithFilter } = composeStories(stories);

Expand Down
9 changes: 8 additions & 1 deletion packages/fuselage/src/components/MultiSelect/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,16 @@ const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>(
};

const handleClick = useStableCallback(() => {
if (visible === AnimatedVisibility.VISIBLE) {
// Toggle based on the self-managed `focus-visible` class rather than the
// `visible` state. The anchor's `onBlur` fires `hide()` during the
// mousedown that precedes this click, so reading `visible` here would
// already see HIDDEN and re-open the dropdown. This mirrors SelectLegacy.
if (innerRef.current?.classList.contains('focus-visible')) {
removeFocusClass();
return hide();
}

innerRef.current?.classList.add('focus-visible');
innerRef.current?.focus();
return show();
});
Expand Down
Loading