Checkbox fires two events on click #5882
-
Environment
Steps to reproduce
import React, { useState } from 'react'
import { HTMLTable, Checkbox } from '@blueprintjs/core';
export default Test
function Test(props: any) {
const [isChecked, setIsChecked] = useState(false)
return (
<HTMLTable>
<thead>
<tr>
<th>Checkbox</th>
</tr>
</thead>
<tbody>
<tr onClick={event => handleClick()}>
<td><Checkbox checked={isChecked}/></td>
<td>Click here and it works!</td>
</tr>
</tbody>
</HTMLTable>
)
function handleClick() {
setIsChecked(!isChecked)
}
}
Actual behaviorWhen clicking row, checkbox is toggled. When clicking checkbox, it does not change. I inserted some debuggers to see what was happening and it turns out that there are many events being fired. Here are the events I saw:
The states get weird in between these and it essentially somehow sets Expected behaviorWhen clicking row, checkbox is toggled. When clicking checkbox, checkbox is toggled. Possible solutionGiving the Ideally, we would be able to not give the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Can you try working around it by checking Edit: @martijnarts's answer below and the linked StackOverflow post is helpful. There is a problem with the current design of Blueprint's control elements where this double click behavior will continue to happen if you attach an |
Beta Was this translation helpful? Give feedback.
-
That also works as a workaround. I think a better question to ask would be: Is this the intended behavior? I've been experimenting with a few component libraries and blueprint is the first one that has given me this problem. However, I could see this being the desired behavior in some cases. |
Beta Was this translation helpful? Give feedback.
-
If it helps, this is standard HTML behaviour: clicking the See also this StackOverflow question. |
Beta Was this translation helpful? Give feedback.
Can you try working around it by checking
event.target === event.currentTarget
inhandleClick
?Edit: @martijnarts's answer below and the linked StackOverflow post is helpful. There is a problem with the current design of Blueprint's control elements where this double click behavior will continue to happen if you attach an
onClick
handler to any parent of a<Checkbox>
or<Switch>
. We can fix this in a future major version of Blueprint by making<input>
a sibling of<label>
, but that would be a breaking change. I will consider this for Blueprint v6.0.