form-label-wrap.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var core = require('@vueuse/core');
  5. require('../../../utils/index.js');
  6. require('../../../tokens/index.js');
  7. require('../../../hooks/index.js');
  8. var form = require('../../../tokens/form.js');
  9. var error = require('../../../utils/error.js');
  10. var index = require('../../../hooks/use-namespace/index.js');
  11. const COMPONENT_NAME = "ElLabelWrap";
  12. var FormLabelWrap = vue.defineComponent({
  13. name: COMPONENT_NAME,
  14. props: {
  15. isAutoWidth: Boolean,
  16. updateAll: Boolean
  17. },
  18. setup(props, {
  19. slots
  20. }) {
  21. const formContext = vue.inject(form.formContextKey, void 0);
  22. const formItemContext = vue.inject(form.formItemContextKey);
  23. if (!formItemContext)
  24. error.throwError(COMPONENT_NAME, "usage: <el-form-item><label-wrap /></el-form-item>");
  25. const ns = index.useNamespace("form");
  26. const el = vue.ref();
  27. const computedWidth = vue.ref(0);
  28. const getLabelWidth = () => {
  29. var _a;
  30. if ((_a = el.value) == null ? void 0 : _a.firstElementChild) {
  31. const width = window.getComputedStyle(el.value.firstElementChild).width;
  32. return Math.ceil(Number.parseFloat(width));
  33. } else {
  34. return 0;
  35. }
  36. };
  37. const updateLabelWidth = (action = "update") => {
  38. vue.nextTick(() => {
  39. if (slots.default && props.isAutoWidth) {
  40. if (action === "update") {
  41. computedWidth.value = getLabelWidth();
  42. } else if (action === "remove") {
  43. formContext == null ? void 0 : formContext.deregisterLabelWidth(computedWidth.value);
  44. }
  45. }
  46. });
  47. };
  48. const updateLabelWidthFn = () => updateLabelWidth("update");
  49. vue.onMounted(() => {
  50. updateLabelWidthFn();
  51. });
  52. vue.onBeforeUnmount(() => {
  53. updateLabelWidth("remove");
  54. });
  55. vue.onUpdated(() => updateLabelWidthFn());
  56. vue.watch(computedWidth, (val, oldVal) => {
  57. if (props.updateAll) {
  58. formContext == null ? void 0 : formContext.registerLabelWidth(val, oldVal);
  59. }
  60. });
  61. core.useResizeObserver(vue.computed(() => {
  62. var _a, _b;
  63. return (_b = (_a = el.value) == null ? void 0 : _a.firstElementChild) != null ? _b : null;
  64. }), updateLabelWidthFn);
  65. return () => {
  66. var _a, _b;
  67. if (!slots)
  68. return null;
  69. const {
  70. isAutoWidth
  71. } = props;
  72. if (isAutoWidth) {
  73. const autoLabelWidth = formContext == null ? void 0 : formContext.autoLabelWidth;
  74. const hasLabel = formItemContext == null ? void 0 : formItemContext.hasLabel;
  75. const style = {};
  76. if (hasLabel && autoLabelWidth && autoLabelWidth !== "auto") {
  77. const marginWidth = Math.max(0, Number.parseInt(autoLabelWidth, 10) - computedWidth.value);
  78. const marginPosition = formContext.labelPosition === "left" ? "marginRight" : "marginLeft";
  79. if (marginWidth) {
  80. style[marginPosition] = `${marginWidth}px`;
  81. }
  82. }
  83. return vue.createVNode("div", {
  84. "ref": el,
  85. "class": [ns.be("item", "label-wrap")],
  86. "style": style
  87. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)]);
  88. } else {
  89. return vue.createVNode(vue.Fragment, {
  90. "ref": el
  91. }, [(_b = slots.default) == null ? void 0 : _b.call(slots)]);
  92. }
  93. };
  94. }
  95. });
  96. exports["default"] = FormLabelWrap;
  97. //# sourceMappingURL=form-label-wrap.js.map