e-desc.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <div class="desc" :style="{ margin }">
  3. <!-- 标题 -->
  4. <h1 v-if="title" class="desc-title" v-html="title"></h1>
  5. <el-row class="desc-row" ref='elRow'>
  6. <slot />
  7. </el-row>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'EDesc',
  13. // 通过provide提供给子组件
  14. provide() {
  15. return {
  16. labelWidth: this.labelWidth,
  17. column: this.column,
  18. size: this.size
  19. }
  20. },
  21. props: {
  22. // 标题
  23. title: {
  24. type: String,
  25. default: ''
  26. },
  27. // 边距
  28. margin: {
  29. type: String,
  30. default: '0'
  31. },
  32. // label宽度
  33. labelWidth: {
  34. type: String,
  35. default: '120px'
  36. },
  37. column: {
  38. // 每行显示的项目个数
  39. type: [Number, String],
  40. default: 3
  41. },
  42. size: {
  43. // 大小
  44. type: String,
  45. default: ''
  46. }
  47. },
  48. data() {
  49. return {
  50. // 监听插槽变化
  51. observe: new MutationObserver(this.computedSpan)
  52. }
  53. },
  54. mounted() {
  55. this.$nextTick(() => {
  56. this.computedSpan()
  57. this.observe.observe(this.$refs.elRow.$el, { childList: true })
  58. })
  59. },
  60. methods: {
  61. computedSpan() {
  62. // 筛选出子组件e-desc-item(防御:slots 可能为 undefined)
  63. const slotNodes = (this.$slots && this.$slots.default) ? this.$slots.default : []
  64. if (!Array.isArray(slotNodes) || slotNodes.length === 0) {
  65. return
  66. }
  67. const dataList = []
  68. slotNodes.forEach(node => {
  69. if (
  70. node &&
  71. node.componentOptions &&
  72. node.componentOptions.tag === 'e-desc-item' &&
  73. node.componentInstance
  74. ) {
  75. dataList.push(node.componentInstance)
  76. }
  77. })
  78. // 若没有 e-desc-item,直接返回
  79. if (dataList.length === 0) return
  80. // 剩余span(确保为数字)
  81. let leftSpan = Number(this.column) || 1
  82. const len = dataList.length
  83. dataList.forEach((item, index) => {
  84. // 处理column与span之间的关系
  85. // 剩余的列数小于设置的span数
  86. const hasLeft = leftSpan <= (item.span || 1)
  87. // 当前列的下一列大于了剩余span
  88. const nextColumnSpan = (index < (len - 1)) && (dataList[index + 1].span >= leftSpan)
  89. // 是最后一行的最后一列
  90. const isLast = index === (len - 1)
  91. if (hasLeft || nextColumnSpan || isLast) {
  92. // 满足以上条件,需要自动补全span,避免最后一列出现残缺的情况
  93. item.selfSpan = leftSpan
  94. leftSpan = Number(this.column) || 1
  95. } else {
  96. leftSpan -= item.span || 1
  97. }
  98. })
  99. }
  100. },
  101. beforeDestroy() {
  102. if (this.observe) {
  103. this.observe.disconnect()
  104. }
  105. }
  106. }
  107. </script>
  108. <style scoped lang="scss">
  109. .desc {
  110. .desc-title {
  111. margin-bottom: 10px;
  112. color: #333;
  113. font-weight: 700;
  114. font-size: 16px;
  115. line-height: 1.5715;
  116. }
  117. .desc-row {
  118. display: flex;
  119. flex-wrap: wrap;
  120. border-radius: 2px;
  121. border: 1px solid #EBEEF5;
  122. border-bottom: 0;
  123. border-right: 0;
  124. width: 100%;
  125. }
  126. }
  127. </style>