Skip to content

Commit 733e7a6

Browse files
authored
Merge pull request marmelab#10173 from julienV/10171-fix-simpleformiterator-doc
[Doc]: Fix SimpleFormIterator add and remove buttons snippets
2 parents 9139b67 + 66ae41a commit 733e7a6

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

docs/SimpleFormIterator.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,32 @@ const OrderEdit = () => (
9393
This prop lets you pass a custom element to replace the default Add button.
9494

9595
```jsx
96-
<SimpleFormIterator addButton={<Button>Add</Button>}>
96+
<SimpleFormIterator addButton={<MyAddButton label={"Add a line"} />}>
9797
<TextInput source="name" />
9898
<NumberInput source="price" />
9999
<NumberInput source="quantity" />
100100
</SimpleFormIterator>
101101
```
102102

103+
You need to provide an element that triggers the `add` function from `useSimpleFormIterator` when clicked. Here is an example:
104+
105+
```jsx
106+
import { ButtonProps, useSimpleFormIterator, useTranslate } from "react-admin";
107+
import React from "react";
108+
import Button from "@mui/material/Button";
109+
110+
export const MyAddButton = (props: ButtonProps) => {
111+
const { add } = useSimpleFormIterator();
112+
const translate = useTranslate();
113+
114+
return (
115+
<Button onClick={() => add()} {...props}>
116+
{translate(props.label ?? 'ra.action.add')}
117+
</Button>
118+
);
119+
};
120+
```
121+
103122
## `children`
104123
105124
A list of Input elements, that will be rendered on each row.
@@ -294,13 +313,37 @@ Without this prop, `<SimpleFormIterator>` will render one input per line.
294313
This prop lets you pass a custom element to replace the default Remove button.
295314
296315
```jsx
297-
<SimpleFormIterator removeButton={<Button>Remove</Button>}>
316+
<SimpleFormIterator removeButton={<MyRemoveButton label="Remove this line" />}>
298317
<TextInput source="name" />
299318
<NumberInput source="price" />
300319
<NumberInput source="quantity" />
301320
</SimpleFormIterator>
302321
```
303322
323+
You need to provide an element that triggers the `remove` function from `useSimpleFormIteratorItem` when clicked. Here is an example:
324+
325+
```jsx
326+
import * as React from 'react';
327+
import clsx from 'clsx';
328+
import { ButtonProps, useSimpleFormIteratorItem, useTranslate } from "react-admin";
329+
import Button from "@mui/material/Button";
330+
331+
export const MyRemoveButton = (props: Omit<ButtonProps, 'onClick'>) => {
332+
const { remove } = useSimpleFormIteratorItem();
333+
const translate = useTranslate();
334+
335+
return (
336+
<Button
337+
onClick={() => remove()}
338+
color="warning"
339+
{...props}
340+
>
341+
{translate(props.label ?? 'ra.action.remove')}
342+
</Button>
343+
);
344+
};
345+
```
346+
304347
## `reOrderButtons`
305348
306349
This prop lets you pass a custom element to replace the default Up and Down buttons. This custom element must use the `useSimpleFormIteratorItem` hook to access the current row index and reorder callback.

0 commit comments

Comments
 (0)