fine-true
Version:
A small and beautiful Vue3 version of the UI Library
63 lines (58 loc) • 1.84 kB
text/typescript
import { Fragment, isVNode, Comment } from 'vue';
const isValid = (value: any): boolean => {
return value !== undefined && value !== null && value !== '';
};
function isEmptyElement(c: any) {
return (
c &&
(c.type === Comment ||
(c.type === Fragment && c.children.length === 0) ||
(c.type === Text && c.children.trim() === ''))
);
}
export const flattenChildren = (children: any = [], filterEmpty = true) => {
const temp = Array.isArray(children) ? children : [children];
const res: any = [];
temp.forEach((child) => {
if (Array.isArray(child)) {
res.push(...flattenChildren(child, filterEmpty));
} else if (child && child.type === Fragment) {
res.push(...flattenChildren(child.children, filterEmpty));
} else if (child && isVNode(child)) {
if (filterEmpty && !isEmptyElement(child)) {
res.push(child);
} else if (!filterEmpty) {
res.push(child);
}
} else if (isValid(child)) {
res.push(child);
}
});
return res;
};
function isValidElement(element: any) {
if (Array.isArray(element) && element.length === 1) {
element = element[0];
}
return element && element.__v_isVNode && typeof element.type !== 'symbol'; // remove text node
}
export function parseTabList(children: any[]): any[] {
return children
.map((node) => {
if (isValidElement(node)) {
const props = { ...(node.props || {}) };
const slots = node.children || {};
const key = node.key !== undefined ? node.key : undefined;
const { label, disabled, closable, value } = props;
return {
label,
disabled,
closable,
node,
value,
};
}
return null;
})
.filter((tab) => tab);
}