Skip to content

Commit

Permalink
feat: support popupElement (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Apr 10, 2024
1 parent 50ae862 commit 4e1d80f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
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'),
);
});
});

0 comments on commit 4e1d80f

Please sign in to comment.