From 6d28e0dba60a001d2c18a5ea789fc5466b52f317 Mon Sep 17 00:00:00 2001 From: MiroslavPetrik Date: Sun, 14 Jan 2024 09:51:56 +0100 Subject: [PATCH] fix(listField): use ListAtomConfig with the invalidItemError (#74) --- src/atoms/list-atom/listAtom.ts | 23 +++++++++++++++++------ src/fields/list-field/listField.ts | 6 ++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/atoms/list-atom/listAtom.ts b/src/atoms/list-atom/listAtom.ts index aac3b34..9c4ee66 100644 --- a/src/atoms/list-atom/listAtom.ts +++ b/src/atoms/list-atom/listAtom.ts @@ -72,18 +72,29 @@ export type ListAtom< } >; +export type ListAtomConfig< + Fields extends ListAtomItems, + Value extends ListAtomValue, +> = { + /** + * The item builder expects a pure function creating the form fields to be used for each item. + * @param value The item value. + * @returns The item fields. + */ + builder: (value: Value) => Fields; + /** + * Error message the listAtom will have, when the its items have nested errors. + */ + invalidItemError?: string; +} & Pick, "name" | "validate" | "value">; + /** * @private */ export function listAtom< Fields extends ListAtomItems, Value extends ListAtomValue, ->( - config: { - builder: (value: Value) => Fields; - invalidItemError?: string; - } & Pick, "name" | "validate" | "value">, -): ListAtom { +>(config: ListAtomConfig): ListAtom { const nameAtom = atomWithReset(config.name); const initialValueAtom = atomWithReset(undefined); diff --git a/src/fields/list-field/listField.ts b/src/fields/list-field/listField.ts index e8c3758..0ae807f 100644 --- a/src/fields/list-field/listField.ts +++ b/src/fields/list-field/listField.ts @@ -1,10 +1,10 @@ -import { FieldAtomConfig } from "form-atoms"; import { Atom } from "jotai"; import { ZodAny, ZodArray, z } from "zod"; import { extendFieldAtom } from "../../atoms/extendFieldAtom"; import { ListAtom, + ListAtomConfig, ListAtomItems, ListAtomSubmitValue, ListAtomValue, @@ -65,9 +65,7 @@ export const listField = < schema, optionalSchema, ...config -}: { - builder: (value: Value) => Fields; -} & FieldAtomConfig & +}: ListAtomConfig & ZodParams & Partial< ValidateConfig, ZodArray>