Skip to content

Commit

Permalink
feat: support isDOM (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Mar 15, 2023
1 parent da23dd8 commit c9de3e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/Dom/findDOMNode.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import React from 'react';
import ReactDOM from 'react-dom';

export function isDOM(node: any) {
// https://developer.mozilla.org/en-US/docs/Web/API/Element
// Since XULElement is also subclass of Element, we only need HTMLElement and SVGElement
return node instanceof HTMLElement || node instanceof SVGElement;
}

/**
* Return if a node is a DOM node. Else will return by `findDOMNode`
*/
export default function findDOMNode<T = Element | Text>(
node: React.ReactInstance | HTMLElement,
node: React.ReactInstance | HTMLElement | SVGElement,
): T {
if (node instanceof HTMLElement) {
if (isDOM(node)) {
return node as unknown as T;
}

Expand Down
7 changes: 6 additions & 1 deletion tests/findDOMNode.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { render } from '@testing-library/react';
import * as React from 'react';
import findDOMNode from '../src/Dom/findDOMNode';

describe('findDOMNode', () => {
Expand Down Expand Up @@ -55,4 +55,9 @@ describe('findDOMNode', () => {
expect(errSpy).toHaveBeenCalled();
errSpy.mockRestore();
});

it('support svg', () => {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
expect(findDOMNode(svg)).toBe(svg);
});
});

1 comment on commit c9de3e4

@vercel
Copy link

@vercel vercel bot commented on c9de3e4 Mar 15, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

util – ./

util.vercel.app
util-git-master-react-component.vercel.app
util-react-component.vercel.app

Please sign in to comment.