xt-element-ui 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,35 @@
1
1
  <template>
2
+ <el-tooltip
3
+ v-if="showTooltip && ellipsis"
4
+ :content="displayTooltipContent"
5
+ :placement="tooltipPlacement"
6
+ :disabled="!isOverflow"
7
+ effect="dark"
8
+ >
9
+ <span
10
+ ref="textRef"
11
+ class="xt-text"
12
+ :class="[
13
+ type ? 'xt-text--' + type : '',
14
+ 'xt-text--' + size,
15
+ { 'xt-text--bold': bold },
16
+ { 'xt-text--money': format === 'money' },
17
+ { 'xt-text--ellipsis': ellipsis },
18
+ { 'xt-text--ellipsis-multiline': ellipsis && ellipsisRows > 1 }
19
+ ]"
20
+ :style="customStyle"
21
+ @mouseenter="handleMouseEnter"
22
+ >
23
+ <slot name="prefix">{{ prefix }}</slot>
24
+ <slot>
25
+ <template v-if="formattedValue !== undefined">{{ formattedValue }}</template>
26
+ </slot>
27
+ <slot name="suffix">{{ suffix }}</slot>
28
+ </span>
29
+ </el-tooltip>
2
30
  <span
31
+ v-else
32
+ ref="textRef"
3
33
  class="xt-text"
4
34
  :class="[
5
35
  type ? 'xt-text--' + type : '',
@@ -54,6 +84,19 @@ export default {
54
84
  default: 1,
55
85
  validator: (val) => val >= 1 && val <= 10
56
86
  },
87
+ showTooltip: {
88
+ type: Boolean,
89
+ default: true
90
+ },
91
+ tooltipPlacement: {
92
+ type: String,
93
+ default: 'top',
94
+ validator: (val) => ['top', 'bottom', 'left', 'right', 'top-start', 'top-end', 'bottom-start', 'bottom-end', 'left-start', 'left-end', 'right-start', 'right-end'].includes(val)
95
+ },
96
+ tooltipContent: {
97
+ type: String,
98
+ default: ''
99
+ },
57
100
 
58
101
  // 格式化模式:normal 普通 | thousand 千分位 | money 金额
59
102
  format: {
@@ -98,6 +141,11 @@ export default {
98
141
  default: ''
99
142
  }
100
143
  },
144
+ data() {
145
+ return {
146
+ isOverflow: false
147
+ }
148
+ },
101
149
  computed: {
102
150
  // 兼容旧 money 属性:如果传了 money=true,自动切为金额模式
103
151
  realFormat() {
@@ -115,6 +163,19 @@ export default {
115
163
  return style
116
164
  },
117
165
 
166
+ displayTooltipContent() {
167
+ if (this.tooltipContent) {
168
+ return this.tooltipContent
169
+ }
170
+ if (this.formattedValue !== undefined) {
171
+ return this.formattedValue
172
+ }
173
+ if (this.$slots.default && this.$slots.default.length > 0) {
174
+ return this.extractSlotText(this.$slots.default)
175
+ }
176
+ return this.value
177
+ },
178
+
118
179
  formattedValue() {
119
180
  const fmt = this.realFormat
120
181
  const { value } = this
@@ -163,6 +224,75 @@ export default {
163
224
  return value
164
225
  }
165
226
  }
227
+ },
228
+ methods: {
229
+ handleMouseEnter() {
230
+ this.checkOverflow()
231
+ },
232
+ checkOverflow() {
233
+ const el = this.$refs.textRef
234
+ if (!el) {
235
+ this.isOverflow = false
236
+ return
237
+ }
238
+
239
+ if (this.ellipsisRows > 1) {
240
+ this.isOverflow = el.scrollHeight > el.clientHeight
241
+ } else {
242
+ this.isOverflow = el.scrollWidth > el.clientWidth
243
+ }
244
+ },
245
+ extractSlotText(nodes) {
246
+ let text = ''
247
+ nodes.forEach(node => {
248
+ if (typeof node.children === 'string') {
249
+ text += node.children
250
+ } else if (typeof node.text === 'string') {
251
+ text += node.text
252
+ } else if (node.children && Array.isArray(node.children)) {
253
+ text += this.extractSlotText(node.children)
254
+ }
255
+ })
256
+ return text.trim()
257
+ }
258
+ },
259
+ mounted() {
260
+ if (this.ellipsis && this.showTooltip) {
261
+ this.$nextTick(() => {
262
+ this.checkOverflow()
263
+ })
264
+ }
265
+ this._resizeObserver = new ResizeObserver(() => {
266
+ if (this.ellipsis && this.showTooltip) {
267
+ this.checkOverflow()
268
+ }
269
+ })
270
+ this.$nextTick(() => {
271
+ if (this.$refs.textRef) {
272
+ this._resizeObserver.observe(this.$refs.textRef)
273
+ }
274
+ })
275
+ },
276
+ beforeDestroy() {
277
+ if (this._resizeObserver) {
278
+ this._resizeObserver.disconnect()
279
+ }
280
+ },
281
+ watch: {
282
+ value() {
283
+ if (this.ellipsis && this.showTooltip) {
284
+ this.$nextTick(() => {
285
+ this.checkOverflow()
286
+ })
287
+ }
288
+ },
289
+ ellipsis(newVal) {
290
+ if (newVal && this.showTooltip) {
291
+ this.$nextTick(() => {
292
+ this.checkOverflow()
293
+ })
294
+ }
295
+ }
166
296
  }
167
297
  }
168
298
  </script>