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

Checkbox not firing onChange #51

Closed
maciejmyslinski opened this issue Dec 12, 2017 · 3 comments
Closed

Checkbox not firing onChange #51

maciejmyslinski opened this issue Dec 12, 2017 · 3 comments

Comments

@maciejmyslinski
Copy link
Contributor

Are you submitting a bug report or a feature request?

It's a bug although I'm not sure if it's caused by react-final-form.

What is the current behavior?

In some specific cases, checkbox does not fire onChange handler function on the first click. This will work fine:

const Input = props => <input {...props} />;

const Checkbox = ({ children, ...restProps }) => (
  <label>
    <Input type="checkbox" {...restProps} />
    {children}
  </label>
);

// then inside a Form
<Field
  name="good"
  render={({ input }) => (
    <Checkbox {...input}>It works as expected</Checkbox>
  )}
  type="checkbox"
/>

This will cause a bug:

const Checkbox = ({ children, ...restProps }) => {
  const Input = props => <input {...props} />;

  return (
    <label>
      <Input type="checkbox" {...restProps} />
      {children}
    </label>
  );
);

// then inside a Form
<Field
  name="bad"
  render={({ input }) => (
    <Checkbox {...input}>
      It is <b>not working on first click</b>.
    </Checkbox>
  )}
  type="checkbox"
/>

What is the expected behavior?

onChange handler should be fired.

Sandbox Link

https://codesandbox.io/s/k5y0znj02v

What's your environment?

package version
React Final Form 1.1.1
Final Form 1.2.1
OS macOS Sierra (10.12.6)
browser Chrome 62 (note: the bug doesn't appear in Firefox nor Safari)

Other information

@erikras
Copy link
Member

erikras commented Dec 12, 2017

This is because, in the latter example, you are creating a new Input component on every render of checkbox, which is being rendered with React.createElement() by using it in JSX. It has to do with how React replaces the DOM element, rather than just updating the existing one. This would happen in React no matter how you were keeping your form state. Ergo, it's not a problem with this library, but simply a pitfall of the way React works. If you did this with a text input, it would lose focus on the first keypress due to the rerender.

Great reporting, though! 👍

@erikras erikras closed this as completed Dec 12, 2017
@maciejmyslinski
Copy link
Contributor Author

Thank you @erikras for such quick response and for such great library! 👍

@lock
Copy link

lock bot commented Dec 13, 2018

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 Dec 13, 2018
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

2 participants