Skip to content

Commit 4e1d80f

Browse files
authored
feat: support popupElement (#452)
1 parent 50ae862 commit 4e1d80f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type {
3737

3838
export interface TriggerRef {
3939
nativeElement: HTMLElement;
40+
popupElement: HTMLElement;
4041
forceAlign: VoidFunction;
4142
}
4243

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

243+
// Used for forwardRef popup. Not use internal
244+
const externalPopupRef = React.useRef<HTMLDivElement>(null);
245+
242246
const setPopupRef = useEvent((node: HTMLDivElement) => {
247+
externalPopupRef.current = node;
248+
243249
if (isDOM(node) && popupEle !== node) {
244250
setPopupEle(node);
245251
}
@@ -462,6 +468,7 @@ export function generateTrigger(
462468
// ============================ Refs ============================
463469
React.useImperativeHandle(ref, () => ({
464470
nativeElement: externalForwardRef.current,
471+
popupElement: externalPopupRef.current,
465472
forceAlign: triggerAlign,
466473
}));
467474

tests/ref.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,18 @@ describe('Trigger.Ref', () => {
3636
container.querySelector('button'),
3737
);
3838
});
39+
40+
it('support popupElement', () => {
41+
const triggerRef = React.createRef<TriggerRef>();
42+
43+
render(
44+
<Trigger ref={triggerRef} popupVisible popup={<div />}>
45+
<button />
46+
</Trigger>,
47+
);
48+
49+
expect(triggerRef.current.popupElement).toBe(
50+
document.querySelector('.rc-trigger-popup'),
51+
);
52+
});
3953
});

0 commit comments

Comments
 (0)