Skip to content

Commit 6f922d9

Browse files
islam3zzattdeekens
authored andcommitted
fix(money-input): parse money value in MoneyInput (#597)
* fix(money-input): use provided locale if povided in parsing value * test(money-input): add demo for updating initial values for parse money value
1 parent e124de8 commit 6f922d9

File tree

2 files changed

+77
-63
lines changed

2 files changed

+77
-63
lines changed

src/components/inputs/money-input/money-input.form.story.js

+76-62
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Formik } from 'formik';
33
import { storiesOf } from '@storybook/react';
44
import { action } from '@storybook/addon-actions';
55
import { injectIntl } from 'react-intl';
6-
import { withKnobs } from '@storybook/addon-knobs';
6+
import { withKnobs, number } from '@storybook/addon-knobs';
77
import withReadme from 'storybook-readme/with-readme';
88
import omitEmpty from 'omit-empty';
99
import ErrorMessage from '../../messages/error-message';
@@ -25,68 +25,82 @@ const validate = (formValues, locale) => {
2525
}
2626
return omitEmpty(errors);
2727
};
28-
const Story = injectIntl(props => (
29-
<Section>
30-
<Formik
31-
initialValues={{ price: { currencyCode: '', amount: '' } }}
32-
validate={formValues => validate(formValues, props.intl.locale)}
33-
onSubmit={(values, formik) => {
34-
// eslint-disable-next-line no-console
35-
console.log(
36-
'money value',
37-
MoneyInput.convertToMoneyValue(values.price, props.intl.locale)
38-
);
39-
action('onSubmit')(values, formik);
40-
formik.resetForm(values);
41-
}}
42-
render={formik => (
43-
<Spacings.Stack scale="l">
44-
<Spacings.Stack scale="s">
45-
<MoneyInput
46-
name="price"
47-
currencies={['EUR', 'USD', 'AED', 'KWD', 'JPY']}
48-
value={formik.values.price}
49-
onChange={formik.handleChange}
50-
onBlur={formik.handleBlur}
51-
hasError={
52-
MoneyInput.isTouched(formik.touched.price) &&
53-
Boolean(formik.errors.price)
54-
}
55-
horizontalConstraint="m"
56-
/>
57-
{MoneyInput.isTouched(formik.touched.price) &&
58-
formik.errors.price &&
59-
formik.errors.price.missing && (
60-
<ErrorMessage>Missing price</ErrorMessage>
61-
)}
62-
{MoneyInput.isTouched(formik.touched.price) &&
63-
formik.errors.price &&
64-
formik.errors.price.unsupportedHighPrecision && (
65-
<ErrorMessage>
66-
This value is a high precision value. High precision pricing
67-
is not supported for products.
68-
</ErrorMessage>
69-
)}
28+
const Story = injectIntl(props => {
29+
const initialCentAmount = number('initial cent amount', 10099);
30+
const initialFractionDigits = number('initial fraction digits', 2);
31+
return (
32+
<Section>
33+
<Formik
34+
initialValues={{
35+
price: MoneyInput.parseMoneyValue(
36+
{
37+
centAmount: initialCentAmount,
38+
currencyCode: 'EUR',
39+
fractionDigits: initialFractionDigits,
40+
type: 'centPrecision',
41+
},
42+
props.intl.locale
43+
),
44+
}}
45+
validate={formValues => validate(formValues, props.intl.locale)}
46+
onSubmit={(values, formik) => {
47+
// eslint-disable-next-line no-console
48+
console.log(
49+
'money value',
50+
MoneyInput.convertToMoneyValue(values.price, props.intl.locale)
51+
);
52+
action('onSubmit')(values, formik);
53+
formik.resetForm(values);
54+
}}
55+
render={formik => (
56+
<Spacings.Stack scale="l">
57+
<Spacings.Stack scale="s">
58+
<MoneyInput
59+
name="price"
60+
currencies={['EUR', 'USD', 'AED', 'KWD', 'JPY']}
61+
value={formik.values.price}
62+
onChange={formik.handleChange}
63+
onBlur={formik.handleBlur}
64+
hasError={
65+
MoneyInput.isTouched(formik.touched.price) &&
66+
Boolean(formik.errors.price)
67+
}
68+
horizontalConstraint="m"
69+
/>
70+
{MoneyInput.isTouched(formik.touched.price) &&
71+
formik.errors.price &&
72+
formik.errors.price.missing && (
73+
<ErrorMessage>Missing price</ErrorMessage>
74+
)}
75+
{MoneyInput.isTouched(formik.touched.price) &&
76+
formik.errors.price &&
77+
formik.errors.price.unsupportedHighPrecision && (
78+
<ErrorMessage>
79+
This value is a high precision value. High precision pricing
80+
is not supported for products.
81+
</ErrorMessage>
82+
)}
83+
</Spacings.Stack>
84+
<Spacings.Inline>
85+
<SecondaryButton
86+
onClick={formik.handleReset}
87+
isDisabled={formik.isSubmitting}
88+
label="Reset"
89+
/>
90+
<PrimaryButton
91+
onClick={formik.handleSubmit}
92+
isDisabled={formik.isSubmitting || !formik.dirty}
93+
label="Submit"
94+
/>
95+
</Spacings.Inline>
96+
<hr />
97+
<FormikBox formik={formik} />
7098
</Spacings.Stack>
71-
<Spacings.Inline>
72-
<SecondaryButton
73-
onClick={formik.handleReset}
74-
isDisabled={formik.isSubmitting}
75-
label="Reset"
76-
/>
77-
<PrimaryButton
78-
onClick={formik.handleSubmit}
79-
isDisabled={formik.isSubmitting || !formik.dirty}
80-
label="Submit"
81-
/>
82-
</Spacings.Inline>
83-
<hr />
84-
<FormikBox formik={formik} />
85-
</Spacings.Stack>
86-
)}
87-
/>
88-
</Section>
89-
));
99+
)}
100+
/>
101+
</Section>
102+
);
103+
});
90104
storiesOf('Examples|Forms/Inputs', module)
91105
.addDecorator(withKnobs)
92106
.addDecorator(withReadme(Readme))

src/components/inputs/money-input/money-input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class MoneyInput extends React.Component {
340340
);
341341

342342
const amount = formatAmount(
343-
String(getAmountAsNumberFromMoneyValue(moneyValue)),
343+
getAmountAsNumberFromMoneyValue(moneyValue).toLocaleString(locale),
344344
moneyValue.currencyCode,
345345
locale
346346
);

0 commit comments

Comments
 (0)