Skip to content

Commit

Permalink
fix(listField): use ListAtomConfig with the invalidItemError (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik authored Jan 14, 2024
1 parent 0b1e474 commit 6d28e0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
23 changes: 17 additions & 6 deletions src/atoms/list-atom/listAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,29 @@ export type ListAtom<
}
>;

export type ListAtomConfig<
Fields extends ListAtomItems,
Value extends ListAtomValue<Fields>,
> = {
/**
* 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<FieldAtomConfig<Value[]>, "name" | "validate" | "value">;

/**
* @private
*/
export function listAtom<
Fields extends ListAtomItems,
Value extends ListAtomValue<Fields>,
>(
config: {
builder: (value: Value) => Fields;
invalidItemError?: string;
} & Pick<FieldAtomConfig<Value[]>, "name" | "validate" | "value">,
): ListAtom<Fields, Value> {
>(config: ListAtomConfig<Fields, Value>): ListAtom<Fields, Value> {
const nameAtom = atomWithReset(config.name);
const initialValueAtom = atomWithReset<Value | undefined>(undefined);

Expand Down
6 changes: 2 additions & 4 deletions src/fields/list-field/listField.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -65,9 +65,7 @@ export const listField = <
schema,
optionalSchema,
...config
}: {
builder: (value: Value) => Fields;
} & FieldAtomConfig<Value[]> &
}: ListAtomConfig<Fields, Value> &
ZodParams &
Partial<
ValidateConfig<ZodArray<ZodAny, "atleastone">, ZodArray<ZodAny, "many">>
Expand Down

0 comments on commit 6d28e0d

Please sign in to comment.