@visactor/openinula-vtable
Version:
The openinula version of VTable
33 lines (25 loc) • 1.85 kB
JavaScript
import { isNil, isArray } from "@visactor/vutils";
import Inula, { isFragment } from "openinula";
let id = 0;
export const uid = prefix => prefix ? `${prefix}-${id++}` : "" + id++;
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(inula.fragment)" === component.type.toString() && "inula.fragment" || "function" == typeof (null == component ? void 0 : component.type) && component.type || "object" == typeof (null == component ? void 0 : component.type) && "Symbol(inula.forward_ref)" === component.type.$$typeof.toString() && "inula.forward_ref" || "string" == typeof component && "string" || "function" == typeof component && "function" || void 0;
export const toArray = children => {
let result = [];
return Inula.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];
};