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: update FilterableMultiSelect onMenuChange behavior #18606

Merged
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
@@ -1,5 +1,5 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -358,6 +358,7 @@ const FilterableMultiSelect = React.forwardRef(function FilterableMultiSelect<
ref: ForwardedRef<HTMLDivElement>
) {
const { isFluid } = useContext(FormContext);
const isFirstRender = useRef(true);
const [isFocused, setIsFocused] = useState<boolean>(false);
const [isOpen, setIsOpen] = useState<boolean>(!!open);
const [prevOpen, setPrevOpen] = useState<boolean>(!!open);
Expand Down Expand Up @@ -524,8 +525,16 @@ const FilterableMultiSelect = React.forwardRef(function FilterableMultiSelect<
}

useEffect(() => {
onMenuChange?.(isOpen);
}, [isOpen, onMenuChange]);
if (isFirstRender.current) {
isFirstRender.current = false;

if (open) {
onMenuChange?.(isOpen);
}
} else {
onMenuChange?.(isOpen);
}
}, [isOpen, onMenuChange, open]);

const {
getToggleButtonProps,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -536,7 +536,7 @@ describe('FilterableMultiSelect', () => {
item ? (
<span className="test-element" data-testid="custom-id-item">
{item.text}{' '}
<span role="img" alt="fire">
<span role="img" aria-label="fire">
{' '}
🔥
</span>
Expand Down Expand Up @@ -922,4 +922,53 @@ describe('FilterableMultiSelect', () => {
`${prefix}--multi-select--filterable--input-focused`
);
});

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

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

it('should not re-trigger `onMenuChange` on re-render if `open` prop remains unchanged', async () => {
const { rerender } = render(<FilterableMultiSelect {...mockProps} open />);
await waitForPosition();

// Initially called once on mount.
expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1);
expect(mockProps.onMenuChange).toHaveBeenCalledWith(true);

// Rerender with the same open prop value.
rerender(<FilterableMultiSelect {...mockProps} open />);
await waitForPosition();

// The callback should not be called again.
expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1);
});

it('should not call `onMenuChange` on mount when uncontrolled', async () => {
render(<FilterableMultiSelect {...mockProps} />);
await waitForPosition();

// Since uncontrolled mode only fires on interactions, expect no call on
// mount.
expect(mockProps.onMenuChange).not.toHaveBeenCalled();
});

it('should call `onMenuChange` when user interactions trigger state changes', async () => {
render(<FilterableMultiSelect {...mockProps} />);
await waitForPosition();

// Open the menu by clicking the combobox.
await userEvent.click(screen.getByRole('combobox'));
expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1);
expect(mockProps.onMenuChange).toHaveBeenCalledWith(true);
mockProps.onMenuChange.mockClear();

// Close the menu by clicking outside of it.
await userEvent.click(document.body);
expect(mockProps.onMenuChange).toHaveBeenCalledTimes(1);
expect(mockProps.onMenuChange).toHaveBeenCalledWith(false);
});
});
Loading