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

Formsy onChange model missing value object methods #379

Closed
wkerswell-gresham opened this issue Feb 5, 2020 · 6 comments · Fixed by #391
Closed

Formsy onChange model missing value object methods #379

wkerswell-gresham opened this issue Feb 5, 2020 · 6 comments · Fixed by #391

Comments

@wkerswell-gresham
Copy link

Hi,

For context I have created a datepicker using withFormsy(). The date selected by the user is a Moment.js object which has a bunch of methods for manipulating and formatting the date.

The model on the Formsy prop onChange((model)=>{ }) remove all the __proto__ methods on the object.

I think this is down to the cloneIfObject() method in

export function cloneIfObject(value: unknown) {
which is spreading the current model values.

This doesn't seem to be the same behaviour with setting the value using the setValue() prop in the withFormsy component.

This was working pre the v1 to v2 update.

Is the cloneIfObject method needed?

Many thanks

@rkuykendall
Copy link
Member

Thank you for the first post-2.x bug report!

The change appears to date back to formsy 1.0 or just after. However, it's probably been used now in a place or way it wasn't before:

https://github.com/formsy/formsy-react/blame/42e39cbc8fa14333374c56b38b9b9bf389916991/src/index.js#L91-L105

Our options as I see them are to:

  1. Avoid cloning, accepting possible param reassignments the original author of the change was trying to keep safe from

  2. Improve the function cloneIfObject to not break objects like Moment.js objects

@aesopwolf do you have any opinions on this?

@wkerswell-gresham
Copy link
Author

Thanks for the quick reply.

I have done some reading and I think the cloneIfObject spread could be replaced with Object.assign({}, data) which is what was originally there.

@rkuykendall
Copy link
Member

image

It doesn't appear so. I'm going to write a test-case to get more definitive information.

Also, one additional option is that objects like this just shouldn't be supported. In that case, you would want to use something like an ISO 8601 string as your actual "value" and either use moment only internally in your input component, only external to the form, or in both. What do you think about that?

There's some sense in formsy only supporting things that could be sent to or from a server as JSON.

@wkerswell-gresham
Copy link
Author

After some playing yesterday I have worked around it by using the model value to create another moment object.

  const onChange = ({date}) =>{
      if(date){
        const momentDate = moment(date);
        ...
      }
  }

Makes sense what you are saying about only supporting the JSON so happy for this issue to be closed.
Thanks

@rkuykendall
Copy link
Member

Released:

v2.0.1

v2.0.0...v2.0.1

@rkuykendall
Copy link
Member

( complex objects should now be safe )

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

Successfully merging a pull request may close this issue.

2 participants