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 = () => ( + listActions.add()}> + Add + + ); + + 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) => ( Remove @@ -31,6 +41,7 @@ const meta = { args: { AddButton, RemoveButton, + Empty, }, }; @@ -147,6 +158,73 @@ export const CustomAddButton = listStory({ }, }); +export const PositioningAddButton = { + parameters: { + docs: { + description: { + story: + "You can disable the default add button by setting a no-render `AddButton` prop. You can control the list from outside by the `useListActions` hook. Here we render a button with `add` action only besides the last item in the list.", + }, + }, + }, + render: () => { + const [atom] = useState(() => + listAtom({ + name: "productFeatures", + value: [{ feature: "quality materials" }, { feature: "solid build" }], + fields: ({ feature }) => ({ feature: fieldAtom({ value: feature }) }), + }), + ); + + const listActions = useListActions(atom); + + const FreePositionAddButton = () => ( + listActions.add()}> + Add + + ); + + 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,
+ You don't have any items in your list. Start by adding your first + one. +
+ You don't have any items in your list. Start by adding your first one. +
+ You don't have any items in your list. Start by adding your + first one. +