Skip to content

Commit

Permalink
Msh
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Mar 15, 2023
2 parents 0563b41 + fba0135 commit 2a3a2d2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

lint-staged
npx lint-staged
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"create-react-class": "^15.6.3",
"cross-env": "^7.0.2",
"dumi": "^2.1.3",
"eslint": "~7.32.0",
"eslint": "~7.31.0",
"father": "^4.1.3",
"glob": "^7.1.6",
"husky": "^8.0.3",
Expand Down
6 changes: 3 additions & 3 deletions src/Dom/findDOMNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

export function isDOM(node: any) {
export function isDOM(node: any): node is HTMLElement | SVGElement {
// 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;
Expand All @@ -14,11 +14,11 @@ export default function findDOMNode<T = Element | Text>(
node: React.ReactInstance | HTMLElement | SVGElement,
): T {
if (isDOM(node)) {
return node as unknown as T;
return (node as unknown) as T;
}

if (node instanceof React.Component) {
return ReactDOM.findDOMNode(node) as unknown as T;
return (ReactDOM.findDOMNode(node) as unknown) as T;
}

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

describe('findDOMNode', () => {
it('base work', () => {
Expand Down Expand Up @@ -60,4 +60,18 @@ describe('findDOMNode', () => {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
expect(findDOMNode(svg)).toBe(svg);
});

it('isDOM type', () => {
const svg: any = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg',
);

// This `getBoundingClientRect` is used for ts type check
if (isDOM(svg) && svg.getBoundingClientRect()) {
expect(true).toBeTruthy();
} else {
expect(true).toBeFalsy();
}
});
});

1 comment on commit 2a3a2d2

@vercel
Copy link

@vercel vercel bot commented on 2a3a2d2 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-react-component.vercel.app
util-git-master-react-component.vercel.app

Please sign in to comment.