Skip to content

Commit

Permalink
fixed thousandSeparator problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenMasterJacob20011 authored and lane-formio committed Jul 24, 2024
1 parent 2a7c911 commit 74964da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/components/number/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ export default class NumberComponent extends Input {
|| separators.decimalSeparator;

if (this.component.delimiter) {
if (this.options.hasOwnProperty('thousandsSeparator')) {
console.warn("Property 'thousandsSeparator' is deprecated. Please use i18n to specify delimiter.");
}

this.delimiter = this.options.properties?.thousandsSeparator || this.options.thousandsSeparator || separators.delimiter;
this.delimiter = this.component.thousandsSeparator || this.options.properties?.thousandsSeparator || this.options.thousandsSeparator || separators.delimiter;
}
else {
if (this.component.thousandsSeparator || this.options.properties?.thousandsSeparator || this.options.thousandsSeparator){
console.warn('In order for thousands separator to work properly, you must set the delimiter to true in the component json');
}
this.delimiter = '';
}

Expand All @@ -92,7 +91,7 @@ export default class NumberComponent extends Input {
prefix: '',
suffix: '',
requireDecimal: _.get(this.component, 'requireDecimal', false),
thousandsSeparatorSymbol: _.get(this.component, 'thousandsSeparator', this.delimiter),
thousandsSeparatorSymbol: this.delimiter || '',
decimalSymbol: _.get(this.component, 'decimalSymbol', this.decimalSeparator),
decimalLimit: _.get(this.component, 'decimalLimit', this.decimalLimit),
allowNegative: _.get(this.component, 'allowNegative', true),
Expand Down Expand Up @@ -171,6 +170,15 @@ export default class NumberComponent extends Input {
return super.setValueAt(index, this.formatValue(this.parseValue(value)), flags);
}

/**
* Converts a string to a floating point number, formats the number based on the parsed float function
* (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) and then returns the
* formatted number back as a string
* Example Input: "123.456,22"
* Example Output: "123456,22"
* @param {string | number} input the numeric string to parse
* @returns {string | null} a parsed string
*/
parseValue(input) {
if (typeof input === 'string') {
input = input.split(this.delimiter).join('').replace(this.decimalSeparator, '.');
Expand Down
24 changes: 23 additions & 1 deletion src/components/number/Number.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {
comp5,
comp6,
comp7,
comp8
comp8,
comp9
} from './fixtures';
import CurrencyComponent from "../currency/Currency";

describe('Number Component', () => {
it('Should build an number component', () => {
Expand Down Expand Up @@ -441,6 +443,26 @@ describe('Number Component', () => {
});
});

it('Should remove thousands separator in parseValue function if set on component JSON', () => {
const numberComponent = new NumberComponent({thousandsSeparator: '.', decimalSymbol: ',', delimiter: true});
assert.equal(numberComponent.parseValue('123.456.789,1'), '123456789,1');
});

it('Should use a . thousands separator when delimiter is true and thousands separator is set to .', (done) => {
Formio.createForm(document.createElement('div'), comp9, {}).then((form) => {
const numberComponent = form.getComponent('number');
const inputEvent = new Event('input');
const blurEvent = new Event('blur');
numberComponent.refs.input[0].value = '111222333';
numberComponent.refs.input[0].dispatchEvent(inputEvent);
numberComponent.refs.input[0].dispatchEvent(blurEvent);
setTimeout(()=>{
assert.equal(numberComponent.refs.input[0].value, '111.222.333');
done();
},200)
})
});

// it('Should add trailing zeros on blur, if decimal required', (done) => {
// const comp = _.cloneDeep(comp3);
//
Expand Down
19 changes: 19 additions & 0 deletions src/components/number/fixtures/comp9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
components: [
{
"label": "Number",
"applyMaskOn": "change",
"mask": false,
"tableView": false,
"delimiter": true,
"requireDecimal": false,
"inputFormat": "plain",
"truncateMultipleSpaces": false,
"key": "number",
"type": "number",
"input": true,
"decimalSymbol": ",",
"thousandsSeparator": "."
}
]
}
1 change: 1 addition & 0 deletions src/components/number/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export comp5 from './comp5';
export comp6 from './comp6';
export comp7 from './comp7';
export comp8 from './comp8';
export comp9 from './comp9';

0 comments on commit 74964da

Please sign in to comment.