@visactor/react-vchart
Version:
The react version of VChart 4.x
33 lines (25 loc) • 1.82 kB
JavaScript
import { isNil, isArray } from "@visactor/vutils";
import React from "react";
import { isFragment } from "react-is";
export const getDisplayName = Comp => "string" == typeof Comp ? Comp : Comp ? Comp.displayName || Comp.name : "";
export const typeOfComponent = (component, customTypeKey = "__TYPE") => (null == component ? void 0 : component.props) && component.props[customTypeKey] || "string" == typeof (null == component ? void 0 : component.type) && component.type || (null == component ? void 0 : component.type) && "symbol" == typeof component.type && "Symbol(react.fragment)" === component.type.toString() && "react.fragment" || "function" == typeof (null == component ? void 0 : component.type) && component.type || "object" == typeof (null == component ? void 0 : component.type) && "Symbol(react.forward_ref)" === component.type.$$typeof.toString() && "react.forward_ref" || "string" == typeof component && "string" || "function" == typeof component && "function" || void 0;
export const toArray = children => {
let result = [];
return React.Children.forEach(children, (child => {
isNil(child) || (isFragment(child) ? result = result.concat(toArray(child.props.children)) : result.push(child));
})), result;
};
export const findAllByType = (children, type) => {
const result = [];
let types = [];
return types = isArray(type) ? type.map((t => getDisplayName(t))) : [ getDisplayName(type) ],
toArray(children).forEach((child => {
const childType = getDisplayName(typeOfComponent(child));
-1 !== types.indexOf(childType) && result.push(child);
})), result;
};
export const findChildByType = (children, type) => {
const result = findAllByType(children, type);
return null == result ? void 0 : result[0];
};
//# sourceMappingURL=util.js.map