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

[question] protected initialValue and/or get hasChanged() #105

Closed
mattleonowicz opened this issue Aug 22, 2024 · 6 comments
Closed

[question] protected initialValue and/or get hasChanged() #105

mattleonowicz opened this issue Aug 22, 2024 · 6 comments

Comments

@mattleonowicz
Copy link
Contributor

mattleonowicz commented Aug 22, 2024

Would you be willing to accept a PR where initialValue in FieldState becomes protected instead of private (to make extending easier) and/or add hasChanged getter which would return boolean if current value is different then the initialValue?

@nighca
Copy link
Collaborator

nighca commented Aug 22, 2024

Would you be willing to accept a PR where initialValue in FieldState becomes protected instead of private (to make extending easier)

@mattleonowicz Yes.

and/or add hasChanged getter which would return boolean if current value is different then the initialValue?

Seems that you are looking for something like dirty in formstate-x v2.x. We replaced that with touched in v3, while with slight difference. Please let know why dirty is needed rather than touched in your case.

@mattleonowicz
Copy link
Contributor Author

mattleonowicz commented Aug 22, 2024

Seems that you are looking for something like dirty

Yes exactly.
Touching can possibly mean the scenario when someone added 1 character and then deleted it (simplest example), effectively bringing the value back to the initialValue

I often need to check if the form is dirty rather then just touched because if the form has unsaved changes, I want to prevent closing of the modal with it.
In this case above, the form is touched, but technically nothing has changed, so there is no need to save it nor warn about discarding unsaved changes.

I'm aware that simple === equality might not work for all complex cases (when objects are stored in the Fieldstate). Is this the reason why dirty was removed?

@nighca
Copy link
Collaborator

nighca commented Aug 22, 2024

I'm aware that simple === equality might not work for all complex cases (when objects are stored in the Fieldstate). Is this the reason why dirty was removed?

That is part of it.

formstate-x recommend composing simpler states to more complex states (with FormState / ArrayFormState / ...), it is also difficult to do equality checking for composed states.

It is also a result of our discussion about user experience. Users can easily recall when they have made a change, but they often struggle to remember the initial value. For instance, if a text input starts with "abc" and the user types to change it to "abcd", it's clear to them why the modal can't close or the save button is enabled—they recognize they have modified something. However, if they continue typing and cycle through "abcde", "abcd", and finally back to "abc", they are likely unaware that the final value is just the same as the original. What they do know is that they have made a change. Therefore, it would be confusing if the modal were allowed to close or the save button were disabled in this situation. Now it may be better to just let them submit the form (or offer some explicit "cancel" / "reset" button), which eliminates uncertainty.

@nighca
Copy link
Collaborator

nighca commented Aug 22, 2024

It's fine if you still prefer "dirty." A PR that changes initialValue in FieldState from private to protected is acceptable.

@mattleonowicz
Copy link
Contributor Author

Ok I understand. I mostly agree with your point of view.
I made a small PR with just that "protected" to make it easier to extend the FieldState if needed.
#106

Thanks!

@nighca
Copy link
Collaborator

nighca commented Aug 22, 2024

@mattleonowicz released in v3.1.3.

@nighca nighca closed this as completed Aug 22, 2024
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

No branches or pull requests

2 participants