{"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,0BAA0B,EAEtCY,iBAAiBf,KACvC;AACD,eAAa;GACX,MAAMgB,cAAcL,MAAMM,UAAUL,MAAM;AAC1C,OAAI,CAACI,YAAa,QAAO;GACzB,MAAM,CAACE,gBAAgBC,UAAUC,oBAAoBJ,YAAY;AAEjE,OAAI,CAACE,gBAAgB;AACnBjB,cAAUK,MAAM,4BAA4B;AAC5C,WAAO;;AAET,OAAIa,SAAS,EACXlB,WAAUK,MAAM,uCAAuC;AAGzD,UAAOT,eAAeH,WAAWwB,gBAAiBN,MAAM,EAAE,CACxD,CAACE,oBAAoB,CACtB,CAAC;;;CAGP,CAAC;AAEF,SAASM,oBACPC,MACwB;AACxB,KAAI,CAACA,KAAM,QAAO,CAAC,MAAM,EAAE;CAC3B,MAAMC,WAAWD;CACjB,MAAME,MAAMD,SAASE,QAAQC,MAAMA,EAAEC,SAASnC,QAAQ,CAAC4B;AAEvD,MAAK,MAAMQ,SAASL,UAAU;;;;;;AAM5B,MAAIpB,SAASyB,MAAM,CACjB,SAAQA,MAAMD,MAAd;GACE,KAAKnC,QACH;GACF,KAAKE;GACL,KAAK,MACH,QAAO,CAACmC,gBAAgBD,MAAM,EAAEJ,IAAI;GACtC,KAAK/B,SACH,QAAO4B,oBA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