Skip to content

Commit

Permalink
Fix checkbox defaultChecked unexpected changing in form (#2135)
Browse files Browse the repository at this point in the history
  • Loading branch information
levinqdl authored Sep 26, 2024
1 parent 4f949f6 commit 25e29c5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .yarn/versions/a0df87d9.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
releases:
"@radix-ui/react-checkbox": patch

declined:
- primitives
62 changes: 62 additions & 0 deletions packages/react/checkbox/src/Checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,68 @@ describe('given a controlled `checked` Checkbox', () => {
});
});

describe('given an uncontrolled Checkbox in form', () => {
describe('when clicking the checkbox', () => {
it('should receive change event with target `defaultChecked` same as the `defaultChecked` prop of Checkbox', (done) => {
const rendered = render(
<form
onChange={(event) => {
const target = event.target as HTMLInputElement;
expect(target.defaultChecked).toBe(true);
}}
>
<CheckboxTest defaultChecked />
</form>
);
const checkbox = rendered.getByRole(CHECKBOX_ROLE);
fireEvent.click(checkbox);
rendered.rerender(
<form
onChange={(event) => {
const target = event.target as HTMLInputElement;
expect(target.defaultChecked).toBe(false);
done();
}}
>
<CheckboxTest defaultChecked={false} />
</form>
);
fireEvent.click(checkbox);
});
});
});

describe('given a controlled Checkbox in a form', () => {
describe('when clicking the checkbox', () => {
it('should receive change event with target `defaultChecked` same as initial value of `checked` of Checkbox', (done) => {
const rendered = render(
<form
onChange={(event) => {
const target = event.target as HTMLInputElement;
expect(target.defaultChecked).toBe(true);
}}
>
<CheckboxTest checked />
</form>
);
const checkbox = rendered.getByRole(CHECKBOX_ROLE);
fireEvent.click(checkbox);
rendered.rerender(
<form
onChange={(event) => {
const target = event.target as HTMLInputElement;
expect(target.defaultChecked).toBe(true);
done();
}}
>
<CheckboxTest checked={false} />
</form>
);
fireEvent.click(checkbox);
});
});
});

function CheckboxTest(props: React.ComponentProps<typeof Checkbox>) {
const containerRef = React.useRef<HTMLDivElement>(null);
React.useEffect(() => {
Expand Down
8 changes: 5 additions & 3 deletions packages/react/checkbox/src/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const CHECKBOX_NAME = 'Checkbox';
type ScopedProps<P> = P & { __scopeCheckbox?: Scope };
const [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME);

type CheckedState = boolean | 'indeterminate';
export type CheckedState = boolean | 'indeterminate';

type CheckboxContextValue = {
state: CheckedState;
Expand Down Expand Up @@ -112,6 +112,7 @@ const Checkbox = React.forwardRef<CheckboxElement, CheckboxProps>(
// rendered it **after** the button. This pulls it back to sit on top
// of the button.
style={{ transform: 'translateX(-100%)' }}
defaultChecked={isIndeterminate(defaultChecked) ? false : defaultChecked}
/>
)}
</CheckboxProvider>
Expand Down Expand Up @@ -167,7 +168,7 @@ interface BubbleInputProps extends Omit<InputProps, 'checked'> {
}

const BubbleInput = (props: BubbleInputProps) => {
const { control, checked, bubbles = true, ...inputProps } = props;
const { control, checked, bubbles = true, defaultChecked, ...inputProps } = props;
const ref = React.useRef<HTMLInputElement>(null);
const prevChecked = usePrevious(checked);
const controlSize = useSize(control);
Expand All @@ -187,11 +188,12 @@ const BubbleInput = (props: BubbleInputProps) => {
}
}, [prevChecked, checked, bubbles]);

const defaultCheckedRef = React.useRef(isIndeterminate(checked) ? false : checked);
return (
<input
type="checkbox"
aria-hidden
defaultChecked={isIndeterminate(checked) ? false : checked}
defaultChecked={defaultChecked ?? defaultCheckedRef.current}
{...inputProps}
tabIndex={-1}
ref={ref}
Expand Down

0 comments on commit 25e29c5

Please sign in to comment.