From b9e0af523b6a8b2e0a7a2163641897e5d18bb99d Mon Sep 17 00:00:00 2001 From: Miroslav Petrik Date: Tue, 5 Mar 2024 13:43:12 +0100 Subject: [PATCH] fix: reuse the buildItem method when adding items to the list --- src/atoms/list-atom/listAtom.ts | 14 ++++---------- src/hooks/use-list-actions/useListActions.ts | 9 +-------- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/src/atoms/list-atom/listAtom.ts b/src/atoms/list-atom/listAtom.ts index fea681d..5cc251c 100644 --- a/src/atoms/list-atom/listAtom.ts +++ b/src/atoms/list-atom/listAtom.ts @@ -66,7 +66,7 @@ export type ListAtom = ExtendFieldAtom< * @internal */ _formFields: Atom; - buildItem(): ListItemForm; + buildItem(fields?: Fields): ListItemForm; } >; @@ -102,22 +102,16 @@ export function listAtom< } } - function buildItem(): ListItemForm { + function buildItem(fields?: Fields): ListItemForm { return listItemForm({ - fields: formBuilder(), + fields: fields ?? formBuilder(), getListNameAtom: (get) => get(self).name, formListAtom: _formListAtom, }); } const makeFormList = (): ListItemForm[] => - formBuilder(config.value).map((fields) => - listItemForm({ - fields, - getListNameAtom: (get) => get(self).name, - formListAtom: _formListAtom, - }), - ); + formBuilder(config.value).map(buildItem); const initialFormListAtom = atomWithDefault(makeFormList); const _formListAtom = atomWithDefault((get) => get(initialFormListAtom)); diff --git a/src/hooks/use-list-actions/useListActions.ts b/src/hooks/use-list-actions/useListActions.ts index f325b10..9740e4e 100644 --- a/src/hooks/use-list-actions/useListActions.ts +++ b/src/hooks/use-list-actions/useListActions.ts @@ -3,7 +3,6 @@ import { useAtomValue, useSetAtom } from "jotai"; import { useCallback, useMemo, useTransition } from "react"; import { ListAtom, ListItem } from "../../atoms/list-atom"; -import { listItemForm } from "../../atoms/list-atom/listItemForm"; export const useListActions = ( listAtom: ListAtom, @@ -24,13 +23,7 @@ export const useListActions = ( const add = useCallback((before?: ListItem, fields?: Fields) => { dispatchSplitList({ type: "insert", - value: fields - ? listItemForm({ - fields, - getListNameAtom: (get) => get(listAtom).name, - formListAtom: atoms._formList, - }) - : atoms.buildItem(), + value: atoms.buildItem(fields), before, }); startTransition(() => {