Skip to content

feat: support popupElement #452

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

Merged
merged 1 commit into from
Apr 10, 2024
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
7 changes: 7 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type {

export interface TriggerRef {
nativeElement: HTMLElement;
popupElement: HTMLElement;
forceAlign: VoidFunction;
}

Expand Down Expand Up @@ -239,7 +240,12 @@ export function generateTrigger(
const id = useId();
const [popupEle, setPopupEle] = React.useState<HTMLDivElement>(null);

// Used for forwardRef popup. Not use internal
const externalPopupRef = React.useRef<HTMLDivElement>(null);

const setPopupRef = useEvent((node: HTMLDivElement) => {
externalPopupRef.current = node;

if (isDOM(node) && popupEle !== node) {
setPopupEle(node);
}
Expand Down Expand Up @@ -462,6 +468,7 @@ export function generateTrigger(
// ============================ Refs ============================
React.useImperativeHandle(ref, () => ({
nativeElement: externalForwardRef.current,
popupElement: externalPopupRef.current,
forceAlign: triggerAlign,
}));

Expand Down
14 changes: 14 additions & 0 deletions tests/ref.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ describe('Trigger.Ref', () => {
container.querySelector('button'),
);
});

it('support popupElement', () => {
const triggerRef = React.createRef<TriggerRef>();

render(
<Trigger ref={triggerRef} popupVisible popup={<div />}>
<button />
</Trigger>,
);

expect(triggerRef.current.popupElement).toBe(
document.querySelector('.rc-trigger-popup'),
);
});
});