Skip to content

Commit 723851b

Browse files
committed
fix: nativeElement has high priority
1 parent 8057704 commit 723851b

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/Dom/findDOMNode.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ export function isDOM(node: any): node is HTMLElement | SVGElement {
1313
export default function findDOMNode<T = Element | Text>(
1414
node: React.ReactInstance | HTMLElement | SVGElement | { nativeElement: T },
1515
): T {
16-
if (isDOM(node)) {
17-
return (node as unknown) as T;
18-
}
19-
2016
if (node && typeof node === 'object' && isDOM((node as any).nativeElement)) {
2117
return ((node as any).nativeElement as unknown) as T;
2218
}
2319

20+
if (isDOM(node)) {
21+
return (node as unknown) as T;
22+
}
23+
2424
if (node instanceof React.Component) {
2525
return (ReactDOM.findDOMNode(node) as unknown) as T;
2626
}

tests/findDOMNode.test.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { render } from '@testing-library/react';
22
import * as React from 'react';
33
import findDOMNode, { isDOM } from '../src/Dom/findDOMNode';
4+
import proxyObject from '../src/proxyObject';
45

56
describe('findDOMNode', () => {
67
it('base work', () => {
@@ -97,4 +98,35 @@ describe('findDOMNode', () => {
9798

9899
expect(findDOMNode(elementRef.current)).toBe(container.querySelector('p'));
99100
});
101+
102+
it('with proxyObject', () => {
103+
const Demo = React.forwardRef((_, ref) => {
104+
const rootRef = React.useRef<HTMLDivElement>(null);
105+
const spanRef = React.useRef<HTMLParagraphElement>(null);
106+
107+
React.useImperativeHandle(ref, () =>
108+
proxyObject(rootRef.current, {
109+
nativeElement: spanRef.current,
110+
}),
111+
);
112+
113+
return (
114+
<p ref={rootRef} id="root">
115+
<span ref={spanRef} />
116+
</p>
117+
);
118+
});
119+
120+
const holderRef = React.createRef<any>();
121+
const { container } = render(
122+
<React.StrictMode>
123+
<Demo ref={holderRef} />
124+
</React.StrictMode>,
125+
);
126+
127+
expect(holderRef.current.id).toBe('root');
128+
expect(findDOMNode(holderRef.current)).toBe(
129+
container.querySelector('span'),
130+
);
131+
});
100132
});

0 commit comments

Comments
 (0)