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(currency): fix currency value #81

Merged
merged 1 commit into from
Aug 29, 2023
Merged
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
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