form-label-wrap.js 3.4 KB

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