final-form / react-final-form

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

Custom Input buttons for radio-like input #357

Open dvnrsn opened 5 years ago

dvnrsn commented 5 years ago

Are you submitting a bug report or a feature request?

Maybe a bug, but probably ignorance of custom input usage.

Basically, I'm looking for the best way to do this and it's not clear to me in the docs...

What is the current behavior?

<Field name="isGhost" parse={() => true}>
  {({input, meta}) => (
    <button
    type='button'
    onClick={input.onChange}
    >
      <i className="cps-icon cps-icon-ghost"></i>
    </button>
  )}
</Field>

yields `Warning: Can't call setState on a component that is not yet mounted.` on first and only first click

What is the expected behavior?

no React lifecycle warning

Sandbox Link

Stack

Codesandbox

What's your environment?

"react-final-form": "^3.6.7", "final-form": "^4.10.0", Chrome

Other information

Best I can tell, this is happening in registerField call in final-form repo. The following line:

change: function change(value) {
  return api.change(name, value);
},
vospascal commented 5 years ago

onchange on a button? try this inside your component render function

return( <Field name="isGhost" parse={() => true}> {({input, meta}) => ( <input type='text' {this.props.name} {...input} /> )} )