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

Field props onChange accepts event or value, but onChange callback only accepts events (not plain values) #3446

Closed
naw opened this issue Sep 23, 2017 · 6 comments

Comments

@naw
Copy link
Contributor

naw commented Sep 23, 2017

According to the docs, it's valid to call onChange with either a synthetic event or a value. My form component calls it with a simple value (like a string or number).

However, if an onChange callback is defined on the given Field, then redux-form assumes the value is an event object and tries to extend it with a preventDefault key. This doesn't work for values like strings and numbers.

This is the code I'm referring to: https://github.com/erikras/redux-form/blob/6e262c2945fd93c6f0b06441487deaba27e8cef0/src/ConnectedField.js#L113-L119

I'm not sure if the code needs to be updated or the docs need to be updated. I'd be happy to work on a PR, if you let me know what the correct resolution is. Thank you.

@ChengYurou
Copy link

i have same question

@nadezhda-serafimova
Copy link

The same for me. I'm using react-select and its onChange prop returns an object { label, value }, instead of the event. redux-form require us to pass an event to its input.onChange function.

So I hot fixed it, as adding a new custom proponChanged to the Field, in order to get the correct value, when the Select is changed:

import React from 'react'
import ReactSelect from 'react-select'

const Select = ({value, name, onChange, onChanged, type, disabled, className = 'c-custom-select',
  placeholder, options, clearable, multi, optionRenderer}) => {
  return <ReactSelect
    // ... all needed props
    onChange={selected => {
      onChange(selected ? selected.value : null)

      if (onChanged) onChanged(selected ? selected.value : null)
    }}
    />
}

export default Select

However, it will be great if onChange accepts both - event and a value.

@stephane-ruhlmann
Copy link

I had to do the same hotfix, not very convenient...

@arvinsim
Copy link

Is this already fixed? I have the same problem where the onChange handler's arguments changes on some calls.

@erikras
Copy link
Member

erikras commented Dec 11, 2018

Published fix in v8.0.0.

@lock
Copy link

lock bot commented Apr 2, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants