Skip to content

Commit 2cc87ef

Browse files
committed
chore: get dom
1 parent e49ff2d commit 2cc87ef

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Portal from '@rc-component/portal';
22
import { clsx } from 'clsx';
33
import type { CSSMotionProps } from '@rc-component/motion';
44
import { useResizeObserver } from '@rc-component/resize-observer';
5-
import { isDOM } from '@rc-component/util/lib/Dom/findDOMNode';
5+
import { getDOM, isDOM } from '@rc-component/util/lib/Dom/findDOMNode';
66
import { getShadowRoot } from '@rc-component/util/lib/Dom/shadow';
7-
import { useComposeRef } from '@rc-component/util/lib/ref';
7+
import { getNodeRef, useComposeRef } from '@rc-component/util/lib/ref';
88
import useEvent from '@rc-component/util/lib/hooks/useEvent';
99
import useId from '@rc-component/util/lib/hooks/useId';
1010
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
@@ -262,9 +262,11 @@ export function generateTrigger(
262262
const externalForwardRef = React.useRef<HTMLElement>(null);
263263

264264
const setTargetRef = useEvent((node: HTMLElement) => {
265-
if (isDOM(node) && targetEle !== node) {
266-
setTargetEle(node);
267-
externalForwardRef.current = node;
265+
const domNode = getDOM(node) as HTMLElement;
266+
267+
if (isDOM(domNode) && targetEle !== domNode) {
268+
setTargetEle(domNode);
269+
externalForwardRef.current = domNode;
268270
}
269271
});
270272

@@ -789,13 +791,13 @@ export function generateTrigger(
789791
useResizeObserver(mergedOpen, targetEle, onTargetResize);
790792

791793
// Compose refs
792-
const mergedRef = useComposeRef(setTargetRef, (child as any).ref);
794+
const mergedRef = useComposeRef(setTargetRef, getNodeRef(child));
793795

794796
// Child Node
795797
const triggerNode = React.cloneElement(child, {
796-
ref: mergedRef,
797798
...mergedChildrenProps,
798799
...passedProps,
800+
ref: mergedRef,
799801
});
800802

801803
// Render

tests/ref.test.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,36 @@ describe('Trigger.Ref', () => {
5050
document.querySelector('.rc-trigger-popup'),
5151
);
5252
});
53+
54+
it('should support ref.nativeElement', () => {
55+
const ChildComponent = React.forwardRef<
56+
{ nativeElement: HTMLElement },
57+
React.PropsWithChildren
58+
>((props, ref) => {
59+
const buttonRef = React.useRef<HTMLButtonElement>(null);
60+
61+
React.useImperativeHandle(ref, () => ({
62+
nativeElement: buttonRef.current,
63+
}));
64+
65+
return <button ref={buttonRef} {...props} />;
66+
});
67+
68+
const triggerRef = React.createRef<TriggerRef>();
69+
const childRef = React.createRef<{ nativeElement: HTMLElement }>();
70+
71+
const { container } = render(
72+
<Trigger ref={triggerRef} popup={<div />}>
73+
<ChildComponent ref={childRef} />
74+
</Trigger>,
75+
);
76+
77+
const buttonElement = container.querySelector('button');
78+
79+
// Check child ref returns object with nativeElement
80+
expect(childRef.current.nativeElement).toBe(buttonElement);
81+
82+
// Check Trigger can extract DOM from child ref
83+
expect(triggerRef.current.nativeElement).toBe(buttonElement);
84+
});
5385
});

0 commit comments

Comments
 (0)