Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Field): Fix Checkbox is not reacting to Form defaultValue bug #1191

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Button } from "@/components/Button";
import { Label } from "@/components/Label";
import { act, fireEvent, render, screen } from "@testing-library/react";
import { Switch } from "@/components/Switch";
import Checkbox from "@/components/Checkbox";

const handleSubmit = vitest.fn();

Expand Down Expand Up @@ -91,3 +92,36 @@ describe("Switch field", () => {
);
});
});

describe("Checkbox field", () => {
interface FormValues {
isActive: boolean;
}

const TestForm = () => {
const form = useForm<FormValues>({
defaultValues: {
isActive: true,
},
});

const Field = typedField(form);

return (
<Form form={form} onSubmit={(values) => handleSubmit(values)}>
<Field name="isActive">
<Checkbox data-testid="checkbox" />
</Field>
<Button type="submit">Submit</Button>
</Form>
);
};

test("switch uses default value", async () => {
render(<TestForm />);

expect(screen.getByTestId("checkbox").getAttribute("data-selected")).toBe(
"true",
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export function Field<T extends FieldValues>(props: Props<T>) {
};

const propsContext: PropsContext = {
Checkbox: formControlProps,
Checkbox: {
...formControlProps,
isSelected: formControlProps.value,
},
CheckboxGroup: formControlProps,
CheckboxButton: formControlProps,
FileField: formControlProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const meta: Meta<typeof Field> = {
lastName: "",
gender: "",
storage: 200,
interests: ["foo"],
testing: true,
},
});
Expand Down