renato-bohler / redux-form-input-masks

Input masking with redux-form made easy
https://bohler.dev/redux-form-input-masks/
MIT License
138 stars 10 forks source link

NumberMask: erroneous behavior when using allowEmpty with initialValues #37

Closed renato-bohler closed 5 years ago

renato-bohler commented 6 years ago

What are you reporting?

What is the current behavior?

When a Field which has an initial value is using a number mask with the allowEmpty option set, it is not possible to set the input value to zero. Also, in this case, instead of setting the Field's value to undefined, it sets to an empty string.

issue37

What is the expected behavior?

We should be able to set the Field to 0 in this case and the value when the input is empty should be undefined or null.

Sandbox Link

https://codesandbox.io/s/n4k073xzj

What's your environment?

Version: 1.1.3

Other information

This is related to https://github.com/erikras/redux-form/issues/3946. The only viable way to fix it is changing may be changing the normalize's return to null for allowEmpty (which makes more sense to me), but this may be a breaking change.

mitchvoll commented 6 years ago

https://github.com/renato-bohler/redux-form-input-masks/blob/31b214d11d9a92101e6b211b9c6e6790a590b5e7/src/createNumberMask.js#L106

This returns an empty string for the normalize function if allowEmpty is set. The API docs say that if allowEmpty is set then undefined will stored. Could this be part of the issue?

renato-bohler commented 6 years ago

@mitchvoll redux-form's change action will not set the Field value to undefined if its payload is undefined (in fact it will do nothing if that's the case). One way to set undefined to the Field with this action is to set the payload to an empty string, but that only works if the target Field has no initial values - and that's the cause of this issue.

There's more information here: https://github.com/erikras/redux-form/issues/3946#issuecomment-388550168.

I think the only way to really solve this for the current version of redux-form is to set the value to null instead of undefined (the change action will set to null if the payload is null), but that will cause a breaking change.

renato-bohler commented 5 years ago

:tada: This issue has been resolved in version 2.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: