@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.
- package/dist/create-zeno-form.d.mts +8 -2
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -1
- package/dist/lib/submit-action.d.mts +8 -2
- package/dist/lib/submit-action.mjs +4 -2
- package/dist/lib/use-field-control.d.mts +76 -0
- package/dist/lib/use-field-control.mjs +67 -0
- package/package.json +1 -1
- package/src/control-field.test.tsx +338 -0
- package/src/create-form.tsx +3 -0
- package/src/create-zeno-form.tsx +20 -1
- package/src/fields/control-field.tsx +57 -0
- package/src/fields/field-shell.tsx +73 -0
- package/src/fields/index.ts +2 -0
- package/src/fields/input-field.tsx +14 -48
- package/src/fields/textarea-field.tsx +14 -48
- package/src/index.ts +6 -0
- package/src/lib/submit-action.ts +18 -4
- package/src/lib/use-field-control.ts +181 -0
- package/src/submit-action.test.tsx +103 -0
- package/src/use-app-fields.test-d.ts +51 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
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
|
+
})
|