Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NcCounterBubble): show original count in title when shortened #6395

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(NcCounterBubble): show original count in title when shortened
Signed-off-by: Grigorii K. Shartsev <[email protected]>
ShGKme committed Jan 19, 2025
commit 78cd95f274f635ebd1ae4891251cabb4c96f31af
22 changes: 17 additions & 5 deletions src/components/NcCounterBubble/NcCounterBubble.vue
Original file line number Diff line number Diff line change
@@ -243,7 +243,7 @@ export default {
methods: {
humanizeCount(count) {
if (this.raw) {
return count
return count.toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this but this is also fine for me :)

Suggested change
return count.toString()
return String(count)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer .toString() as a safer method. If something went wrong and count is invalid, e.g. null or undefined, .toString() fails explicitly while String constructor successfully created string 'undefined' which goes to UI or further to computations.

}

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

/**
* Get the humanized count from `count` prop
* @return {string | undefined}
* @return {{ humanized: string, original: string} | undefined}
*/
getHumanizedCount() {
// If we have count prop - just render from count
if (this.count !== undefined) {
return this.humanizedCount
return {
humanized: this.humanizedCount,
original: this.count.toString(),
}
}

// Raw value - render as it is
@@ -274,17 +277,26 @@ export default {
const slotContent = this.$slots.default[0].text?.trim()
if (slotContent && /^\d+$/.test(slotContent)) {
const count = parseInt(slotContent, 10)
return this.humanizeCount(count)
return {
humanized: this.humanizeCount(count),
original: slotContent,
}
}
}
},
},

render(h) {
const count = this.getHumanizedCount()

return h('div', {
staticClass: 'counter-bubble__counter',
class: this.counterClassObject,
}, [this.getHumanizedCount() ?? this.$slots.default])
attrs: {
// Show original count in title if humanized
title: count && count.original !== count.humanized ? count.original : undefined,
},
}, [count?.humanized ?? this.$slots.default])
},
}

16 changes: 13 additions & 3 deletions tests/unit/components/NcCounterBubble/NcCounterBubble.spec.js
Original file line number Diff line number Diff line change
@@ -21,24 +21,34 @@ describe('NcCounterBubble', () => {
})

describe('with humanization', () => {
it('should render count 1020 with humanization as "1K"', () => {
it('should render count 1042 with humanization as "1K" and original count in the title', () => {
const wrapper = mount(NcCounterBubble, { propsData: { count: 1042 } })
expect(wrapper.text()).toBe('1K')
expect(wrapper.attributes('title')).toBe('1042')
})

it('should render count 12 without humanization and without title', () => {
const wrapper = mount(NcCounterBubble, { propsData: { count: 12 } })
expect(wrapper.text()).toBe('12')
expect(wrapper.attributes('title')).toBeUndefined()
})

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

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

it('should render slot content 1020 as it is with raw prop', () => {
it('should render slot content 1042 as it is with raw prop', () => {
const wrapper = mount(NcCounterBubble, { propsData: { raw: true }, slots: { default: '1042' } })
expect(wrapper.text()).toBe('1042')
expect(wrapper.attributes('title')).toBeUndefined()
})
})


Unchanged files with check annotations Beta

isRTL: isRTL(),
}
},
mixins: [ActionTextMixin],

Check warning on line 392 in src/components/NcActionButton/NcActionButton.vue

GitHub Actions / NPM lint

The "mixins" property should be above the "setup" property on line 387
inject: {

Check warning on line 394 in src/components/NcActionButton/NcActionButton.vue

GitHub Actions / NPM lint

The "inject" property should be above the "setup" property on line 387
isInSemanticMenu: {
from: 'NcActions:isSemanticMenu',
default: false,
},
},
props: {

Check warning on line 401 in src/components/NcActionButton/NcActionButton.vue

GitHub Actions / NPM lint

The "props" property should be above the "setup" property on line 387
/**
* @deprecated To be removed in @nextcloud/vue 9. Migration guide: remove ariaHidden prop from NcAction* components.
* @todo Add a check in @nextcloud/vue 9 that this prop is not provided,
return this.$refs.menu.querySelector('li.active')
},
/**
* @return {NodeListOf<HTMLElement>}

Check warning on line 1523 in src/components/NcActions/NcActions.vue

GitHub Actions / NPM lint

The type 'NodeListOf' is undefined
*/
getFocusableMenuItemElements() {
return this.$refs.menu.querySelectorAll(focusableSelector)
<template>
<span ref="main"
:title="tooltip"
v-click-outside="closeMenu"

Check warning on line 157 in src/components/NcAvatar/NcAvatar.vue

GitHub Actions / NPM lint

Attribute "v-click-outside" should go before ":title"
:class="{
'avatardiv--unknown': userDoesNotExist,
'avatardiv--with-menu': hasMenu,
listeners() {
return {
...this.$listeners,
/**

Check warning on line 303 in src/components/NcDateTimePickerNative/NcDateTimePickerNative.vue

GitHub Actions / NPM lint

JSDoc @return declaration present but return expression not available in function
* Handle the input event
*
* @param {InputEvent} $event input event payload