Skip to content

Commit afacca7

Browse files
committed
Add headless documentation
1 parent ad72de4 commit afacca7

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
layout: default
3+
title: "<ArrayInputBase>"
4+
---
5+
6+
`<ArrayInputBase>` allows editing of embedded arrays, like the `items` field in the following `order` record:
7+
8+
```json
9+
{
10+
"id": 1,
11+
"date": "2022-08-30",
12+
"customer": "John Doe",
13+
"items": [
14+
{
15+
"name": "Office Jeans",
16+
"price": 45.99,
17+
"quantity": 1,
18+
},
19+
{
20+
"name": "Black Elegance Jeans",
21+
"price": 69.99,
22+
"quantity": 2,
23+
},
24+
{
25+
"name": "Slim Fit Jeans",
26+
"price": 55.99,
27+
"quantity": 1,
28+
},
29+
],
30+
}
31+
```
32+
33+
## Usage
34+
35+
`<ArrayInputBase>` expects a single child, which must be a *form iterator* component. A form iterator is a component rendering a field array (the object returned by react-hook-form's [`useFieldArray`](https://react-hook-form.com/docs/usefieldarray)). You can build such component using [the `<SimpleFormIteratorBase>`](./SimpleFormIteratorBase.md).
36+
37+
```tsx
38+
import { ArrayInputBase, EditBase, Form } from 'ra-core';
39+
import { MyFormIterator } from './MyFormIterator';
40+
import { DateInput } from './DateInput';
41+
import { NumberInput } from './NumberInput';
42+
import { TextInput } from './TextInput';
43+
44+
export const OrderEdit = () => (
45+
<EditBase>
46+
<Form>
47+
<DateInput source="date" />
48+
<div>
49+
<div>Tags:</div>
50+
<ArrayInputBase source="tags">
51+
<MyFormIterator>
52+
<TextInput source="name" />
53+
<NumberInput source="price" />
54+
<NumberInput source="quantity" />
55+
</MyFormIterator>
56+
</ArrayInputBase>
57+
</div>
58+
<button type="submit">Save</button>
59+
</Form>
60+
</EditBase>
61+
)
62+
```
63+
64+
**Note**: Setting [`shouldUnregister`](https://react-hook-form.com/docs/useform#shouldUnregister) on a form should be avoided when using `<ArrayInputBase>` (which internally uses `useFieldArray`) as the unregister function gets called after input unmount/remount and reorder. This limitation is mentioned in the react-hook-form [documentation](https://react-hook-form.com/docs/usecontroller#props). If you are in such a situation, you can use the [`transform`](./EditBase.html#transform) prop to manually clean the submitted values.
65+
66+
## Props
67+
68+
| Prop | Required | Type | Default | Description |
69+
|-----------------| -------- |---------------------------| ------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
70+
| `source` | Required | `string` | - | Name of the entity property to use for the input value |
71+
| `defaultValue` | Optional | `any` | - | Default value of the input. |
72+
| `readOnly` | Optional | `boolean` | `false` | If true, the input is in read-only mode. |
73+
| `disabled` | Optional | `boolean` | `false` | If true, the input is disabled. |
74+
| `validate` | Optional | `Function` &#124; `array` | - | Validation rules for the current property. See the [Validation Documentation](./Validation.md#per-input-validation-built-in-field-validators) for details. |
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
layout: default
3+
title: "<SimpleFormIteratorBase>"
4+
---
5+
6+
`<SimpleFormIteratorBase>` helps building a component that lets users edit, add, remove and reorder sub-records. It is designed to be used as a child of [`<ArrayInputBase>`](./ArrayInputBase.md) or [`<ReferenceManyInput>`](./ReferenceManyInput.md). You can also use it within an `ArrayInputContext` containing a *field array*, i.e. the value returned by [react-hook-form's `useFieldArray` hook](https://react-hook-form.com/docs/usefieldarray).
7+
8+
## Usage
9+
10+
Here's one could implement a minimal `SimpleFormIterator` using `<SimpleFormIteratorBase>`:
11+
12+
```tsx
13+
export const SimpleFormIterator = ({ children, ...props }: SimpleFormIteratorBaseProps) => {
14+
const { fields, replace } = useArrayInput(props);
15+
const records = useFieldValue({ source: finalSource });
16+
17+
return (
18+
<SimpleFormIteratorBase {...props}>
19+
<ul>
20+
{fields.map((member, index) => (
21+
<SimpleFormIteratorItemBase
22+
key={member.id}
23+
fields={fields}
24+
index={index}
25+
>
26+
<li>
27+
{props.children}
28+
</li>
29+
</SimpleFormIteratorItemBase>
30+
))}
31+
</ul>
32+
</SimpleFormIteratorBase>
33+
)
34+
}
35+
```
36+
37+
## Props
38+
39+
| Prop | Required | Type | Default | Description |
40+
|------------|----------|----------------|-----------------------|-----------------------------------------------|
41+
| `children` | Optional | `ReactElement` | - | List of inputs to display for each array item |

0 commit comments

Comments
 (0)