UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 5.72 kB
{"version":3,"file":"form-label-wrap.mjs","names":["Fragment","computed","defineComponent","inject","nextTick","onBeforeUnmount","onMounted","onUpdated","ref","watch","createVNode","_createVNode","_Fragment","useResizeObserver","throwError","useNamespace","formContextKey","formItemContextKey","COMPONENT_NAME","name","props","isAutoWidth","Boolean","updateAll","setup","slots","formContext","undefined","formItemContext","ns","el","computedWidth","getLabelWidth","value","firstElementChild","width","window","getComputedStyle","Math","ceil","Number","parseFloat","updateLabelWidth","action","default","deregisterLabelWidth","updateLabelWidthFn","val","oldVal","registerLabelWidth","autoLabelWidth","hasLabel","style","marginWidth","max","parseInt","labelPosition","marginPosition","be"],"sources":["../../../../../../packages/components/form/src/form-label-wrap.tsx"],"sourcesContent":["import {\n Fragment,\n computed,\n defineComponent,\n inject,\n nextTick,\n onBeforeUnmount,\n onMounted,\n onUpdated,\n ref,\n watch,\n} from 'vue'\nimport { useResizeObserver } from '@vueuse/core'\nimport { throwError } from '@element-plus/utils'\nimport { useNamespace } from '@element-plus/hooks'\nimport { formContextKey, formItemContextKey } from './constants'\n\nimport type { CSSProperties } from 'vue'\n\nconst COMPONENT_NAME = 'ElLabelWrap'\nexport default defineComponent({\n name: COMPONENT_NAME,\n props: {\n isAutoWidth: Boolean,\n updateAll: Boolean,\n },\n\n setup(props, { slots }) {\n const formContext = inject(formContextKey, undefined)\n const formItemContext = inject(formItemContextKey)\n if (!formItemContext)\n throwError(\n COMPONENT_NAME,\n 'usage: <el-form-item><label-wrap /></el-form-item>'\n )\n\n const ns = useNamespace('form')\n\n const el = ref<HTMLElement>()\n const computedWidth = ref(0)\n\n const getLabelWidth = () => {\n if (el.value?.firstElementChild) {\n const width = window.getComputedStyle(el.value.firstElementChild).width\n return Math.ceil(Number.parseFloat(width))\n } else {\n return 0\n }\n }\n\n const updateLabelWidth = (action: 'update' | 'remove' = 'update') => {\n nextTick(() => {\n if (slots.default && props.isAutoWidth) {\n if (action === 'update') {\n computedWidth.value = getLabelWidth()\n } else if (action === 'remove') {\n formContext?.deregisterLabelWidth(computedWidth.value)\n }\n }\n })\n }\n const updateLabelWidthFn = () => updateLabelWidth('update')\n\n onMounted(() => {\n updateLabelWidthFn()\n })\n onBeforeUnmount(() => {\n updateLabelWidth('remove')\n })\n onUpdated(() => updateLabelWidthFn())\n\n watch(computedWidth, (val, oldVal) => {\n if (props.updateAll) {\n formContext?.registerLabelWidth(val, oldVal)\n }\n })\n\n useResizeObserver(\n computed(\n () => (el.value?.firstElementChild ?? null) as HTMLElement | null\n ),\n updateLabelWidthFn\n )\n\n return () => {\n if (!slots) return null\n\n const { isAutoWidth } = props\n if (isAutoWidth) {\n const autoLabelWidth = formContext?.autoLabelWidth\n const hasLabel = formItemContext?.hasLabel\n const style: CSSProperties = {}\n if (hasLabel && autoLabelWidth && autoLabelWidth !== 'auto') {\n const marginWidth = Math.max(\n 0,\n Number.parseInt(autoLabelWidth, 10) - computedWidth.value\n )\n const labelPosition =\n formItemContext.labelPosition || formContext.labelPosition\n\n const marginPosition =\n labelPosition === 'left' ? 'marginRight' : 'marginLeft'\n\n if (marginWidth) {\n style[marginPosition] = `${marginWidth}px`\n }\n }\n return (\n <div ref={el} class={[ns.be('item', 'label-wrap')]} style={style}>\n {slots.default?.()}\n </div>\n )\n } else {\n return <Fragment ref={el}>{slots.default?.()}</Fragment>\n }\n }\n },\n})\n"],"mappings":";;;;;;;AAmBA,MAAMkB,iBAAiB;AACvB,8BAAehB,gCAAgB;CAC7BiB,MAAMD;CACNE,OAAO;EACLC,aAAaC;EACbC,WAAWD;EACZ;CAEDE,MAAMJ,OAAO,EAAEK,SAAS;EACtB,MAAMC,cAAcvB,OAAOa,gBAAgBW,OAAU;EACrD,MAAMC,kBAAkBzB,OAAOc,mBAAmB;AAClD,MAAI,CAACW,gBACHd,YACEI,gBACA,qDACD;EAEH,MAAMW,KAAKd,aAAa,OAAO;EAE/B,MAAMe,KAAKtB,KAAkB;EAC7B,MAAMuB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