Skip to content

Commit 9ede29f

Browse files
authored
Merge pull request #6395 from nextcloud-libraries/fix/NcCounterBubble--title
fix(NcCounterBubble): show original count in title when shortened
2 parents 25f0661 + 78cd95f commit 9ede29f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/components/NcCounterBubble/NcCounterBubble.vue

+17-5
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export default {
243243
methods: {
244244
humanizeCount(count) {
245245
if (this.raw) {
246-
return count
246+
return count.toString()
247247
}
248248

249249
const formatter = new Intl.NumberFormat(getCanonicalLocale(), {
@@ -256,12 +256,15 @@ export default {
256256

257257
/**
258258
* Get the humanized count from `count` prop
259-
* @return {string | undefined}
259+
* @return {{ humanized: string, original: string} | undefined}
260260
*/
261261
getHumanizedCount() {
262262
// If we have count prop - just render from count
263263
if (this.count !== undefined) {
264-
return this.humanizedCount
264+
return {
265+
humanized: this.humanizedCount,
266+
original: this.count.toString(),
267+
}
265268
}
266269

267270
// Raw value - render as it is
@@ -274,17 +277,26 @@ export default {
274277
const slotContent = this.$slots.default[0].text?.trim()
275278
if (slotContent && /^\d+$/.test(slotContent)) {
276279
const count = parseInt(slotContent, 10)
277-
return this.humanizeCount(count)
280+
return {
281+
humanized: this.humanizeCount(count),
282+
original: slotContent,
283+
}
278284
}
279285
}
280286
},
281287
},
282288

283289
render(h) {
290+
const count = this.getHumanizedCount()
291+
284292
return h('div', {
285293
staticClass: 'counter-bubble__counter',
286294
class: this.counterClassObject,
287-
}, [this.getHumanizedCount() ?? this.$slots.default])
295+
attrs: {
296+
// Show original count in title if humanized
297+
title: count && count.original !== count.humanized ? count.original : undefined,
298+
},
299+
}, [count?.humanized ?? this.$slots.default])
288300
},
289301
}
290302

tests/unit/components/NcCounterBubble/NcCounterBubble.spec.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,34 @@ describe('NcCounterBubble', () => {
2121
})
2222

2323
describe('with humanization', () => {
24-
it('should render count 1020 with humanization as "1K"', () => {
24+
it('should render count 1042 with humanization as "1K" and original count in the title', () => {
2525
const wrapper = mount(NcCounterBubble, { propsData: { count: 1042 } })
2626
expect(wrapper.text()).toBe('1K')
27+
expect(wrapper.attributes('title')).toBe('1042')
28+
})
29+
30+
it('should render count 12 without humanization and without title', () => {
31+
const wrapper = mount(NcCounterBubble, { propsData: { count: 12 } })
32+
expect(wrapper.text()).toBe('12')
33+
expect(wrapper.attributes('title')).toBeUndefined()
2734
})
2835

2936
it('should not humanize with raw', () => {
3037
const wrapper = mount(NcCounterBubble, { propsData: { count: 1042, raw: true } })
3138
expect(wrapper.text()).toBe('1042')
39+
expect(wrapper.attributes('title')).toBeUndefined()
3240
})
3341

34-
it('should render slot content 1020 with humanization as "1K"', () => {
42+
it('should render slot content 1042 with humanization as "1K" and original count in the title', () => {
3543
const wrapper = mount(NcCounterBubble, { slots: { default: '1042' } })
3644
expect(wrapper.text()).toBe('1K')
45+
expect(wrapper.attributes('title')).toBe('1042')
3746
})
3847

39-
it('should render slot content 1020 as it is with raw prop', () => {
48+
it('should render slot content 1042 as it is with raw prop', () => {
4049
const wrapper = mount(NcCounterBubble, { propsData: { raw: true }, slots: { default: '1042' } })
4150
expect(wrapper.text()).toBe('1042')
51+
expect(wrapper.attributes('title')).toBeUndefined()
4252
})
4353
})
4454

0 commit comments

Comments
 (0)