Skip to content

Commit

Permalink
feat: add fieldValidators
Browse files Browse the repository at this point in the history
  • Loading branch information
Makisuo committed Jul 22, 2024
1 parent e86858c commit 972cc4c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-llamas-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pixelshades/auto-form": patch
---

add fieldValidators Props
15 changes: 7 additions & 8 deletions apps/docs/src/app/test/_comp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { AutoForm, AutoFormSubmit } from "@pixelshades/auto-form"
import { z } from "zod"

const formSchema = z.object({
// username: z
// .string({
// required_error: "Username is required.",
// })
// .min(2, {
// message: "Username must be at least 2 characters.",
// }),
username: z
.string({
required_error: "Username is required.",
})
.min(2, {
message: "Username must be at least 2 characters.",
}),

// password: z
// .string({
Expand Down Expand Up @@ -71,7 +71,6 @@ const formSchema = z.object({
export function Test() {
return (
<AutoForm
fieldConfig={{}}
defaultValues={{
birthday: new Date("Tue Jul 23 2024 00:00:00 GMT+0200"),
}}
Expand Down
1 change: 1 addition & 0 deletions packages/auto-form/src/fields/auto-form-object.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function AutoFormObject<SchemaType extends z.ZodObject<any, any>>({
key={key}
validators={{
onChange: item,
...fieldConfigItem.fieldValidators,
}}
// biome-ignore lint/correctness/noChildrenProp: <explanation>
children={(field) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/auto-form/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { FieldApi } from "@tanstack/react-form"
import type { FieldApi, FieldValidators } from "@tanstack/react-form"
import type * as z from "zod"
import type { INPUT_COMPONENTS } from "./config"

// TODO: Renable zodEffects
export type ZodObjectOrWrapped = z.ZodObject<any, any> | z.ZodEffects<z.ZodObject<any, any>>

export type FieldConfigItem = {
Expand All @@ -15,6 +14,8 @@ export type FieldConfigItem = {
renderParent?: (props: {
children: React.ReactNode
}) => React.ReactElement | null

fieldValidators?: Omit<FieldValidators<any, any>, "onChange">
}

export type FieldConfig<SchemaType extends z.infer<z.ZodObject<any, any>>> = {
Expand Down

0 comments on commit 972cc4c

Please sign in to comment.