Skip to content
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
17 changes: 12 additions & 5 deletions src/Dom/findDOMNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import ReactDOM from 'react-dom';

export function isDOM(node: any): node is HTMLElement | SVGElement {
// https://developer.mozilla.org/en-US/docs/Web/API/Element
Expand All @@ -26,15 +25,23 @@ export function getDOM(node: any): HTMLElement | SVGElement | null {
* Return if a node is a DOM node. Else will return by `findDOMNode`
*/
export default function findDOMNode<T = Element | Text>(
node: React.ReactInstance | HTMLElement | SVGElement | { nativeElement: T },
): T {
node:
| React.ReactInstance
| HTMLElement
| SVGElement
| { nativeElement: T }
| { current: T },
): T | null {
const domNode = getDOM(node);
if (domNode) {
return domNode as T;
}

if (node instanceof React.Component) {
return ReactDOM.findDOMNode?.(node) as unknown as T;
if (node && typeof node === 'object' && 'current' in node) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还要检测一下这个 current 是不是 DOM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

const refDomNode = getDOM(node.current);
if (refDomNode) {
return refDomNode as T;
}
}

return null;
Expand Down
26 changes: 23 additions & 3 deletions tests/findDOMNode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ describe('findDOMNode', () => {
const errSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

class DOMWrapper extends React.Component {
private elementRef = React.createRef<HTMLDivElement>();
getDOM = () => {
return findDOMNode(this);
return findDOMNode(this.elementRef);
};

render() {
return <div />;
return <div ref={this.elementRef} />;
}
}

Expand All @@ -53,7 +54,7 @@ describe('findDOMNode', () => {

expect(wrapperRef.current!.getDOM()).toBe(container.firstChild);

expect(errSpy).toHaveBeenCalled();
expect(errSpy).not.toHaveBeenCalled();
errSpy.mockRestore();
});

Expand All @@ -75,7 +76,26 @@ describe('findDOMNode', () => {
expect(true).toBeFalsy();
}
});
it('should return DOM node from ref.current', () => {
const TestComponent = React.forwardRef<HTMLDivElement>((_, ref) => {
return <div ref={ref}>test</div>;
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const elementRef = React.createRef<HTMLDivElement>();
const { container } = render(
<React.StrictMode>
<TestComponent ref={elementRef} />
</React.StrictMode>,
);

expect(findDOMNode(elementRef)).toBe(container.firstChild);
});

it('should return null if ref is not mounted', () => {
const elementRef = React.createRef<HTMLDivElement>();

expect(findDOMNode(elementRef)).toBeNull();
});
it('nativeElement', () => {
const Element = React.forwardRef<{ nativeElement: HTMLDivElement }>(
(_, ref) => {
Expand Down
Loading