@zeno-lib/forms 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
- import { test } from "vitest"
1
+ import type { ReactNode } from "react"
2
+ import { expectTypeOf, test } from "vitest"
2
3
  import { z } from "zod"
3
4
  import { useForm } from "./create-form"
5
+ import { createZenoForm } from "./create-zeno-form"
4
6
 
5
7
  const schema = z.object({
6
8
  accept: z.boolean(),
@@ -93,3 +95,51 @@ test("listeners prop is accepted on every field wrapper", () => {
93
95
  name: "email",
94
96
  })
95
97
  })
98
+
99
+ test("ControlField types its control with the value at `name`", () => {
100
+ const form = useForm({
101
+ onSubmit: () => undefined,
102
+ schema: z.object({
103
+ phone: z.string().nullable(),
104
+ rating: z.number(),
105
+ }),
106
+ })
107
+ form.ControlField({
108
+ name: "phone",
109
+ render: (control) => {
110
+ expectTypeOf(control.value).toEqualTypeOf<string | null>()
111
+ return null
112
+ },
113
+ })
114
+ form.ControlField({
115
+ name: "rating",
116
+ render: (control) => {
117
+ control.onChange(3)
118
+ // @ts-expect-error — the field holds a number
119
+ control.onChange("3")
120
+ return null
121
+ },
122
+ })
123
+ // @ts-expect-error — not a valid path
124
+ form.ControlField({ name: "nonexistent", render: () => null })
125
+ })
126
+
127
+ test("a field whose `render` takes another argument keeps its own type", () => {
128
+ const { useForm: useListForm } = createZenoForm({
129
+ fieldComponents: {
130
+ ListField: (_: { render: (row: { id: string }) => ReactNode }) => null,
131
+ },
132
+ formComponents: {},
133
+ })
134
+ const form = useListForm({
135
+ onSubmit: () => undefined,
136
+ schema: z.object({ rows: z.array(z.string()) }),
137
+ })
138
+ form.ListField({
139
+ name: "rows",
140
+ render: (row) => {
141
+ expectTypeOf(row).toEqualTypeOf<{ id: string }>()
142
+ return null
143
+ },
144
+ })
145
+ })