From 9a8f80013d0c43bea9658efa99c5731e110ad2ce Mon Sep 17 00:00:00 2001 From: Miroslav Petrik Date: Thu, 18 Jan 2024 14:05:56 +0100 Subject: [PATCH] fix(useFieldProps): enable passing initialValue via options --- src/hooks/use-field-props/FieldProps.ts | 13 +++++++++++++ src/hooks/use-field-props/index.ts | 1 + src/hooks/use-field-props/useFieldProps.ts | 16 +++++----------- 3 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 src/hooks/use-field-props/FieldProps.ts diff --git a/src/hooks/use-field-props/FieldProps.ts b/src/hooks/use-field-props/FieldProps.ts new file mode 100644 index 0000000..15ff008 --- /dev/null +++ b/src/hooks/use-field-props/FieldProps.ts @@ -0,0 +1,13 @@ +import { UseFieldOptions } from "form-atoms"; +import { ReactNode } from "react"; + +import type { ZodField, ZodFieldValue } from "../../fields"; + +/** + * Common Props to be used on FieldComponents like CheckboxField or NumberField. + */ +export type FieldProps = { + field: Field; + label?: ReactNode; + required?: boolean; +} & Pick>, "initialValue">; diff --git a/src/hooks/use-field-props/index.ts b/src/hooks/use-field-props/index.ts index 156858f..5c358ad 100644 --- a/src/hooks/use-field-props/index.ts +++ b/src/hooks/use-field-props/index.ts @@ -1 +1,2 @@ export * from "./useFieldProps"; +export * from "./FieldProps"; diff --git a/src/hooks/use-field-props/useFieldProps.ts b/src/hooks/use-field-props/useFieldProps.ts index 509a0b5..f6db0db 100644 --- a/src/hooks/use-field-props/useFieldProps.ts +++ b/src/hooks/use-field-props/useFieldProps.ts @@ -1,15 +1,8 @@ -import { useField } from "form-atoms"; +import { UseFieldOptions, useField } from "form-atoms"; import { useAtomValue, useSetAtom } from "jotai"; -import { ChangeEvent, ReactNode, useMemo, useTransition } from "react"; +import { ChangeEvent, useMemo, useTransition } from "react"; -import { ZodField, ZodFieldValue } from "../../fields/zod-field/zodField"; - -export type FieldProps> = { - field: Field; - label?: ReactNode; - helperText?: ReactNode; - required?: boolean; -}; +import { ZodField, ZodFieldValue } from "../../fields"; export function useFieldProps< Field extends ZodField, @@ -23,8 +16,9 @@ export function useFieldProps< value: ZodFieldValue, ) => ZodFieldValue : never, + options?: UseFieldOptions>, ) { - const { actions, state } = useField>(fieldAtom); + const { actions, state } = useField>(fieldAtom, options); const field = useAtomValue(fieldAtom); const name = useAtomValue(field.name); const required = useAtomValue(field.required);