diff --git a/src/index.tsx b/src/index.tsx index 83e3fc9..9d75816 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -67,7 +67,7 @@ export const Checkbox = forwardRef((props, ref) => { return; } - if (!('checked' in props)) { + if (checked === undefined) { setRawValue(e.target.checked); } diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 18aaac9..1c50d35 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -62,6 +62,19 @@ describe('rc-checkbox', () => { expect(container.firstChild).toMatchSnapshot(); }); + it('works in uncontrolled mode when checked prop is explicitly undefined', () => { + const { container } = render(); + const inputEl = container.querySelector('input')!; + + expect(inputEl.checked).toBe(false); + + fireEvent.click(inputEl); + expect(inputEl.checked).toBe(true); + + fireEvent.click(inputEl); + expect(inputEl.checked).toBe(false); + }); + it('stopPropagation and preventDefault', () => { const onChange = jest.fn();