Skip to content

Commit c9de3e4

Browse files
authored
feat: support isDOM (#432)
1 parent da23dd8 commit c9de3e4

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Dom/findDOMNode.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33

4+
export function isDOM(node: any) {
5+
// https://developer.mozilla.org/en-US/docs/Web/API/Element
6+
// Since XULElement is also subclass of Element, we only need HTMLElement and SVGElement
7+
return node instanceof HTMLElement || node instanceof SVGElement;
8+
}
9+
410
/**
511
* Return if a node is a DOM node. Else will return by `findDOMNode`
612
*/
713
export default function findDOMNode<T = Element | Text>(
8-
node: React.ReactInstance | HTMLElement,
14+
node: React.ReactInstance | HTMLElement | SVGElement,
915
): T {
10-
if (node instanceof HTMLElement) {
16+
if (isDOM(node)) {
1117
return node as unknown as T;
1218
}
1319

tests/findDOMNode.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as React from 'react';
21
import { render } from '@testing-library/react';
2+
import * as React from 'react';
33
import findDOMNode from '../src/Dom/findDOMNode';
44

55
describe('findDOMNode', () => {
@@ -55,4 +55,9 @@ describe('findDOMNode', () => {
5555
expect(errSpy).toHaveBeenCalled();
5656
errSpy.mockRestore();
5757
});
58+
59+
it('support svg', () => {
60+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
61+
expect(findDOMNode(svg)).toBe(svg);
62+
});
5863
});

0 commit comments

Comments
 (0)