Skip to content

Commit

Permalink
Merge pull request #81 from indec-it/fix/currencyValue
Browse files Browse the repository at this point in the history
fix(currency): fix currency value
  • Loading branch information
maximilianoforlenza authored Aug 29, 2023
2 parents 8f79701 + c34e5a7 commit 35869b3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@indec/form-builder",
"version": "2.4.0",
"version": "2.4.1",
"description": "Form builder",
"main": "index.js",
"private": false,
Expand Down
5 changes: 3 additions & 2 deletions src/components/Currency/Currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import TextField from '../TextField';

function Currency({form, field, label, ...props}) {
const handleChange = values => {
const {formattedValue} = values;
form.setFieldValue(field.name, formattedValue);
const {floatValue} = values;
form.setFieldValue(field.name, floatValue);
form.setFieldTouched(field.name, false);
};

Expand All @@ -18,6 +18,7 @@ function Currency({form, field, label, ...props}) {
onValueChange={handleChange}
thousandSeparator="."
decimalSeparator=","
isNumericString={false}
customInput={TextField}
form={form}
field={field}
Expand Down
8 changes: 4 additions & 4 deletions src/components/Currency/Currency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ describe('<Currency>', () => {
fireEvent.change(textField, {target: {name: props.field.name, value: '1000'}});
});

it('should fire `props.field.onChange with thousand separator`', () => {
it('should fire `props.field.onChange without thousand separator`', () => {
expect(props.form.setFieldValue).toHaveBeenCalledTimes(1);
expect(props.form.setFieldValue).toHaveBeenCalledWith(props.field.name, '1.000');
expect(props.form.setFieldValue).toHaveBeenCalledWith(props.field.name, 1000);
});
});

Expand All @@ -34,9 +34,9 @@ describe('<Currency>', () => {
fireEvent.change(textField, {target: {name: props.field.name, value: '1000,21'}});
});

it('should fire `props.field.onChange with thousand and decimal separator`', () => {
it('should fire `props.field.onChange without thousand and decimal separator`', () => {
expect(props.form.setFieldValue).toHaveBeenCalledTimes(1);
expect(props.form.setFieldValue).toHaveBeenCalledWith(props.field.name, '1.000,21');
expect(props.form.setFieldValue).toHaveBeenCalledWith(props.field.name, 1000.21);
});
});
});
25 changes: 1 addition & 24 deletions src/utils/getValidationRules.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
import validationTypes from '@/constants/validationTypes';

import operations from './operations';

const convertString = input => {
const withoutThousands = input.replace(/\./g, '');
const withDecimalPoint = withoutThousands.replace(',', '.');
const convertedNumber = parseFloat(withDecimalPoint);
return convertedNumber;
};

const getConditions = ({conditions, answers}) => conditions.map(
condition => {
if (!Object.prototype.hasOwnProperty.call(answers, condition.question)) {
return false;
}
const question = answers[condition.question];
let value = question?.answer?.value;
if (
[
validationTypes.GREATER_OR_EQUAL_TO,
validationTypes.GREATER_THAN,
validationTypes.LESS_THAN,
validationTypes.LESS_THAN_OR_EQUAL_TO,
validationTypes.NOT_EQUAL,
validationTypes.EQUAL
].includes(condition.type)
&& typeof condition.value === 'number'
&& typeof value === 'string'
) {
value = convertString(value);
}
const value = question?.answer?.value;
return operations[condition.type](typeof value === 'number' ? value : value || '', condition.value);
}
);
Expand Down

0 comments on commit 35869b3

Please sign in to comment.