final-form / react-final-form

🏁 High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.39k stars 481 forks source link

Key-Value pair removed instead of updated when cleaning input field #614

Open borjadotai opened 5 years ago

borjadotai commented 5 years ago

Describe the bug I just came across something probably considered a feature that may cause some headaches and I'm not sure it's intended behaviour and if so, would love to know why.

If a field, (let's say a non-required text input, such as middle-name of a user), is pre-populated through initialValues, and such text is then manually removed by the user. Instead of setting such key's value to empty-string or null, it would instead completely remove the key-value pair of the internal state.

This means that if you were to send that state object to your API, the change made by the user (in this case, removing their own middle-name), wouldn't be a part of the set of changes sent, and so, it wouldn't be modified.

There obviously are many ways around this. For starters, you could just check for that condition and populate that state object on submission yourself. I'm just wondering if this behavior is intended and if so, why.

To Reproduce https://codesandbox.io/embed/react-final-form-simple-example-9rzur React Final Form Simple example with initialValues pre-populated.

Just manually remove the middle name's text on the bottom of the form and look at how the values in state change just below.

Expected behavior The state values don't reflect the middleName value to change to empty string or null, rather it just completely removes the key-value pair.

Platform (please complete the following information):

JurajKavka commented 2 years ago

have you found solution to this? why the key from initialValues object is completely removed? It disables validation before submit.