qiniu / formstate-x

Manage state of form with ease.
https://qiniu.github.io/formstate-x
MIT License
34 stars 10 forks source link

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

Closed mattleonowicz closed 3 weeks ago

mattleonowicz commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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

mattleonowicz commented 3 weeks ago

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. https://github.com/qiniu/formstate-x/pull/106

Thanks!

nighca commented 3 weeks ago

@mattleonowicz released in v3.1.3.