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

Format currency input with commas in household input #941

Merged
merged 7 commits into from
Dec 14, 2023

Conversation

abhcs
Copy link
Collaborator

@abhcs abhcs commented Dec 7, 2023

Fixed #178 by using the formatter property in ant-design InputNumber component. Renamed locale en to en-GB, the standard Unicode identifier for the English language in the UK region (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale for details). Improved the interface for locale-related functions with better names and documentation.

Tested in UK and US locales to verify that currencies are displayed properly in inputs and charts.
Screenshot 2023-12-07 at 11 41 45 AM

Screenshot 2023-12-07 at 11 44 44 AM

🤖[deprecated] Generated by Copilot at 63f4416

Summary

🌐💷📊

This pull request improves the localization of currency values and plotly charts in the policy engine app. It introduces two new functions, localeCode and currencyCode, to get the appropriate codes for a given country. It also updates the VariableEditor component and the output charts to use these functions and the native toLocaleString method of numbers. It also renames and updates the UK English plotly locale module.

Oh, we're the jolly coders of the policy engine
We format currencies and locales with ease
We use localeCode and currencyCode to make it clean
And we heave away on the pull request with a breeze

Walkthrough

  • Replace currencyString and localeString functions with localeCode and currencyCode functions in src/api/language.js to return locale and currency codes for a given country id (link)
  • In src/pages/household/input/VariableEditor.jsx, update the formatter prop of the InputNumber component in the HouseholdVariableEntityInput function (link, link)
  • In src/pages/policy/output/AverageImpactByDecile.jsx, update the text prop of the bar trace in the AverageImpactByDecilePlot function (link, link)
  • In src/pages/policy/output/AverageImpactByWealthDecile.jsx, update the text prop of the bar trace in the AverageImpactByWealthDecilePlot function (link, link)
  • In src/pages/policy/output/AverageImpactByDecile.jsx, update the locale prop of the Plot component in the AverageImpactByDecilePlot function (link)
  • In src/pages/policy/output/AverageImpactByWealthDecile.jsx, update the locale prop of the Plot component in the AverageImpactByWealthDecilePlot function (link)
  • In src/pages/policy/output/BudgetaryImpact.jsx, update the locale prop of the Plot component in the BudgetaryImpactPlot function (link, link)
  • In src/pages/policy/output/DetailedBudgetaryImpact.jsx, update the locale prop of the Plot component in the DetailedBudgetaryImpactPlot function (link, link)
  • Rename src/plotly_locales/locale-en.js to src/plotly_locales/locale-en-gb.js and change the name property of the loc_en module to en-GB to reflect the correct locale code for the UK English plotly locale (link)
  • Update the import and registration of the UK English plotly locale in src/redesign/components/PolicyEngine.jsx to use the renamed loc_en_gb module (link, link)

@abhcs abhcs requested a review from nikhilwoodruff December 7, 2023 17:48
@MaxGhenis MaxGhenis requested a review from anth-volk December 9, 2023 21:55
@nikhilwoodruff
Copy link
Contributor

Thanks @abhcs - how does this handle user input? Just to check that if a user sees commas and changes the value to a string including commas that this wouldn't break the app.

@abhcs
Copy link
Collaborator Author

abhcs commented Dec 11, 2023

The formatter property changes the display string on each event. The user will be unable to type the string 1,234 in the normal way because at 1,2 the output will get reformatted to 12.

input display
1 1
1, 1,
1,2 12
1,23 123
1,234 1,234

Note that the current component also reformats the display string similarly but does not add commas. This is an unavoidable side-effect of "formatting in real time" (#178); see also the formatter example at https://ant.design/components/input-number.

If one were to directly paste the string $123456 into the new component, then it would get reformatted to 123,456. In the current component, it would get reformatted to 123456.

@anth-volk
Copy link
Collaborator

anth-volk commented Dec 11, 2023

This looks like a great change. I tested locally and can see that your edits do handle the comma properly, as well as clipping out garbage values. That said, per the video I've included here, the edits don't properly format an input sub-dollar unit divisible by ten; for instance, if I input "$15,000.30", it shortens this to "$15,000.3". So, to proceed, I'm wondering if you'd be willing to do the following:

  1. Investigate if there is an antd setting that always displays two decimal places if the input number contains a decimal point (at least after the user is no longer focused on the box)
  2. Remove the default "0" that's displayed when nothing has been input, and instead have the box be empty, as it is currently on the live app
  3. Rebase off of master

Thanks so much for your contributions to the app!

Screen.Recording.2023-12-11.at.4.43.43.PM.mov

@anth-volk
Copy link
Collaborator

Screen.Recording.2023-12-11.at.4.43.43.PM.mov

Hopefully the video uploaded correctly in this comment

Copy link
Collaborator

@anth-volk anth-volk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies, I should've left my comments here, but please see my comments on the PR itself

@abhcs
Copy link
Collaborator Author

abhcs commented Dec 12, 2023

This looks like a great change. I tested locally and can see that your edits do handle the comma properly, as well as clipping out garbage values. That said, per the video I've included here, the edits don't properly format an input sub-dollar unit divisible by ten; for instance, if I input "$15,000.30", it shortens this to "$15,000.3". So, to proceed, I'm wondering if you'd be willing to do the following:

  1. Investigate if there is an antd setting that always displays two decimal places if the input number contains a decimal point (at least after the user is no longer focused on the box)

In the OnChange handler for InputNumber, we get a value of type string but it has already lost its trailing zeros. The conversion of the input to this string is handled by antd; thus, the internal logic of InputNumber is responsible for the good behavior, e.g., removing elements like $. The formatter never sees the input $15,000.30, it only sees 15000.3 provided by antd. There are properties like stringMode in InputNumber that may be worth exploring but we would lose the abstraction of InputNumber.

The abstraction of InputNumber is an important one. Notice in the image below how the zeros after the comma are ignored when we just use Input in ParameterEditor:

Screenshot 2023-12-10 at 3 34 19 PM
  1. Remove the default "0" that's displayed when nothing has been input, and instead have the box be empty, as it is currently on the live app

Since pull request #892, the default is displayed as 0.00. The current request changes this to 0.

  1. Rebase off of master.

Done.

Thanks so much for your contributions to the app!

You are welcome.

@abhcs
Copy link
Collaborator Author

abhcs commented Dec 12, 2023

The new commits should fix #928. Now, I am using the formatter property for all floats. Despite age being a float, the trailing zeros get stripped.

@abhcs abhcs requested a review from anth-volk December 12, 2023 04:05
@anth-volk
Copy link
Collaborator

I've been digging into this to see if we can get it to display two decimal places only after the user clicks outside the input, as doing it before prevents the user from inputting a second decimal place without hitting backspace. I'll come back to it tomorrow.

@abhcs
Copy link
Collaborator Author

abhcs commented Dec 13, 2023

Please check the latest commit. It may do what you are looking for; otherwise, we can revert it. It uses a strange combination of formatter and precision, which seems to work.

Copy link
Collaborator

@anth-volk anth-volk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've taken the liberty of making a minor edit: In Ant Design, the precision feel is overridden whenever a formatter is present, meaning your last commit unfortunately didn't have its intended impact. However, there's a pretty hidden userTyping param that allows for conditional formatting based on whether the user's actively engaging with the box, so I've edited it to format decimals only when the user isn't typing.

Thanks again for digging deep into antd's docs, @abhcs.

Edit: Just waiting to push until the checks finish

@anth-volk anth-volk merged commit 0eb6ed3 into PolicyEngine:master Dec 14, 2023
2 checks passed
@abhcs
Copy link
Collaborator Author

abhcs commented Dec 14, 2023

The docs say that precision is overridden by formatter but the solution was working! It was allowing the user to type an arbitrary number of fractional digits but the precision was getting applied on blur, i.e., not typing. I think that using userTyping is a better solution though. See PR #962.

@abhcs abhcs deleted the fix-issue-178 branch December 14, 2023 02:26
@anth-volk
Copy link
Collaborator

anth-volk commented Dec 14, 2023 via email

@abhcs
Copy link
Collaborator Author

abhcs commented Dec 14, 2023

Yeah, its best not to depend on undocumented quirks in any case. So userTyping is a better solution. Maybe it was browser-specific. I am using Safari.

@anth-volk
Copy link
Collaborator

anth-volk commented Dec 14, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Format currency in real time, with commas
3 participants