Skip to content

Disabled checkbox inside a label still triggers onChange method when clicked #1234

Done

Description

@albertnev

Reproduction example

https://codesandbox.io/p/sandbox/zealous-feynman-rxvrlm?file=%2Fsrc%2FApp.test.js

Prerequisites

  1. Render a component with an <input type="checkbox" onChange={yourFunc} disabled /> inside a <label>.
  2. Click any element inside the <label> (or the label itself).

Expected behavior

The onChange method of the checkbox should not be called, as the checkbox is disabled.

Actual behavior

The onChange method of the checkbox is being called.

User-event version

14.5.2

Environment

If the Codesandbox is not available, it is easily reproducible by a single test file:

import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import { screen } from "@testing-library/dom";
import { userEvent } from "@testing-library/user-event";

describe("Checkbox", () => {
  it("does not execute onChange if checkbox is disabled", async () => {
    const onChange = vi.fn(); // also reproducible with jest.fn();
    render(
      <label>
        <input type="checkbox" onChange={onChange} data-testid="input" disabled />
        <span>My label</span>
      </label>
    );
    await userEvent.click(screen.getByText("My label"));
    expect(onChange).not.toHaveBeenCalled(); // this fails
  });
});

Additional context

If the same test is performed but using the onClick event, the test is successful and the event is not called. However, onChange should not be called either. When trying this same scenario in a real browser, onChange is correctly ignored.

Activity

  1. added
    bug
    needs assessment
    on Sep 23, 2024
  2. removed
    needs assessment
    on Oct 25, 2024
  3. yucheng-du commented on Dec 2, 2026

    @yucheng-du

    I tried the React example on both versions with React 18 and jsdom 20. On 14.5.2, clicking the span or the label checked the disabled input and fired one onChange. On 14.6.1, it stayed unchecked with zero calls. The enabled checkbox still fired once when clicked through either element. Chromium, Firefox and WebKit gave the same result as 14.6.1 for both states.

    #1130 shipped in 14.6.1 and covers this case. Updating @testing-library/user-event to that version fixed the reproduction here.

  4. snowystinger commented on Dec 2, 2026

    @snowystinger
    Contributor

    Thanks, I'm going to close this. I've added a PR to add an explicit regression test for it later as well #1361

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions