diff --git a/src/components/list/Docs.mdx b/src/components/list/Docs.mdx index b01d04e..19004d0 100644 --- a/src/components/list/Docs.mdx +++ b/src/components/list/Docs.mdx @@ -149,6 +149,71 @@ const Example = () => ( + + +```tsx +import { type FieldAtom, fieldAtom, InputField } from "form-atoms"; +import { List, listField, useListActions } from "@form-atoms/list-atom"; + +const atom = listAtom({ + name: "productFeatures", + value: [{ feature: "quality materials" }, { feature: "solid build" }], + fields: ({ feature }) => ({ feature: fieldAtom({ value: feature }) }), +}); + +const Example = () => { + /* The useListAction hook enables you to control the listAtom from outside the List component */ + const listActions = useListActions(atom); + + const FreePositionAddButton = () => ( + + ); + + return ( + <>} + RemoveButton={RemoveButton} + Empty={() => { + return ( +
+

+ You don't have any items in your list. Start by adding your first + one. +

+ +
+ ); + }} + > + {({ fields, RemoveButton, index, count }) => ( +
+ +
+ {index + 1 === count && ( +
+ + +
+ )} +
+
+ )} +
+ ); +}; +``` + +
+ ```tsx diff --git a/src/components/list/List.stories.tsx b/src/components/list/List.stories.tsx index e718b4f..2c26ccf 100644 --- a/src/components/list/List.stories.tsx +++ b/src/components/list/List.stories.tsx @@ -7,13 +7,23 @@ import { fieldAtom, useFieldActions, } from "form-atoms"; +import { useState } from "react"; import { AddButtonProps, List, ListProps, RemoveButtonProps } from "./List"; import { listAtom } from "../../atoms"; +import { useListActions } from "../../hooks"; import { PicoFieldErrors } from "../../story/PicoFieldErrors"; import { PicoFieldName } from "../../story/PicoFieldName"; import { StoryForm } from "../../story/StoryForm"; +const Empty = () => ( +
+

+ You don't have any items in your list. Start by adding your first one. +

+
+); + const RemoveButton = ({ remove }: RemoveButtonProps) => ( + ); + + return ( + <>} + RemoveButton={RemoveButton} + Empty={() => { + return ( +
+

+ You don't have any items in your list. Start by adding your + first one. +

+ +
+ ); + }} + > + {({ fields, RemoveButton, index, count }) => ( +
+ +
+ {index + 1 === count && ( +
+ + +
+ )} +
+
+ )} +
+ ); + }, +}; + export const EmptyRenderProp = listStory({ parameters: { docs: { @@ -158,7 +236,7 @@ export const EmptyRenderProp = listStory({ }, args: { atom: listAtom({ - value: [{ hobby: "gardening" }], + value: [] as { hobby: string }[], fields: ({ hobby }) => ({ hobby: fieldAtom({ value: hobby }) }), }), AddButton: AddHobbyButton,