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
1 change: 1 addition & 0 deletions src/BaseSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ const BaseSelect = React.forwardRef<BaseSelectRef, BaseSelectProps>((props, ref)

// We should give focus back to selector if clicked item is not focusable
if (popupElement?.contains(target as HTMLElement) && triggerOpen) {
(event.nativeEvent as any)._ori_target = target;
// Tell `open` not to close since it's safe in the popup
triggerOpen(true);
}
Expand Down
50 changes: 50 additions & 0 deletions tests/ShadowTrigger.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { act, render, waitFor } from '@testing-library/react';
import React from 'react';
import Select from '../src';

// The default trigger mock renders popups inline and does not exercise portals.
jest.mock('@rc-component/trigger', () => jest.requireActual('@rc-component/trigger').default);

it('keeps the popup open inside a separate closed shadow root and closes outside', async () => {
const host = document.createElement('div');
const popupHost = document.createElement('div');
document.body.append(host, popupHost);
const root = host.attachShadow({ mode: 'closed' });
const popupRoot = popupHost.attachShadow({ mode: 'closed' });
const mount = document.createElement('div');
const popupMount = document.createElement('div');
const outside = document.createElement('div');
root.appendChild(mount);
popupRoot.append(popupMount, outside);
const onPopupVisibleChange = jest.fn();
const { unmount } = render(
<Select
open
onPopupVisibleChange={onPopupVisibleChange}
options={[{ value: 'a' }]}
getPopupContainer={() => popupMount}
popupRender={() => <div data-testid="popup-content">Content</div>}
/>,
{ container: mount },
);

await waitFor(() =>
expect(popupRoot.querySelector('[data-testid="popup-content"]')).not.toBeNull(),
);
const popupContent = popupRoot.querySelector('[data-testid="popup-content"]');
act(() =>
popupContent.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, composed: true })),
);
await act(() => new Promise((resolve) => setTimeout(resolve, 30)));
expect(onPopupVisibleChange).not.toHaveBeenCalledWith(false);
act(() => outside.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, composed: true })));
await waitFor(() => expect(onPopupVisibleChange).toHaveBeenCalledWith(false));
onPopupVisibleChange.mockClear();
act(() =>
document.body.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, composed: true })),
);
await waitFor(() => expect(onPopupVisibleChange).toHaveBeenCalledWith(false));
unmount();
host.remove();
popupHost.remove();
});