Skip to content

Commit

Permalink
docs: expose example for prepending items
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Apr 19, 2024
1 parent a01dcd9 commit 9d3654f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
55 changes: 46 additions & 9 deletions src/components/list/Docs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,16 @@ const hobbies = listAtom({
});

const Example = () => (
<List atom={hobbies} initialValue={["coding", "gardening", "mountain bike"]}>
<List
atom={hobbies}
initialValue={[
{ hobby: "coding" },
{ hobby: "gardening" },
{ hobby: "mountain bike" },
]}
>
{({ fields, moveUp, moveDown, RemoveButton }) => (
<div
style={{
display: "grid",
gridGap: 16,
gridTemplateColumns: "auto min-content min-content min-content",
}}
>
<fieldset role="group">
<InputField atom={fields.hobby} component="input" />
<button type="button" className="outline" onClick={moveUp}>
Up
Expand All @@ -220,7 +221,43 @@ const Example = () => (
Down
</button>
<RemoveButton />
</div>
</fieldset>
)}
</List>
);
```

</Example>

<Example of={ListStories.Prepend} >

```tsx
import { fieldAtom, InputField } from "form-atoms";
import { List, listAtom } from "@form-atoms/list-atom";

const hobbies = listAtom({
value: [],
name: "hobbies",
fields: ({ hobby }) => ({ hobby: fieldAtom({ value: hobby }) }),
});

const Example = () => (
<List
atom={hobbies}
initialValue={[
{ hobby: "swimming" },
{ hobby: "gardening" },
{ hobby: "coding" },
]}
>
{({ fields, RemoveButton, add, item }) => (
<fieldset role="group">
<InputField atom={fields.hobby} component="input" />
<button type="button" className="outline" onClick={() => add(item)}>
Prepend
</button>
<RemoveButton />
</fieldset>
)}
</List>
);
Expand Down
15 changes: 7 additions & 8 deletions src/components/list/List.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,20 @@ export const Prepend = listStory({
value: [{ hobby: "gardening" }],
fields: ({ hobby }) => ({ hobby: fieldAtom({ value: hobby }) }),
}),
initialValue: [
{ hobby: "swimming" },
{ hobby: "gardening" },
{ hobby: "coding" },
],
AddButton: AddHobbyButton,
children: ({ fields, RemoveButton, add, item }) => (
<div
style={{
display: "grid",
gridGap: 16,
gridTemplateColumns: "auto min-content min-content",
}}
>
<fieldset role="group">
<InputField atom={fields.hobby} component="input" />
<button type="button" className="outline" onClick={() => add(item)}>
Prepend
</button>
<RemoveButton />
</div>
</fieldset>
),
},
});
Expand Down

0 comments on commit 9d3654f

Please sign in to comment.