Skip to content
Open
Show file tree
Hide file tree
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
95 changes: 95 additions & 0 deletions cypress/component/TableCellNumber.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { setLocale } from '@nextcloud/l10n'

import TableCellNumber from '../../src/shared/components/ncTable/partials/TableCellNumber.vue'
import NumberForm from '../../src/shared/components/ncTable/partials/rowTypePartials/NumberForm.vue'

describe('TableCellNumber', () => {
beforeEach(() => {
setLocale('en_US')
})

it('displays decimals with the current locale separator', () => {
setLocale('de_DE')

mountNumberCell(1.234, { numberDecimals: 3 })

cy.get('.number-display').should('contain.text', '1,234')
})

it('keeps the configured amount of decimal places', () => {
mountNumberCell(1.2, { numberDecimals: 2 })

cy.get('.number-display').should('contain.text', '1.20')
})

it('clamps out-of-range decimal counts for Intl.NumberFormat', () => {
mountNumberCell(1.2, { numberDecimals: 101 })

cy.get('.number-display').invoke('text').should((text) => {
expect(text).to.match(/^1\.20+$/)
expect(text.trim().split('.')[1]).to.have.length(100)
})
})

it('clamps out-of-range decimal counts when editing row values', () => {
mountRowNumberForm(1.2, { numberDecimals: 101 }).then(({ wrapper }) => {
expect(wrapper.vm.numberFractionDigits).to.equal(100)
expect(wrapper.vm.parseValue('1.234')).to.equal(1.234)
expect(wrapper.vm.getStep).to.match(/^\.0+1$/)

const step = wrapper.vm.getStep
expect(step.slice(1, -1)).to.have.length(99)
})
})
})

function mountNumberCell(value, columnOverrides = {}) {
cy.mount(TableCellNumber, {
props: {
column: {
id: 1,
numberDecimals: 0,
numberMax: null,
numberMin: null,
numberPrefix: '',
numberSuffix: '',
...columnOverrides,
},
rowId: 1,
value,
},
})
}

function mountRowNumberForm(value, columnOverrides = {}) {
return cy.mount(NumberForm, {
props: {
column: {
id: 1,
title: 'Amount',
description: '',
mandatory: false,
numberDecimals: 0,
numberDefault: undefined,
numberMax: null,
numberMin: null,
numberPrefix: '',
numberSuffix: '',
viewColumnInformation: {},
...columnOverrides,
},
value,
},
global: {
components: {
RowFormWrapper: {
template: '<div><slot /></div>',
},
},
},
})
}
12 changes: 11 additions & 1 deletion src/shared/components/ncTable/partials/TableCellNumber.vue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need to modify NumberForm.vue too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. I added min="0" max="100" step="1" to the column NumberForm.vue decimals input in b59aa28, so normal edits stay inside the same range.

I also checked the row value editor and found the same invalid persisted-data risk there: it used raw numberDecimals for both String.repeat() and toFixed(). The same commit now normalizes decimals to 0..100 before both getStep and parseValue, with Cypress coverage for numberDecimals: 101 on the row form path.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</template>

<script>
import { getCanonicalLocale } from '@nextcloud/l10n'
import cellEditMixin from '../mixins/cellEditMixin.js'

export default {
Expand All @@ -42,7 +43,16 @@ export default {
if (this.value === null) {
return null
}
return this.value.toFixed(this.column?.numberDecimals)
// Intl.NumberFormat only accepts 0..100 fraction digits
const raw = Number(this.column?.numberDecimals ?? 0)
const fractionDigits = Number.isFinite(raw)
? Math.min(100, Math.max(0, Math.trunc(raw)))
: 0
return new Intl.NumberFormat(getCanonicalLocale(), {
maximumFractionDigits: fractionDigits,
minimumFractionDigits: fractionDigits,
useGrouping: false,
}).format(this.value)
},

getStep() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{{ t('tables', 'Decimals') }}
</div>
<div class="fix-col-4">
<input v-model="mutableColumn.numberDecimals" type="number">
<input v-model="mutableColumn.numberDecimals" type="number" min="0" max="100" step="1">
</div>
</div>
<div v-if="defaultValueErrorHint !== ''" class="col-4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ export default {
'update:value',
],
computed: {
numberFractionDigits() {
const raw = Number(this.column?.numberDecimals ?? 0)
return Number.isFinite(raw)
? Math.min(100, Math.max(0, Math.trunc(raw)))
: 0
},
getStep() {
if (this.column?.numberDecimals === 0) {
if (this.numberFractionDigits === 0) {
return '1'
} else if (this.column?.numberDecimals > 0) {
return '.' + '0'.repeat(this.column.numberDecimals - 1) + '1'
} else if (this.numberFractionDigits > 0) {
return '.' + '0'.repeat(this.numberFractionDigits - 1) + '1'
} else {
return 'any'
}
Expand Down Expand Up @@ -95,7 +101,7 @@ export default {
} else {
parsedValue = inputValue
}
const roundedValue = parsedValue.toFixed(this.column?.numberDecimals)
const roundedValue = parsedValue.toFixed(this.numberFractionDigits)
let value = parseFloat(roundedValue)
if ((this.column?.numberMin !== null && this.column?.numberMin !== undefined) && value < this.column?.numberMin) {
value = this.column.numberMin
Expand Down