完全跑通1.0版本

This commit is contained in:
2026-05-26 12:56:03 +08:00
parent 2ece5174a7
commit 93c714a93b
11557 changed files with 1648225 additions and 36 deletions

View File

@@ -0,0 +1,2 @@
import { OnlyChild, OnlyChildExpose } from "./src/only-child.js";
export { OnlyChild as ElOnlyChild, OnlyChild, OnlyChildExpose };

View File

@@ -0,0 +1,2 @@
import { OnlyChild } from "./src/only-child.mjs";
export { OnlyChild as ElOnlyChild, OnlyChild };

View File

@@ -0,0 +1,12 @@
import * as _$vue from "vue";
import { Ref, VNode } from "vue";
//#region ../../packages/components/slot/src/only-child.d.ts
declare const OnlyChild: _$vue.DefineComponent<{}, () => VNode<_$vue.RendererNode, _$vue.RendererElement, {
[key: string]: any;
}> | null, {}, {}, {}, _$vue.ComponentOptionsMixin, _$vue.ComponentOptionsMixin, {}, string, _$vue.PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, _$vue.ComponentProvideOptions, true, {}, any>;
type OnlyChildExpose = {
forwardRef: Ref<HTMLElement>;
};
//#endregion
export { OnlyChild, OnlyChildExpose };

View File

@@ -0,0 +1,53 @@
import { isObject } from "../../../utils/types.mjs";
import { debugWarn } from "../../../utils/error.mjs";
import { NOOP } from "../../../utils/functions.mjs";
import { useNamespace } from "../../../hooks/use-namespace/index.mjs";
import { FORWARD_REF_INJECTION_KEY, useForwardRefDirective } from "../../../hooks/use-forward-ref/index.mjs";
import { Comment, Fragment, Text, cloneVNode, createVNode, defineComponent, inject, withDirectives } from "vue";
//#region ../../packages/components/slot/src/only-child.tsx
const NAME = "ElOnlyChild";
const OnlyChild = /* @__PURE__ */ defineComponent({
name: NAME,
setup(_, { slots, attrs }) {
const forwardRefDirective = useForwardRefDirective(inject(FORWARD_REF_INJECTION_KEY)?.setForwardRef ?? NOOP);
return () => {
const defaultSlot = slots.default?.(attrs);
if (!defaultSlot) return null;
const [firstLegitNode, length] = findFirstLegitChild(defaultSlot);
if (!firstLegitNode) {
debugWarn(NAME, "no valid child node found");
return null;
}
if (length > 1) debugWarn(NAME, "requires exact only one valid child.");
return withDirectives(cloneVNode(firstLegitNode, attrs), [[forwardRefDirective]]);
};
}
});
function findFirstLegitChild(node) {
if (!node) return [null, 0];
const children = node;
const len = children.filter((c) => c.type !== Comment).length;
for (const child of children) {
/**
* when user uses h(Fragment, [text]) to render plain string,
* this switch case just cannot handle, when the value is primitives
* we should just return the wrapped string
*/
if (isObject(child)) switch (child.type) {
case Comment: continue;
case Text:
case "svg": return [wrapTextContent(child), len];
case Fragment: return findFirstLegitChild(child.children);
default: return [child, len];
}
return [wrapTextContent(child), len];
}
return [null, 0];
}
function wrapTextContent(s) {
return createVNode("span", { "class": useNamespace("only-child").e("content") }, [s]);
}
//#endregion
export { OnlyChild };
//# sourceMappingURL=only-child.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"only-child.mjs","names":["Comment","Fragment","Text","cloneVNode","defineComponent","inject","withDirectives","createVNode","_createVNode","NOOP","debugWarn","isObject","FORWARD_REF_INJECTION_KEY","useForwardRefDirective","useNamespace","NAME","OnlyChild","name","setup","_","slots","attrs","forwardRefInjection","forwardRefDirective","setForwardRef","defaultSlot","default","firstLegitNode","length","findFirstLegitChild","node","children","len","filter","c","type","child","wrapTextContent","s","ns","e"],"sources":["../../../../../../packages/components/slot/src/only-child.tsx"],"sourcesContent":["import {\n Comment,\n Fragment,\n Text,\n cloneVNode,\n defineComponent,\n inject,\n withDirectives,\n} from 'vue'\nimport { NOOP, debugWarn, isObject } from '@element-plus/utils'\nimport {\n FORWARD_REF_INJECTION_KEY,\n useForwardRefDirective,\n useNamespace,\n} from '@element-plus/hooks'\n\nimport type { Ref, VNode } from 'vue'\n\nconst NAME = 'ElOnlyChild'\n\nexport const OnlyChild = defineComponent({\n name: NAME,\n setup(_, { slots, attrs }) {\n const forwardRefInjection = inject(FORWARD_REF_INJECTION_KEY)\n const forwardRefDirective = useForwardRefDirective(\n forwardRefInjection?.setForwardRef ?? NOOP\n )\n return () => {\n const defaultSlot = slots.default?.(attrs)\n if (!defaultSlot) return null\n const [firstLegitNode, length] = findFirstLegitChild(defaultSlot)\n\n if (!firstLegitNode) {\n debugWarn(NAME, 'no valid child node found')\n return null\n }\n if (length > 1) {\n debugWarn(NAME, 'requires exact only one valid child.')\n }\n\n return withDirectives(cloneVNode(firstLegitNode!, attrs), [\n [forwardRefDirective],\n ])\n }\n },\n})\n\nfunction findFirstLegitChild(\n node: VNode[] | undefined\n): [VNode | null, number] {\n if (!node) return [null, 0]\n const children = node as VNode[]\n const len = children.filter((c) => c.type !== Comment).length\n\n for (const child of children) {\n /**\n * when user uses h(Fragment, [text]) to render plain string,\n * this switch case just cannot handle, when the value is primitives\n * we should just return the wrapped string\n */\n if (isObject(child)) {\n switch (child.type) {\n case Comment:\n continue\n case Text:\n case 'svg':\n return [wrapTextContent(child), len]\n case Fragment:\n return findFirstLegitChild(child.children as VNode[])\n default:\n return [child, len]\n }\n }\n return [wrapTextContent(child), len]\n }\n return [null, 0]\n}\n\nfunction wrapTextContent(s: string | VNode) {\n const ns = useNamespace('only-child')\n return <span class={ns.e('content')}>{s}</span>\n}\n\nexport type OnlyChildExpose = {\n forwardRef: Ref<HTMLElement>\n}\n"],"mappings":";;;;;;;AAkBA,MAAMe,OAAO;AAEb,MAAaC,YAAYZ,gCAAgB;CACvCa,MAAMF;CACNG,MAAMC,GAAG,EAAEC,OAAOC,SAAS;EAEzB,MAAME,sBAAsBV,uBADAR,OAAOO,0BAEd,EAAEY,iBAAiBf,KACvC;EACD,aAAa;GACX,MAAMgB,cAAcL,MAAMM,UAAUL,MAAM;GAC1C,IAAI,CAACI,aAAa,OAAO;GACzB,MAAM,CAACE,gBAAgBC,UAAUC,oBAAoBJ,YAAY;GAEjE,IAAI,CAACE,gBAAgB;IACnBjB,UAAUK,MAAM,4BAA4B;IAC5C,OAAO;;GAET,IAAIa,SAAS,GACXlB,UAAUK,MAAM,uCAAuC;GAGzD,OAAOT,eAAeH,WAAWwB,gBAAiBN,MAAM,EAAE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