Skip to content

Commit e28d022

Browse files
gnerkusmontezume
authored andcommitted
fix(localizedmoneyinput): remove 'amount/currencyCode' from event name (#419)
1 parent 7bbc6c6 commit e28d022

File tree

5 files changed

+213
-73
lines changed

5 files changed

+213
-73
lines changed

src/components/inputs/localized-money-input/README.md

+41-15
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ states.
1111
import { LocalizedMoneyInput } from '@commercetools-frontend/ui-kit';
1212

1313
<LocalizedMoneyInput
14-
value={{ USD: '12.22', EUR: '41.44' }}
14+
value={{
15+
USD: { currencyCode: 'USD', amount: '12.22' },
16+
EUR: { currencyCode: 'EUR', amount: '41.44' },
17+
}}
1518
onChange={event => alert(event.target.name, event.target.value)}
1619
/>;
1720
```
@@ -22,7 +25,7 @@ import { LocalizedMoneyInput } from '@commercetools-frontend/ui-kit';
2225
| ------------------------------- | ---------------- | :------: | ---------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
2326
| `id` | `string` | - | - | - | Used as prefix of HTML `id` property. Each input field id will have the currency as a suffix (`${idPrefix}.${lang}`), e.g. `foo.en` |
2427
| `name` | `string` | - | - | - | Used as HTML `name` property for each input field. Each input field name will have the currency as a suffix (`${namePrefix}.${lang}`), e.g. `foo.en` |
25-
| `value` | `object` || - | - | Values to use. Keyed by currency, the values are the actual values, e.g. `{ USD: '12.22', EUR: '41.44' }` |
28+
| `value` | `object` || - | - | Values to use. Keyed by currency, the values are the money values, e.g. `{ USD: {currencyCode: 'USD', amount: '12.22'}, EUR: {currencyCode: 'EUR', amount: '41.44'} }` |
2629
| `onChange` | `function` || - | - | Gets called when any input is changed. Is called with the change event of the changed input. |
2730
| `selectedCurrency` | `string` || - | - | Specifies which currency will be shown in case the `LocalizedMoneyInput` is collapsed. |
2831
| `onBlur` | `function` | - | - | - | Called when any field is blurred. Is called with the `event` of that field. |
@@ -85,12 +88,18 @@ LocalizedMoneyInput.getEmptyCurrencies({});
8588
```
8689

8790
```js
88-
LocalizedMoneyInput.getEmptyCurrencies({ USD: '', EUR: '' });
91+
LocalizedMoneyInput.getEmptyCurrencies({
92+
USD: { currencyCode: 'USD', amount: '' },
93+
EUR: { currencyCode: 'EUR', amount: '' },
94+
});
8995
// -> ['USD', 'EUR']
9096
```
9197

9298
```js
93-
LocalizedMoneyInput.getEmptyCurrencies({ USD: '12.43', EUR: '' });
99+
LocalizedMoneyInput.getEmptyCurrencies({
100+
USD: { currencyCode: 'USD', amount: '12.43' },
101+
EUR: { currencyCode: 'EUR', amount: '' },
102+
});
94103
// -> ['EUR']
95104
```
96105

@@ -105,16 +114,28 @@ LocalizedMoneyInput.getHighPrecisionCurrencies({});
105114

106115
```js
107116
LocalizedMoneyInput.getHighPrecisionCurrencies({
108-
USD: '12.2221',
109-
EUR: '9.9999',
117+
USD: {
118+
currencyCode: 'USD',
119+
amount: '12.2221',
120+
},
121+
EUR: {
122+
currencyCode: 'EUR',
123+
amount: '9.9999',
124+
},
110125
});
111126
// -> ['USD', 'EUR']
112127
```
113128

114129
```js
115130
LocalizedMoneyInput.getHighPrecisionCurrencies({
116-
USD: '12.43',
117-
EUR: '0.00001',
131+
USD: {
132+
currencyCode: 'USD',
133+
amount: '12.43',
134+
},
135+
EUR: {
136+
currencyCode: 'EUR',
137+
amount: '0.00001',
138+
},
118139
});
119140
// -> ['EUR']
120141
```
@@ -153,16 +174,21 @@ Here are examples of `centPrecision` and `highPrecision` prices.
153174

154175
##### `LocalizedMoneyInput.parseMoneyValues`
155176

156-
The `parseMoneyValues` function will turn a [`MoneyValue`](https://docs.commercetools.com/http-api-types#money) into a value the LocalizedMoneyInput component can handle `({ currencyCode: amount })`.
177+
The `parseMoneyValues` function will turn a [`MoneyValue`](https://docs.commercetools.com/http-api-types#money) into a value the LocalizedMoneyInput component can handle `({ [currencyCode]: {currencyCode, amount} })`.
157178

158179
##### `LocalizedMoneyInput.getEmptyCurrencies`
159180

160-
The `getEmptyCurrencies` function will return array of crrencies that don't have amount .
181+
The `getEmptyCurrencies` function will return array of currencies that don't have amount .
161182

162183
```js
163-
LocalizedMoneyInput.getEmptyCurrencies({ EUR: '', USD: '12.77' }); // -> ['EUR']
164-
165-
LocalizedMoneyInput.getEmptyCurrencies({ EUR: '12.77' }); // -> []
184+
LocalizedMoneyInput.getEmptyCurrencies({
185+
EUR: { currencyCode: 'EUR', amount: '' },
186+
USD: { currencyCode: 'USD', amount: '12.77' },
187+
}); // -> ['EUR']
188+
189+
LocalizedMoneyInput.getEmptyCurrencies({
190+
EUR: { currencyCode: 'EUR', amount: '12.77' },
191+
}); // -> []
166192
```
167193

168194
### Example
@@ -219,8 +245,8 @@ const validate = formValues => {
219245
const emptyCurrencies = LocalizedMoneyInput.getEmptyCurrencies(
220246
formValues.prices
221247
);
222-
// ['ERU', 'USD']
223-
// This form dosen't accept highprecision prices
248+
// ['EUR', 'USD']
249+
// This form doesn't accept high precision prices
224250
const highPrecisionCurrencies = LocalizedMoneyInput.getHighPrecisionCurrencies(
225251
formValues.prices
226252
);

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

+14-23
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ export class LocalizedMoneyInput extends React.Component {
111111
name: PropTypes.string,
112112
// then input doesn't accept a "currencies" prop, instead all possible
113113
// currencies have to exist (with empty or filled strings) on the value:
114-
// { EUR: '12.00', USD: '14.23', CAD: '13.70' }
115-
value: PropTypes.objectOf(PropTypes.string).isRequired,
114+
// { EUR: {amount: '12.00', currencyCode: 'EUR'}, USD: {amount: '', currencyCode: 'USD'}}
115+
value: PropTypes.objectOf(
116+
PropTypes.shape({
117+
amount: PropTypes.string.isRequired,
118+
currencyCode: PropTypes.string.isRequired,
119+
})
120+
).isRequired,
116121
onChange: PropTypes.func,
117122
selectedCurrency: PropTypes.string.isRequired,
118123
onBlur: PropTypes.func,
@@ -154,38 +159,27 @@ export class LocalizedMoneyInput extends React.Component {
154159
};
155160

156161
static convertToMoneyValues = values =>
157-
Object.keys(values).map(currencyCode =>
158-
MoneyInput.convertToMoneyValue({
159-
currencyCode,
160-
amount: values[currencyCode],
161-
})
162-
);
162+
Object.values(values).map(value => MoneyInput.convertToMoneyValue(value));
163163

164164
static parseMoneyValues = (moneyValues = [], locale) =>
165165
moneyValues
166166
.map(value => MoneyInput.parseMoneyValue(value, locale))
167167
.reduce(
168168
(pairs, value) => ({
169169
...pairs,
170-
[value.currencyCode]: value.amount,
170+
[value.currencyCode]: value,
171171
}),
172172
{}
173173
);
174174

175175
static getHighPrecisionCurrencies = values =>
176-
Object.keys(values).filter(currency =>
177-
MoneyInput.isHighPrecision({
178-
currencyCode: currency,
179-
amount: values[currency],
180-
})
176+
Object.keys(values).filter(currencyCode =>
177+
MoneyInput.isHighPrecision(values[currencyCode])
181178
);
182179

183180
static getEmptyCurrencies = values =>
184-
Object.keys(values).filter(currency =>
185-
MoneyInput.isEmpty({
186-
currencyCode: currency,
187-
amount: values[currency],
188-
})
181+
Object.keys(values).filter(currencyCode =>
182+
MoneyInput.isEmpty(values[currencyCode])
189183
);
190184

191185
state = {
@@ -255,10 +249,7 @@ export class LocalizedMoneyInput extends React.Component {
255249
key={currency}
256250
id={LocalizedMoneyInput.getId(this.state.id, currency)}
257251
name={LocalizedMoneyInput.getName(this.props.name, currency)}
258-
value={{
259-
currencyCode: currency,
260-
amount: this.props.value[currency],
261-
}}
252+
value={this.props.value[currency]}
262253
onChange={this.props.onChange}
263254
currency={currency}
264255
placeholder={

0 commit comments

Comments
 (0)