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

[ReactFormState] Numeric string fields with formatting are incorrectly interpreted as dirty #2629

Open
LauraAubin opened this issue Apr 14, 2023 · 4 comments
Assignees
Labels
react-form Type: Bug 🐛 Something isn't working

Comments

@LauraAubin
Copy link

LauraAubin commented Apr 14, 2023

Numeric fields which are of the type string are incorrectly comparing the initial and current value. For example, if you have a numeric field which passes an initial value of "2.0", and the user replaces the value in this field with "2", since FormState is directly comparing the strings, this field will be interpreted as dirty.

One option would be to use numeric values for the form, but in some cases this is not ideal as the field has existing string formatting (such as a currency field replacing 2 with 2.00) and would cause us to have to reformat the field from when we pass in the initial value, and when we show the current value.

In the Web codebase, we worked around this by converting our initial and current values and doing the dirty check ourselves: Number("2.0") === Number("2"). I think we should be casting the numeric form fields to Numbers instead of comparing the exact strings. In cases where the field value is not a number, Number("A") will return NaN which can be omitted as a valid dirty evaluation (since NaN !== NaN if it is evaluated).

@LauraAubin LauraAubin added Type: Bug 🐛 Something isn't working react-form labels Apr 14, 2023
@jayanth-kumar-morem
Copy link

Hi @LauraAubin

A field with an initial value of "2.0" replaced by the user with "2" will be incorrectly considered dirty, as the strings are directly compared. We can instead of directly comparing the strings, we can cast numeric form fields to Numbers and then compare them. This way, the field would only be considered dirty if the underlying numeric values are different. When the field value is not a number, the comparison will result in NaN, which can be omitted as a valid dirty evaluation. We can Update the ReactFormState logic to cast numeric form fields to Numbers before comparing them, ensuring a more accurate dirty evaluation. Exclude cases where the field value is not a number, as NaN !== NaN if it is evaluated.

I would love to take this one up. Can you assign me to this.

CC: @macournoyer @shayarnett @tobi @methodmissing

@LauraAubin
Copy link
Author

@jayanth-kumar-morem go for it! Let me know if you need a hand or want to figure out a way to pair together.

@jayanth-kumar-morem
Copy link

Sure!, Thanks @LauraAubin

jayanth-kumar-morem added a commit to jayanth-kumar-morem/quilt that referenced this issue Apr 30, 2023
… state

1. Update the form state comparison by converting numeric string fields to numbers, enabling correct dirty state evaluation
2. Add isNumericString utility function to identify numeric strings and exclude empty strings

Fixes: Shopify#2629
jayanth-kumar-morem added a commit to jayanth-kumar-morem/quilt that referenced this issue Apr 30, 2023
1. Add tests to ensure isNumericString() returns true for valid numeric strings
2. Add tests to ensure isNumericString() returns false for non-numeric strings
3. Add tests to ensure isNumericString() returns false for non-string values

Fixes: Shopify#2629
@jayanth-kumar-morem
Copy link

Hey @LauraAubin ,

I raised a PR for the issue. Kindly review it.

CC: @ryanwilsonperkin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
react-form Type: Bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants