Skip to content

[EuiCheckableCard] onChange handler invoked twice upon checkbox click #8782

Closed

Description

@tonyghiani

Describe the bug
When the EuiCheckableCard component is set with the checkableType="checkbox" property, the handler passed to onChange is invoked twice when the click occurs directly on the checkbox itself, while it triggers correctly only once when clicking around the checkbox or on the label.

✅ onChange triggered once

Image

❌ onChange triggered twice

Image

Environment and versions

  • EUI version: 103
  • React version: 18

To Reproduce

Run this component in isolation

export default () => {
  const [checkbox, setCheckbox] = useState(false);
  const checkboxCardId = useGeneratedHtmlId({ prefix: 'checkboxCard' });

  return (
    <EuiCheckableCard
      id={checkboxCardId}
      label="I am a checkbox"
      checkableType="checkbox"
      value="checkbox1"
      checked={checkbox}
      onChange={() => setCheckbox(prev => !prev)}
    />
  );
};

N.B. Differently from the demo in the docs, the boolean update is made with a functional update to demonstrate how this could easily break depending on what the event handler does.

Expected behavior
The event handler passed to onChange should always be invoked once, indipendently from where the click occurs in the card.

Screenshots (Optional)

Screen.Recording.2025-06-11.at.14.33.49.mov

Activity

  1. added theissue type on Jun 11, 2025
  2. added
    ⚠️ needs validation
    on Jun 11, 2025
  3. weronikaolejniczak commented on Jun 11, 2025

    @weronikaolejniczak
    Contributor

    The key difference is here: onChange={() => setCheckbox((prevValue) => !prevValue)} This should work correctly. Reproduction sandbox

    For a workaround, reference the state directly: !isChecked. Not ideal but according to the docs example works.

  4. tonyghiani commented on Jun 11, 2025

    @tonyghiani
    ContributorAuthor

    @weronikaolejniczak the suggested workaround is valid just because React would reference the old value twice when the handler is triggered, but the onChange is not just to toggle or integrate with a watched value.
    For instance, this workaround is won't work when integrating with other state management libraries.

  5. mgadewoll commented on Jun 12, 2025

    @mgadewoll
    Contributor

    ℹ It's triggered twice because of this part here.

    When the input is clicked, it's clicked as itself and via the additional programmatic click. We'll need to check the target to exclude it.

  6. removed
    ⚠️ needs validation
    on Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions