mirror of
https://github.com/bendtherules/react-fiber-traverse.git
synced 2026-08-18 22:01:49 +00:00
src/utils - Add helpers for detecting react functional and class components
This commit is contained in:
+59
-10
@@ -1,20 +1,69 @@
|
||||
import * as React from 'react';
|
||||
import { FiberNode, FiberNodeisHTMLLike, FiberNodeForFunctionComponent, FiberNodeForComponentClass } from './mocked-types';
|
||||
import * as React from "react";
|
||||
import {
|
||||
FiberNode,
|
||||
FiberNodeisHTMLLike,
|
||||
FiberNodeForFunctionComponent,
|
||||
FiberNodeForComponentClass
|
||||
} from "./mocked-types";
|
||||
|
||||
function isNodeHtmlLike(node: FiberNode): node is FiberNodeisHTMLLike {
|
||||
return (typeof node.type === "string") || node.type === null;
|
||||
return typeof node.type === "string" || node.type === null;
|
||||
}
|
||||
|
||||
function isNodeNotHtmlLike(node: FiberNode): node is Exclude<FiberNode, FiberNodeisHTMLLike> {
|
||||
return !isNodeHtmlLike(node);
|
||||
function isNodeNotHtmlLike(
|
||||
node: FiberNode
|
||||
): node is Exclude<FiberNode, FiberNodeisHTMLLike> {
|
||||
return !isNodeHtmlLike(node);
|
||||
}
|
||||
|
||||
function isNodeFunctionComponent(node: FiberNode): node is FiberNodeForFunctionComponent {
|
||||
return isNodeNotHtmlLike(node) && node.stateNode === null;
|
||||
function isNodeFunctionComponent(
|
||||
node: FiberNode
|
||||
): node is FiberNodeForFunctionComponent {
|
||||
return isNodeNotHtmlLike(node) && node.stateNode === null;
|
||||
}
|
||||
|
||||
function isNodeComponentClass(node: FiberNode): node is FiberNodeForComponentClass {
|
||||
return isNodeNotHtmlLike(node) && (node.stateNode instanceof React.Component);
|
||||
function isNodeComponentClass(
|
||||
node: FiberNode
|
||||
): node is FiberNodeForComponentClass {
|
||||
return isNodeNotHtmlLike(node) && node.stateNode instanceof React.Component;
|
||||
}
|
||||
|
||||
export { isNodeHtmlLike, isNodeNotHtmlLike, isNodeFunctionComponent, isNodeComponentClass };
|
||||
function isConstructorHtmlLike(ctr: React.ElementType): ctr is Exclude<React.ElementType, React.ComponentType> {
|
||||
if (typeof ctr === "string" || ctr === null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function isConstructorComponentClass(
|
||||
ctr: React.ElementType
|
||||
): ctr is React.ComponentClass {
|
||||
if (isConstructorHtmlLike(ctr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
ctr.prototype !== undefined &&
|
||||
(ctr.prototype as any) instanceof React.Component
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function isConstructorFunctionComponent(
|
||||
ctr: React.ElementType
|
||||
): ctr is React.FunctionComponent {
|
||||
return !isConstructorComponentClass(ctr);
|
||||
}
|
||||
|
||||
export {
|
||||
isNodeHtmlLike,
|
||||
isNodeNotHtmlLike,
|
||||
isNodeFunctionComponent,
|
||||
isNodeComponentClass,
|
||||
isConstructorHtmlLike,
|
||||
isConstructorComponentClass,
|
||||
isConstructorFunctionComponent
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user