@visactor/react-vchart
Version:
The react version of VChart 4.x
50 lines (37 loc) • 2.49 kB
JavaScript
;
var __importDefault = this && this.__importDefault || function(mod) {
return mod && mod.__esModule ? mod : {
default: mod
};
};
Object.defineProperty(exports, "__esModule", {
value: !0
}), exports.findChildByType = exports.findAllByType = exports.toArray = exports.typeOfComponent = exports.getDisplayName = void 0;
const vutils_1 = require("@visactor/vutils"), react_1 = __importDefault(require("react")), react_is_1 = require("react-is"), getDisplayName = Comp => "string" == typeof Comp ? Comp : Comp ? Comp.displayName || Comp.name : "";
exports.getDisplayName = getDisplayName;
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;
exports.typeOfComponent = typeOfComponent;
const toArray = children => {
let result = [];
return react_1.default.Children.forEach(children, (child => {
(0, vutils_1.isNil)(child) || ((0, react_is_1.isFragment)(child) ? result = result.concat((0,
exports.toArray)(child.props.children)) : result.push(child));
})), result;
};
exports.toArray = toArray;
const findAllByType = (children, type) => {
const result = [];
let types = [];
return types = (0, vutils_1.isArray)(type) ? type.map((t => (0, exports.getDisplayName)(t))) : [ (0,
exports.getDisplayName)(type) ], (0, exports.toArray)(children).forEach((child => {
const childType = (0, exports.getDisplayName)((0, exports.typeOfComponent)(child));
-1 !== types.indexOf(childType) && result.push(child);
})), result;
};
exports.findAllByType = findAllByType;
const findChildByType = (children, type) => {
const result = (0, exports.findAllByType)(children, type);
return null == result ? void 0 : result[0];
};
exports.findChildByType = findChildByType;
//# sourceMappingURL=util.js.map