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: Prevent dropdowns from closing when elements inside self-dismiss #541

Merged
merged 1 commit into from
Dec 1, 2022
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
30 changes: 28 additions & 2 deletions src/internal/components/dropdown/__tests__/dropdown.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import { act, render, fireEvent } from '@testing-library/react';
import React, { useState } from 'react';
import { act, screen, render, fireEvent } from '@testing-library/react';
import Dropdown from '../../../../../lib/components/internal/components/dropdown';
import { calculatePosition } from '../../../../../lib/components/internal/components/dropdown/dropdown-fit-handler';
import DropdownWrapper from '../../../../../lib/components/test-utils/dom/internal/dropdown';
Expand Down Expand Up @@ -61,6 +61,32 @@ describe('Dropdown Component', () => {
act(() => outsideElement.click());
expect(handleCloseDropdown).toBeCalled();
});

test('does not fire close event when a self-destructible element inside dropdown was clicked', async () => {
function SelfDestructible() {
const [visible, setVisible] = useState(true);
return visible ? (
<button data-testid="dismiss" onClick={() => setVisible(false)}>
Dismiss
</button>
) : (
<span data-testid="after-dismiss">Gone!</span>
);
}
const handleCloseDropdown = jest.fn();
const [wrapper] = renderDropdown(
<Dropdown trigger={<button />} onDropdownClose={handleCloseDropdown} open={true}>
<SelfDestructible />
</Dropdown>
);
await runPendingEvents();

// NB: this should NOT be wrapped into act or React re-render will happen too late to reproduce the issue
wrapper.find('[data-testid="dismiss"]')!.click();

expect(handleCloseDropdown).not.toBeCalled();
expect(screen.getByTestId('after-dismiss')).toBeTruthy();
});
});
describe('dropdown recalculate position on scroll', () => {
beforeEach(() => {
Expand Down
12 changes: 2 additions & 10 deletions src/internal/components/dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,10 @@ const Dropdown = ({
fireNonCancelableEvent(onDropdownClose);
}
};

/*
* This small delay allows the event that opened the dropdown to
* finish bubbling, so that it is not immediately captured here.
*/
const timeout = setTimeout(() => {
window.addEventListener('click', clickListener);
}, 0);
window.addEventListener('click', clickListener, true);

return () => {
clearTimeout(timeout);
window.removeEventListener('click', clickListener);
window.removeEventListener('click', clickListener, true);
};
}, [open, onDropdownClose]);

Expand Down