The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(ui): keep Checkbox state controlled (#24516)
Félix Malfaite6bbd6332c9b
diff --git a/packages/twenty-ui/src/input/Checkbox/Checkbox.tsx b/packages/twenty-ui/src/input/Checkbox/Checkbox.tsxindex a605459926d..aa87a00f8bf 100644--- a/packages/twenty-ui/src/input/Checkbox/Checkbox.tsx+++ b/packages/twenty-ui/src/input/Checkbox/Checkbox.tsx@@ -1,6 +1,6 @@import { Checkbox as CheckboxPrimitive } from '@base-ui/react/checkbox';import { clsx } from 'clsx';-import * as React from 'react';+import { type ChangeEvent } from 'react';import { IconCheck, IconMinus } from '@ui/icon/components/TablerIcons';@@ -31,7 +31,7 @@ type CheckboxProps = {checked: boolean;indeterminate?: boolean;hoverable?: boolean;- onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;+ onChange?: (event: ChangeEvent<HTMLInputElement>) => void;onCheckedChange?: (value: boolean) => void;variant?: CheckboxVariant;size?: CheckboxSize;@@ -60,16 +60,9 @@ export const Checkbox = ({'aria-label': ariaLabel,'aria-labelledby': ariaLabelledby,}: CheckboxProps) => {- const [isInternalChecked, setIsInternalChecked] =- React.useState<boolean>(false);-- React.useEffect(() => {- setIsInternalChecked(checked ?? false);- }, [checked]);-return (<CheckboxPrimitive.Root- checked={isInternalChecked}+ checked={checked}indeterminate={indeterminate}disabled={disabled}id={id}@@ -79,10 +72,9 @@ export const Checkbox = ({data-testid="input-checkbox"onCheckedChange={(value, eventDetails) => {onChange?.(- eventDetails.event as unknown as React.ChangeEvent<HTMLInputElement>,+ eventDetails.event as unknown as ChangeEvent<HTMLInputElement>,);onCheckedChange?.(value);- setIsInternalChecked(value);}}className={clsx(styles.root,diff --git a/packages/twenty-ui/src/input/Checkbox/__tests__/Checkbox.test.tsx b/packages/twenty-ui/src/input/Checkbox/__tests__/Checkbox.test.tsxnew file mode 100644index 00000000000..e0afb1ef060--- /dev/null+++ b/packages/twenty-ui/src/input/Checkbox/__tests__/Checkbox.test.tsx@@ -0,0 +1,52 @@+import { render, screen } from '@testing-library/react';+import userEvent from '@testing-library/user-event';++import { Checkbox } from '../Checkbox';++describe('Checkbox', () => {+ const originalPointerEvent = window.PointerEvent;++ beforeAll(() => {+ Object.defineProperty(window, 'PointerEvent', {+ configurable: true,+ value: MouseEvent,+ });+ });++ afterAll(() => {+ Object.defineProperty(window, 'PointerEvent', {+ configurable: true,+ value: originalPointerEvent,+ });+ });++ it('keeps the rendered state controlled by the checked prop', async () => {+ const user = userEvent.setup();+ const onCheckedChange = jest.fn();+ const { rerender } = render(+ <Checkbox+ checked={false}+ onCheckedChange={onCheckedChange}+ aria-label="Controlled checkbox"+ />,+ );+ const checkbox = screen.getByRole('checkbox', {+ name: 'Controlled checkbox',+ });++ await user.click(checkbox);++ expect(onCheckedChange).toHaveBeenCalledWith(true);+ expect(checkbox).not.toBeChecked();++ rerender(+ <Checkbox+ checked+ onCheckedChange={onCheckedChange}+ aria-label="Controlled checkbox"+ />,+ );++ expect(checkbox).toBeChecked();+ });+});