-
Notifications
You must be signed in to change notification settings - Fork 26
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(localizedmoneyinput): remove 'amount/currencyCode' from event name #419
Conversation
// "price.EUR.amount" | ||
// | ||
// eslint-disable-next-line no-param-reassign | ||
event.target.name = event.target.name.replace(/(.*)(\..*)$/, '$1'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably change the element name instead as we also need these event names on the blur event.
You should be able to change the implementation of LocalizedMoneyInput.getName
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LocalizedMoneyInput.getName
works correctly:
export const getName = (namePrefix, language) =>
namePrefix ? `${namePrefix}.${language}` : undefined;
The .amount
and .currencyCode
suffices come from the MoneyInput
.
To change the element name would require a change to the elements contained in the MoneyInput
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, got it now! To me this is an indication that our form values might still be a bit off (or maybe they are not and this is the best we can do).
Give this approach a thought for bit:
I assume our form value currently looks like this:
{
EUR: "12.50",
AUD: "34.60"
}
If we changed the form value to
{
EUR: { currencyCode: "EUR", amount: "12.50" }
AUD: { currencyCode: "AUD", amount: "12.50" }
}
then we would not need to change the name.
This would also allow our utility functions we write for MoneyInput
to be usable with LocalizedMoneyInput
since then both would be dealing with a shape of { currencyCode, amount }
.
We'd need to adapt some of the static conversion functions. Maybe this is simpler than mangling the event names?
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer this too. It's simpler than changing the event names.
fd94e04
to
7d6bdeb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking very good, just some small things left 👍
Could you update the PR description to reflect the latest changes? Having up-to-date PR descriptions helps us write a better changelog.
@@ -22,7 +25,7 @@ import { LocalizedMoneyInput } from '@commercetools-frontend/ui-kit'; | |||
| ------------------------------- | ---------------- | :------: | ---------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | |||
| `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` | | |||
| `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` | | |||
| `value` | `object` | ✅ | - | - | Values to use. Keyed by currency, the values are the actual values, e.g. `{ USD: '12.22', EUR: '41.44' }` | | |||
| `value` | `object` | ✅ | - | - | Values to use. Keyed by currency, the values are the actual values, e.g. `{ USD: {currencyCode: 'USD', amount: '12.22'}, EUR: {currencyCode: 'EUR', amount: '41.44'} }` | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The values are the money values" instead of "actual values"
@@ -153,16 +174,21 @@ Here are examples of `centPrecision` and `highPrecision` prices. | |||
|
|||
##### `LocalizedMoneyInput.parseMoneyValues` | |||
|
|||
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 })`. | |||
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: [currencyCode], amount: [amount]} })`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes it look like there are arrays. I'd write
{ [currencyCode]: { currencyCode , amount } }
Notice that I also removed the regular brackets. If we want regular brackets, they should be outside like this "({ [currencyCode]: { currencyCode , amount } }
)"
amount: PropTypes.string.isRequired, | ||
currencyCode: PropTypes.string.isRequired, | ||
}) | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value should still be required
value: PropTypes.objectOf(
PropTypes.shape({
amount: PropTypes.string.isRequired,
currencyCode: PropTypes.string.isRequired,
})
).isRequired,
currencyCode, | ||
amount: values[currencyCode], | ||
}) | ||
MoneyInput.convertToMoneyValue(values[currencyCode]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this could be written as
static convertToMoneyValues = values =>
Object.values(values).map(value => MoneyInput.convertToMoneyValue(value))
}, | ||
}); | ||
if (this.props.handleChange) this.props.handleChange(event); | ||
if (this.props.onChange) this.props.onChange(event); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. We need to document the change in the changelog though.
@@ -9,17 +9,31 @@ class TestComponent extends React.Component { | |||
static displayName = 'TestComponent'; | |||
static propTypes = { | |||
id: PropTypes.string, | |||
value: PropTypes.objectOf(PropTypes.string).isRequired, | |||
value: PropTypes.objectOf( | |||
PropTypes.shape({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a breaking change here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do I do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No change required, when we make the changelog for the next version we just need to check this PR and document the changes 😄
Currently working of the tests. |
@gnerkus any update on this PR? Need any help? |
0e71ee4
to
e18178f
Compare
Requested changes have been made. @montezume @dferber90 please review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you merge @gnerkus ? 🎉
Summary
{amount, currencyCode}
Description
This PR re-defines the
values
prop expected by theLocalizedMoneyInput
as an object of{amount, currencyCode}
. This PR changes the required valuesfrom:
to:
This is required to keep the change event signature consistent with the event from the contained
MoneyInput
components.Visual diffs