dohomi / react-hook-form-mui

Material-UI form components ready to use with react-hook-form
https://react-hook-form-material-ui.vercel.app
MIT License
561 stars 111 forks source link

Fix Bug where 0 was returned as null/'' #282

Closed bethmaloney closed 4 months ago

bethmaloney commented 5 months ago

Currently we do a falsy check on the input/output of the TextFieldElement. As 0 is a falsy value this means that we return null or an empty string.

To fix this we check if the value is nullish instead.

You can test it by using the Number input control and inputting 0. Previously it would show a blank value but now it shows 0.

vercel[bot] commented 5 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-hook-form-material-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 2, 2024 1:40am
bethmaloney commented 5 months ago

Sorry need to rebuild storybook

bethmaloney commented 5 months ago

Rebuilt storybook. Should be good to go.

akullpp commented 4 months ago

Please release

dohomi commented 4 months ago

@bethmaloney thanks for your time an contribution. I just got back from holiday and straight caught a bad cold thats why my response (and brain) still a bit slow. There are minor concerns but currently your changes would change an empty string into a "0" thats where we need to look at as first. I merged your other PR's already they where 👍 thanks

bethmaloney commented 4 months ago

Hi @dohomi no problems. Let me fix that up

bethmaloney commented 4 months ago

Thinking about it a bit more I think it makes sense for a TextFieldElement with type='number' to return Number | null | undefined.

null | undefined would be returned when the field is blank while Number is returned when the field is not empty. The other option would be to return Number | undefined | ''. I feel that checking if the return value is undefined or a string would feel a little strange and might surprise consumers. Let me know what you think. Below is the behaviour of the changes I've just pushed (happy to change however you'd like).

No Default Value image

Value added and cleared image

0 value image

Other value image