You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.9 KiB

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var shared = require('@vue/shared');
require('../../../hooks/index.js');
var error = require('../../../utils/error.js');
var index = require('../../../hooks/use-forward-ref/index.js');
const NAME = "ElOnlyChild";
const OnlyChild = vue.defineComponent({
name: NAME,
setup(_, { slots, attrs }) {
var _a;
const forwardRefInjection = vue.inject(index.FORWARD_REF_INJECTION_KEY, void 0);
const forwardRefDirective = index.useForwardRefDirective((_a = forwardRefInjection.setForwardRef) != null ? _a : shared.NOOP);
return () => {
var _a2;
const defaultSlot = (_a2 = slots.default) == null ? void 0 : _a2.call(slots, attrs);
if (!defaultSlot)
return null;
if (defaultSlot.length > 1) {
error.debugWarn(NAME, "ElOnlyChild requires exact only one valid child.");
return null;
}
const firstLegitNode = findFirstLegitChild(defaultSlot);
if (!firstLegitNode) {
error.debugWarn(NAME, "no valid child node found");
return null;
}
return vue.withDirectives(vue.cloneVNode(firstLegitNode, attrs), [
[forwardRefDirective]
]);
};
}
});
function findFirstLegitChild(node) {
if (!node)
return null;
const children = node;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (shared.isObject(child)) {
switch (child.type) {
case vue.Comment:
continue;
case vue.Text:
return wrapTextContent(child);
case vue.Fragment:
return findFirstLegitChild(child.children);
default:
return child;
}
}
return wrapTextContent(child);
}
return null;
}
function wrapTextContent(s) {
return vue.h("span", { class: "el-only-child__content" }, [s]);
}
exports["default"] = OnlyChild;
//# sourceMappingURL=only-child.js.map