@gitlab/ui
Version:
GitLab UI Components
40 lines (36 loc) • 1.86 kB
JavaScript
import isFunction from 'lodash/isFunction';
import { DISCLOSURE_DROPDOWN_ITEM_NAME, DISCLOSURE_DROPDOWN_GROUP_NAME } from './constants';
const itemValidator = item => {
var _item$text;
return (item === null || item === void 0 ? void 0 : (_item$text = item.text) === null || _item$text === void 0 ? void 0 : _item$text.length) > 0 && !Array.isArray(item === null || item === void 0 ? void 0 : item.items);
};
const isItem = item => Boolean(item) && itemValidator(item);
const isGroup = group => Boolean(group) && Array.isArray(group.items) && Boolean(group.items.length) &&
// eslint-disable-next-line unicorn/no-array-callback-reference
group.items.every(isItem);
// eslint-disable-next-line unicorn/no-array-callback-reference
const itemsValidator = items => items.every(isItem) || items.every(isGroup);
const isListItem = tag => ['gl-disclosure-dropdown-group', 'gl-disclosure-dropdown-item', 'li'].includes(tag);
const isValidSlotTagVue2 = vNode => {
var _vNode$componentOptio;
return Boolean(vNode) && isListItem(((_vNode$componentOptio = vNode.componentOptions) === null || _vNode$componentOptio === void 0 ? void 0 : _vNode$componentOptio.tag) || vNode.tag);
};
const isValidSlotTag = vNode => {
var _vNode$type;
return [DISCLOSURE_DROPDOWN_ITEM_NAME, DISCLOSURE_DROPDOWN_GROUP_NAME].includes((_vNode$type = vNode.type) === null || _vNode$type === void 0 ? void 0 : _vNode$type.name) || vNode.type === 'li';
};
const hasOnlyListItems = _ref => {
let {
default: defaultSlot
} = _ref;
if (!isFunction(defaultSlot)) {
return false;
}
const nodes = defaultSlot();
if (!Array.isArray(nodes)) {
return false;
}
const tags = nodes.filter(vNode => vNode.tag);
return tags.length && tags.every(tag => isValidSlotTag(tag) || isValidSlotTagVue2(tag));
};
export { hasOnlyListItems, isGroup, isItem, itemsValidator };