From b4eb80df7bc148e8d1527ad54228eaa03879a34b Mon Sep 17 00:00:00 2001 From: Sterling Camden Date: Mon, 3 Apr 2023 18:02:28 -0400 Subject: [PATCH 1/2] fix submit types --- src/createSchemaForm.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/createSchemaForm.tsx b/src/createSchemaForm.tsx index e6d0402..3c6a9e2 100644 --- a/src/createSchemaForm.tsx +++ b/src/createSchemaForm.tsx @@ -252,7 +252,7 @@ export type RTFFormProps< /** * A callback function that will be called with the data once the form has been submitted and validated successfully. */ - onSubmit: RTFFormSubmitFn; + onSubmit: RTFFormSubmitFn; /** * Initializes your form with default values. Is a deep partial, so all properties and nested properties are optional. */ @@ -584,7 +584,7 @@ function useSubmitter({ setError, }: { resolver: ReturnType; - onSubmit: RTFFormSubmitFn; + onSubmit: RTFFormSubmitFn; setError: ReturnType["setError"]; }) { const coerceUndefinedFieldsRef = useRef>(new Set()); From cb4979165556ff4268b0ea9c39b5ba89767f4e4a Mon Sep 17 00:00:00 2001 From: Sterling Camden Date: Mon, 3 Apr 2023 18:12:28 -0400 Subject: [PATCH 2/2] test(onsubmit): test the onsubmit for proper typing --- src/__tests__/createSchemaForm.test.tsx | 42 ++++++++++++++++++++++++- src/__tests__/utils/testForm.tsx | 9 ++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/__tests__/createSchemaForm.test.tsx b/src/__tests__/createSchemaForm.test.tsx index d2ae9db..8ff8f91 100644 --- a/src/__tests__/createSchemaForm.test.tsx +++ b/src/__tests__/createSchemaForm.test.tsx @@ -37,12 +37,14 @@ const testIds = { booleanField: "_boolean-field", }; +function assertNever(_thing: never) {} + describe("createSchemaForm", () => { it("should render a text field and a boolean field based on the mapping and schema", () => { const testSchema = z.object({ textField: z.string(), textFieldTwo: z.string(), - booleanField: z.string(), + booleanField: z.boolean(), t: z.string(), t2: z.string(), t3: z.string(), @@ -72,6 +74,44 @@ describe("createSchemaForm", () => { expect(screen.queryByTestId(testIds.textFieldTwo)).toBeTruthy(); expect(screen.queryByTestId(testIds.booleanField)).toBeTruthy(); }); + it("should type the onSubmit properly", () => { + const testSchema = z.object({ + textField: z.string(), + numberField: z.number(), + booleanField: z.boolean(), + }); + + render( + { + if (typeof v.textField !== "string") { + assertNever(v.textField); + } + if (typeof v.numberField !== "number") { + assertNever(v.numberField); + } + if (typeof v.booleanField !== "boolean") { + assertNever(v.booleanField); + } + }} + schema={testSchema} + props={{ + textField: { + testId: testIds.textField, + }, + numberField: { + testId: "number-field", + }, + booleanField: { + testId: testIds.booleanField, + }, + }} + /> + ); + + // this test is just about types + expect(true).toBe(true); + }); it("should render a text field and a boolean field based on the mapping and schema into slots in a custom form", () => { const testSchema = z.object({ textField: z.string(), diff --git a/src/__tests__/utils/testForm.tsx b/src/__tests__/utils/testForm.tsx index c06e88c..d131ff6 100644 --- a/src/__tests__/utils/testForm.tsx +++ b/src/__tests__/utils/testForm.tsx @@ -40,6 +40,14 @@ function BooleanField(props: { return ; } +function NumberField(props: { + control: Control; + name: string; + testId: string; +}) { + return ; +} + export const customFieldTestId = "custom"; function CustomTextField(props: { @@ -81,6 +89,7 @@ export const TestCustomFieldSchema = createUniqueFieldSchema(z.string(), "id"); const mapping = [ [z.string(), TextField] as const, [z.boolean(), BooleanField] as const, + [z.number(), NumberField] as const, [TestCustomFieldSchema, CustomTextField] as const, [z.enum(enumFieldValues), EnumField] as const, ] as const;