{"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,CACxD,CAACE,oBAAoB,CACtB,CAAC;;;CAGP,CAAC;AAEF,SAASM,oBACPC,MACwB;CACxB,IAAI,CAACA,MAAM,OAAO,CAAC,MAAM,EAAE;CAC3B,MAAMC,WAAWD;CACjB,MAAME,MAAMD,SAASE,QAAQC,MAAMA,EAAEC,SAASnC,QAAQ,CAAC4B;CAEvD,KAAK,MAAMQ,SAASL,UAAU;;;;;;EAM5B,IAAIpB,SAASyB,MAAM,EACjB,QAAQA,MAAMD,MAAd;GACE,KAAKnC,SACH;GACF,KAAKE;GACL,KAAK,OACH,OAAO,CAACmC,gBAAgBD,MAAM,EAAEJ,IAAI;GACtC,KAAK/B,UACH,OAAO4B,oBAAoBO,MAAML,SAAoB;GACvD,SACE,OAAO,CAACK,OAAOJ,IAAI;;EAGzB,OAAO,CAACK,gBAAgBD,MAAM,EAAEJ,IAAI;;CAEtC,OAAO,CAAC,MAAM,EAAE;;AAGlB,SAASK,gBAAgBC,GAAmB;CAE1C,OAAA9B,YAAA,QAAA,EAAA,SADWM,aAAa,aACF,CAAC0B,EAAE,UAAS,EAAC,EAAA,CAAGF,EAAC,CAAA"}