Skip to content

Commit c688ee8

Browse files
authored
fix: 🐛 fix SelectField of Shadcn UI (#154)
Select field give validation error even if value is selected Default value for select from zod schema not pre selected Add Test case for Shadcn zod enum type
1 parent 37a3ada commit c688ee8

File tree

3 files changed

+86
-6
lines changed

3 files changed

+86
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import React from "react";
2+
import { AutoForm } from "@autoform/shadcn/components/ui/autoform/AutoForm";
3+
import { ZodProvider } from "@autoform/zod";
4+
import { z } from "zod";
5+
import { TestWrapper } from "./utils";
6+
7+
describe("AutoForm Enums Tests", () => {
8+
const arraySchema = z.object({
9+
gender: z.enum(["male", "female"]),
10+
});
11+
const schemaProvider = new ZodProvider(arraySchema);
12+
13+
const arraySchemaWithDefault = z.object({
14+
gender: z.enum(["male", "female"]).default("male"),
15+
});
16+
const schemaProviderWithDefault = new ZodProvider(arraySchemaWithDefault);
17+
18+
it("renders enums fields correctly", () => {
19+
cy.mount(
20+
<TestWrapper>
21+
<AutoForm
22+
schema={schemaProvider}
23+
onSubmit={cy.stub().as("onSubmit")}
24+
withSubmit
25+
/>
26+
</TestWrapper>,
27+
);
28+
29+
// Check if select trigger exists
30+
cy.get('[role="combobox"]').should("exist");
31+
32+
// Open the select dropdown
33+
cy.get('[role="combobox"]').click();
34+
35+
// Verify options are rendered correctly
36+
cy.get('[role="option"]').should("have.length", 2);
37+
cy.get('[role="option"]').eq(0).should("have.text", "male");
38+
cy.get('[role="option"]').eq(1).should("have.text", "female");
39+
40+
// Test form submission with selected value
41+
cy.get('[role="option"]').contains("female").click();
42+
cy.get("form").submit();
43+
cy.get("@onSubmit").should("have.been.calledWith", {
44+
gender: "female",
45+
});
46+
});
47+
48+
it("handles default values for enums correctly", () => {
49+
cy.mount(
50+
<TestWrapper>
51+
<AutoForm
52+
schema={schemaProviderWithDefault}
53+
onSubmit={cy.stub().as("onSubmit")}
54+
withSubmit
55+
/>
56+
</TestWrapper>,
57+
);
58+
59+
// Verify default value is selected
60+
cy.get('[role="combobox"]').should("contain.text", "male");
61+
62+
// Submit form without changes to verify default value
63+
cy.get("form").submit();
64+
cy.get("@onSubmit").should("have.been.calledWith", {
65+
gender: "male",
66+
});
67+
});
68+
});

package-lock.json

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/shadcn/src/components/ui/autoform/components/SelectField.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ export const SelectField: React.FC<AutoFormFieldProps> = ({
1717
const { key, ...props } = inputProps;
1818

1919
return (
20-
<Select {...props}>
20+
<Select
21+
{...props}
22+
onValueChange={(value) => {
23+
const syntheticEvent = {
24+
target: {
25+
value,
26+
name: field.key,
27+
},
28+
} as React.ChangeEvent<HTMLInputElement>;
29+
props.onChange(syntheticEvent);
30+
}}
31+
defaultValue={field.default}
32+
>
2133
<SelectTrigger id={id} className={error ? "border-destructive" : ""}>
2234
<SelectValue placeholder="Select an option" />
2335
</SelectTrigger>

0 commit comments

Comments
 (0)