element-plus
Version:
A Component Library for Vue 3
1 lines • 4.48 kB
Source Map (JSON)
{"version":3,"file":"label-wrap.mjs","sources":["../../../../../../packages/components/form/src/label-wrap.ts"],"sourcesContent":["import {\n defineComponent,\n Fragment,\n h,\n inject,\n nextTick,\n onBeforeUnmount,\n onMounted,\n onUpdated,\n ref,\n watch,\n} from 'vue'\nimport {\n addResizeListener,\n removeResizeListener,\n} from '@element-plus/utils/resize-event'\nimport { elFormItemKey, elFormKey } from '@element-plus/tokens'\nimport type { ResizableElement } from '@element-plus/utils/resize-event'\n\nimport type { CSSProperties } from 'vue'\nimport type { Nullable } from '@element-plus/utils/types'\n\nexport default defineComponent({\n name: 'ElLabelWrap',\n props: {\n isAutoWidth: Boolean,\n updateAll: Boolean,\n },\n setup(props, { slots }) {\n const el = ref<Nullable<HTMLElement>>(null)\n const elForm = inject(elFormKey)\n const elFormItem = inject(elFormItemKey)\n\n const computedWidth = ref(0)\n watch(computedWidth, (val, oldVal) => {\n if (props.updateAll) {\n elForm.registerLabelWidth(val, oldVal)\n elFormItem.updateComputedLabelWidth(val)\n }\n })\n\n const getLabelWidth = () => {\n if (el.value?.firstElementChild) {\n const width = window.getComputedStyle(el.value.firstElementChild).width\n return Math.ceil(parseFloat(width))\n } else {\n return 0\n }\n }\n const updateLabelWidth = (action = 'update') => {\n nextTick(() => {\n if (slots.default && props.isAutoWidth) {\n if (action === 'update') {\n computedWidth.value = getLabelWidth()\n } else if (action === 'remove') {\n elForm.deregisterLabelWidth(computedWidth.value)\n }\n }\n })\n }\n const updateLabelWidthFn = () => updateLabelWidth('update')\n\n onMounted(() => {\n addResizeListener(\n el.value.firstElementChild as ResizableElement,\n updateLabelWidthFn\n )\n updateLabelWidthFn()\n })\n\n onUpdated(updateLabelWidthFn)\n\n onBeforeUnmount(() => {\n updateLabelWidth('remove')\n removeResizeListener(\n el.value?.firstElementChild as ResizableElement,\n updateLabelWidthFn\n )\n })\n\n function render() {\n if (!slots) return null\n if (props.isAutoWidth) {\n const autoLabelWidth = elForm.autoLabelWidth\n const style = {} as CSSProperties\n if (autoLabelWidth && autoLabelWidth !== 'auto') {\n const marginWidth = Math.max(\n 0,\n parseInt(autoLabelWidth, 10) - computedWidth.value\n )\n const marginPosition =\n elForm.labelPosition === 'left' ? 'marginRight' : 'marginLeft'\n if (marginWidth) {\n style[marginPosition] = `${marginWidth}px`\n }\n }\n return h(\n 'div',\n {\n ref: el,\n class: ['el-form-item__label-wrap'],\n style,\n },\n slots.default?.()\n )\n } else {\n return h(Fragment, { ref: el }, slots.default?.())\n }\n }\n\n return render\n },\n})\n"],"names":[],"mappings":";;;;;AAsBA,gBAAe,gBAAgB;AAAA,EAC7B,MAAM;AAAA,EACN,OAAO;AAAA,IACL,aAAa;AAAA,IACb,WAAW;AAAA;AAAA,EAEb,MAAM,OAAO,EAAE,SAAS;AACtB,UAAM,KAAK,IAA2B;AACtC,UAAM,SAAS,OAAO;AACtB,UAAM,aAAa,OAAO;AAE1B,UAAM,gBAAgB,IAAI;AAC1B,UAAM,eAAe,CAAC,KAAK,WAAW;AACpC,UAAI,MAAM,WAAW;AACnB,eAAO,mBAAmB,KAAK;AAC/B,mBAAW,yBAAyB;AAAA;AAAA;AAIxC,UAAM,gBAAgB,MAAM;AAzChC;AA0CM,UAAI,SAAG,UAAH,mBAAU,mBAAmB;AAC/B,cAAM,QAAQ,OAAO,iBAAiB,GAAG,MAAM,mBAAmB;AAClE,eAAO,KAAK,KAAK,WAAW;AAAA,aACvB;AACL,eAAO;AAAA;AAAA;AAGX,UAAM,mBAAmB,CAAC,SAAS,aAAa;AAC9C,eAAS,MAAM;AACb,YAAI,MAAM,WAAW,MAAM,aAAa;AACtC,cAAI,WAAW,UAAU;AACvB,0BAAc,QAAQ;AAAA,qBACb,WAAW,UAAU;AAC9B,mBAAO,qBAAqB,cAAc;AAAA;AAAA;AAAA;AAAA;AAKlD,UAAM,qBAAqB,MAAM,iBAAiB;AAElD,cAAU,MAAM;AACd,wBACE,GAAG,MAAM,m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