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

Fix whole number replacement when using fixedDecimalScale and prefix/suffix #228

Conversation

wolfib
Copy link
Contributor

@wolfib wolfib commented Sep 21, 2018

Double clicking on a number selects the whole number, but not any prefix/suffix. Previously, when making an edit after such a double click, the first digit entered would not lead to any change, if the NumberFormat component uses fixedDecimalScale and prefix/suffix.

That's because replacing the comma separator is not allowed, except when replacing the whole number. But since the prefix/suffix is not selected, this "replacing the whole number" behaviour was not triggered.

This commit fixes the behaviour by taking prefixes and suffixes into account.

See issue #159

@@ -595,8 +595,8 @@ class NumberFormat extends React.Component {
value.length > lastValue.length
|| !value.length ||
start === end ||
(start === 0 && end === lastValue.length) ||
(selectionStart === 0 && selectionEnd === lastValue.length)
(start <= prefix.length && end >= lastValue.length - suffix.length) ||
Copy link
Owner

@s-yadav s-yadav Sep 22, 2018

Choose a reason for hiding this comment

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

  • ^update the comment
  • create two variables, and check for weather format pattern is not defined.
const leftBound = !!format ? 0 : prefix.length;
const rightBound = lastValue.length - (!!format ? 0 : suffix.length);

Copy link
Owner

Choose a reason for hiding this comment

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

Also the condition start <= prefix.length && end >= lastValue.length - suffix.length will return half formatted value when multiple character is used for prefix/ suffix. For example if you used $$ as prefix.
and the value is $$3.00. And if you replace $3.00 with character 4. This method will return $4 which this method is trying to avoid. This might cause some other bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The point about partially selected prefixes/suffixes is very valid. In my manual tests the overall behaviour in such a case still seemed to be fine, but since I do not know the whole context here I am now suggesting a more defensive approach.
Only adding one additional condition for returning early from correctInputValue(), which is if exactly the number without prefix/suffix is selected/changed. If parts of prefix/suffix are part of the selection, this results in no change to the value.

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.

Looks good. There is small change which I will make. Thanks

@@ -591,12 +591,16 @@ class NumberFormat extends React.Component {
or if value is empty string (when whole input is cleared)
or whole input is replace with a number
*/
const leftBound = !!format ? 0 : prefix.length;
const rightBound = lastValue.length - (!!format ? 0 : suffix.length);
if (
value.length > lastValue.length
|| !value.length ||
start === end ||
(start === 0 && end === lastValue.length) ||
Copy link
Owner

@s-yadav s-yadav Sep 30, 2018

Choose a reason for hiding this comment

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

logically when ever start === 0 && end === lastValue.length is true selectionStart === 0 && selectionEnd === lastValue.length will always be true (in case of deletion, any addition case is already handled on value.length > lastValue.length). Same for leftBound and rightBound condition.
I will remove the extra condition.

Copy link
Collaborator

@nikhil-varma nikhil-varma Mar 30, 2021

Choose a reason for hiding this comment

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

Please refer this for understanding more about these conditions and why we need both

@s-yadav
Copy link
Owner

s-yadav commented Sep 30, 2018

Rebased and merged with master.

@s-yadav s-yadav closed this Sep 30, 2018
s-yadav added a commit that referenced this pull request Sep 30, 2018
@wolfib
Copy link
Contributor Author

wolfib commented Oct 2, 2018

Awesome! Thanks for merging!

@wolfib wolfib deleted the bugfix-whole-number-replacement-with-fixedDecimalScale branch October 2, 2018 08:09
@wolfib wolfib restored the bugfix-whole-number-replacement-with-fixedDecimalScale branch October 2, 2018 08:11
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