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

[Bug]: Checkbox onClick event firing twice not allowing checkboxes to be selected #10122

Closed
2 tasks done
kevinsperrine opened this issue Nov 16, 2021 · 3 comments · Fixed by #10130 or #10264
Closed
2 tasks done

Comments

@kevinsperrine
Copy link
Contributor

kevinsperrine commented Nov 16, 2021

Package

carbon-components-react

Browser

Chrome

Package version

v10.48.0

Description

The onClick event is being fired on both the label and the input causing the checkbox to check and immediately uncheck.

CodeSandbox example

https://react.carbondesignsystem.com/?path=/story/components-checkbox--playground
https://codesandbox.io/s/cool-lederberg-qwoc1?file=/src/index.js

Steps to reproduce

Try to select a checkbox in the playground. https://react.carbondesignsystem.com/?path=/story/components-checkbox--playground

Code of Conduct

@tay1orjones
Copy link
Member

tay1orjones commented Nov 16, 2021

Hey @kevinsperrine! Sorry you ran into this. For implementing a controlled checkbox, the onChange is the intended way to catch updates and set local state.

function ExampleComponent() {
  const [isChecked, setIsChecked] = useState(false);
  return <Checkbox checked={isChecked} onChange={setIsChecked} />;
}

The onClick was added recently in #9827 and admittedly makes the Checkbox API a bit more confusing. The intent of onClick was to provide a handler to control the bubbling of click events from Checkbox without having to implement your own wrapper component/element.

I think this is something that would benefit being documented more thoroughly in the storybook. I'll put together a PR.

@bjornalm
Copy link

bjornalm commented Dec 2, 2021

@tay1orjones or @sstrubberg
Can you reopen this one? The additional event from the click is still causing problems even when using onChange if the wrapping element also needs a click event handler. See https://codesandbox.io/s/proud-hooks-c96dc?file=/src/index.js

In order to work around this I have to add an onClick handler to the Checkbox and call stopPropagation on the event just to stop the wrapping element from also firing twice.

@tay1orjones
Copy link
Member

@bjornalm Sorry for the hassle with this. I think we can mirror Checkbox to have parity with recent changes to InlineCheckbox #10151 and only allow click events to propagate from the input and not the label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment