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

Add allowedDecimalSeparators prop #345

Merged
merged 3 commits into from
Sep 29, 2019

Conversation

macobo
Copy link
Contributor

@macobo macobo commented Sep 24, 2019

When defined, this prop defines which keys to accept as decimal
separators. This is useful in cases where users might want to enter both
comma and dot as separators

Related issues: #324, #133, #223, #261

When defined, this prop defines which keys to accept as decimal
separators. This is useful in cases where users might want to enter both
comma and dot as separators

Related issues: s-yadav#324, s-yadav#133, s-yadav#223, s-yadav#261
/** Check if only . is added in the numeric format and replace it with decimal separator */
if (!format && decimalSeparator !== '.' && start === end && value[selectionStart] === '.') {
/** Check for any allowed decimal separator is added in the numeric format and replace it with decimal separator */
if (!format && start === end && allowedDecimalSeparators.includes(value[selectionStart])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: This might not work in all browsers. Should I extract a util instead?

Copy link
Owner

Choose a reason for hiding this comment

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

You can use .indexOf instead.

@macobo
Copy link
Contributor Author

macobo commented Sep 27, 2019

Hi @s-yadav, mind taking a look? Happy to iterate on this as/if needed.

Copy link
Owner

@s-yadav s-yadav left a comment

Choose a reason for hiding this comment

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

Thanks @macobo for putting this. The PR looks good with this two small changes.

/** Check if only . is added in the numeric format and replace it with decimal separator */
if (!format && decimalSeparator !== '.' && start === end && value[selectionStart] === '.') {
/** Check for any allowed decimal separator is added in the numeric format and replace it with decimal separator */
if (!format && start === end && allowedDecimalSeparators.includes(value[selectionStart])) {
Copy link
Owner

Choose a reason for hiding this comment

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

You can use .indexOf instead.


if (thousandSeparator === true) {
thousandSeparator = ','
}
if (!allowedDecimalSeparators) {
allowedDecimalSeparators = [decimalSeparator]
if (decimalSeparator !== '.') {
Copy link
Owner

Choose a reason for hiding this comment

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

No need for this if condition. You can just define

allowedDecimalSeparators = ['.', decimalSeparator]

It's okay if dot gets duplicate as we are using this array just for checking includes.

@macobo
Copy link
Contributor Author

macobo commented Sep 29, 2019

Thanks for the feedback @s-yadav. I've updated the PR.

@s-yadav
Copy link
Owner

s-yadav commented Sep 29, 2019

Can you also update the readme for the new prop

@macobo
Copy link
Contributor Author

macobo commented Sep 29, 2019

Also done @s-yadav.

@s-yadav
Copy link
Owner

s-yadav commented Sep 29, 2019

I see that you have updated the example file, we will need it to update on README file.

@macobo
Copy link
Contributor Author

macobo commented Sep 29, 2019

Oops - sorry, tried to push to the wrong remote thus the PR didn't get updated. Done.

@s-yadav s-yadav merged commit 3fe3806 into s-yadav:master Sep 29, 2019
@s-yadav
Copy link
Owner

s-yadav commented Sep 29, 2019

Merged. Right now I am traveling. Will test it out and release it on npm tomorrow.

@macobo
Copy link
Contributor Author

macobo commented Sep 29, 2019 via email

s-yadav added a commit that referenced this pull request Oct 1, 2019
@tenkij
Copy link

tenkij commented Oct 1, 2019

Thank you!
Little problems:
https://codesandbox.io/s/react-number-format-demo-9y7j7

  1. If you paste a number '11,11', then the formatted number will be '1 111'. If you enter a number '11,11', then the formatted number will be '11.11'.

  2. Press the “11, 11” button. The input value will be “11, 11”. Then click on the input. After losing focus, the input value will be “1 111”.

@macobo
Copy link
Contributor Author

macobo commented Oct 1, 2019

Hi @tenkij it's worth creating a new issue for these rather than on a merged PR. The pasting issue seems to exist before this PR at least.

@macobo macobo deleted the add-allowed-decimal-separators-prop branch October 1, 2019 20:25
@s-yadav
Copy link
Owner

s-yadav commented Oct 1, 2019

@tenkij the allowedDecimalSeparators is to only allow user to type a character which will be converted to passed decimalSeparator prop. When you paste or change a state value, the incorrect characters are removed.
But when you pass isNumericString true, it is an indication to NumberFormat that the passed value prop is a valid numeric string (without formatting) do not try to process it.

@s-yadav
Copy link
Owner

s-yadav commented Oct 1, 2019

But there seem to be some inconsistencies on paste, which I will look into.

@tenkij
Copy link

tenkij commented Oct 1, 2019

@macobo, ok.
Problem only if the following conditions are true:

  1. allowedDecimalSeparators={[",", "."]}
  2. decimalSeparator = "."
  3. paste '11,11'

paste '11.11' - ok.

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

Successfully merging this pull request may close these issues.

3 participants