UNPKG

@antdv/pro-utils

Version:

@antdv/pro-utils

143 lines (142 loc) 4.29 kB
var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; import { camelize, hasOwn } from "@vue/shared"; import { cloneVNode, Fragment, isVNode } from "vue"; import { isArray, isComment, isEmptyElement, isFragment, isTemplate, isValid } from "../is/index.mjs"; function filterEmpty(children = []) { const res = []; children.forEach((child) => { if (Array.isArray(child)) res.push(...child); else if ((child == null ? void 0 : child.type) === Fragment) res.push(...filterEmpty(child.children)); else res.push(child); }); return res.filter((c) => !isEmptyElement(c)); } function filterEmptyWithUndefined(children) { if (children) { const coms = filterEmpty(children); return coms.length ? coms : void 0; } else { return children; } } const skipFlattenKey = Symbol("skipFlatten"); function flattenChildren(children = [], isFilterEmpty = true) { const temp = Array.isArray(children) ? children : [children]; const res = []; temp.forEach((child) => { if (Array.isArray(child)) { res.push(...flattenChildren(child, isFilterEmpty)); } else if (child && child.type === Fragment) { if (child.key === skipFlattenKey) res.push(child); else res.push(...flattenChildren(child.children, isFilterEmpty)); } else if (child && isVNode(child)) { if (isFilterEmpty && !isEmptyElement(child)) res.push(child); else if (!isFilterEmpty) res.push(child); } else if (isValid(child)) { res.push(child); } }); return res; } function getSlot(self, name = "default", options = {}) { var _a, _b; if (isVNode(self)) { if (self.type === Fragment) return name === "default" ? flattenChildren(self.children) : []; else if (self.children && self.children[name]) return flattenChildren((_b = (_a = self.children)[name]) == null ? void 0 : _b.call(_a, options)); else return []; } else { const res = self.$slots[name] && self.$slots[name](options); return flattenChildren(res); } } function cloneElement(vnode, nodeProps = {}, override = true, mergeRef = false) { let ele = vnode; if (Array.isArray(vnode)) ele = filterEmpty(vnode)[0]; if (!ele) return null; const node = cloneVNode(ele, nodeProps, mergeRef); node.props = override ? __spreadValues(__spreadValues({}, node.props), nodeProps) : node.props; return node; } function ensureOnlyChild(children) { if (!isArray(children) || children.length > 1) { throw new Error("expect to receive a single Vue element child"); } return children[0]; } function getNormalizedProps(node) { if (!isVNode(node)) { console.warn("[getNormalizedProps] must be a VNode"); return {}; } const raw = node.props || {}; const type = (isVNode(node.type) ? node.type.props : void 0) || {}; const props = {}; Object.keys(type).forEach((key) => { if (hasOwn(type[key], "default")) { props[key] = type[key].default; } }); Object.keys(raw).forEach((key) => { props[camelize(key)] = raw[key]; }); return props; } function getChildren(node, depth) { if (isComment(node)) return; if (isFragment(node) || isTemplate(node)) { return depth > 0 ? getFirstValidNode(node.children, depth - 1) : void 0; } return node; } function getFirstValidNode(nodes, maxDepth = 3) { if (Array.isArray(nodes)) { return getChildren(nodes[0], maxDepth); } else { return getChildren(nodes, maxDepth); } } export { cloneElement, ensureOnlyChild, filterEmpty, filterEmptyWithUndefined, flattenChildren, getFirstValidNode, getNormalizedProps, getSlot, skipFlattenKey };