Skip to content

Commit 82d0cd6

Browse files
authored
Merge pull request #10837 from marmelab/marmelab/listRenderProp
Add render prop page and reference MUI components
2 parents dcc816b + 4fb60f5 commit 82d0cd6

40 files changed

+1050
-158
lines changed

docs/Create.md

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,25 @@ export default App;
5555

5656
You can customize the `<Create>` component using the following props:
5757

58-
* [`actions`](#actions): override the actions toolbar with a custom component
59-
* [`aside`](#aside): component to render aside to the main content
60-
* `children`: the components that renders the form
61-
* `className`: passed to the root component
62-
* [`component`](#component): override the root component
63-
* [`disableAuthentication`](#disableauthentication): disable the authentication check
64-
* [`mutationMode`](#mutationmode): switch to optimistic or undoable mutations (pessimistic by default)
65-
* [`mutationOptions`](#mutationoptions): options for the `dataProvider.create()` call
66-
* [`record`](#record): initialize the form with a record
67-
* [`redirect`](#redirect): change the redirect location after successful creation
68-
* [`resource`](#resource): override the name of the resource to create
69-
* [`sx`](#sx-css-api): Override the styles
70-
* [`title`](#title): override the page title
71-
* [`transform`](#transform): transform the form data before calling `dataProvider.create()`
58+
| Prop | Required | Type | Default | Description |
59+
|---------------------|----------|---------------------|----------------|--------------------------------------------------------------------------------------------------|
60+
| `children` | Optional&nbsp;* | `ReactNode` | - | The components that render the form |
61+
| `render` | Optional&nbsp;* | `function` | - | Alternative to children. Function that renders the form, receives the create context as argument |
62+
| `actions` | Optional | `ReactNode` | Default toolbar| Override the actions toolbar with a custom component |
63+
| `aside` | Optional | `ReactNode` | - | Component to render aside to the main content |
64+
| `className` | Optional | `string` | - | Passed to the root component |
65+
| `component` | Optional | `string`/`Component`| `Card` | Override the root component |
66+
| `disableAuthentication` | Optional | `boolean` | `false` | Disable the authentication check |
67+
| `mutationMode` | Optional | `string` | `pessimistic` | Switch to optimistic or undoable mutations |
68+
| `mutationOptions` | Optional | `object` | - | Options for the `dataProvider.create()` call |
69+
| `record` | Optional | `object` | `{}` | Initialize the form with a record |
70+
| `redirect` | Optional | `string`/`function` | `'edit'` | Change the redirect location after successful creation |
71+
| `resource` | Optional | `string` | From URL | Override the name of the resource to create |
72+
| `sx` | Optional | `object` | - | Override the styles |
73+
| `title` | Optional | `string`/`ReactNode`| Translation | Override the page title |
74+
| `transform` | Optional | `function` | - | Transform the form data before calling `dataProvider.create()` |
75+
76+
`*` You must provide either `children` or `render`.
7277

7378
## `actions`
7479

@@ -120,6 +125,28 @@ const PostCreate = () => (
120125

121126
{% endraw %}
122127

128+
## `children`
129+
130+
The `<Create>` component will render its children inside a [`CreateContext`](./useCreateContext.md#return-value). Children can be any React node, but are usually a form component like [`<SimpleForm>`](./SimpleForm.md), [`<TabbedForm>`](./TabbedForm.md), or the headless [`<Form>`](./Form.md) component.
131+
132+
```tsx
133+
import { Create, SimpleForm, TextInput, DateInput, required } from 'react-admin';
134+
import RichTextInput from 'ra-input-rich-text';
135+
136+
export const PostCreate = () => (
137+
<Create>
138+
<SimpleForm>
139+
<TextInput source="title" validate={[required()]} />
140+
<TextInput source="teaser" multiline={true} label="Short description" />
141+
<RichTextInput source="body" />
142+
<DateInput label="Publication date" source="published_at" defaultValue={new Date()} />
143+
</SimpleForm>
144+
</Create>
145+
);
146+
```
147+
148+
**Tip**: Alternatively to `children`, you can pass a [`render`](#render) prop to `<Create>`.
149+
123150
## `component`
124151

125152
By default, the `<Create>` view render the main form inside a Material UI `<Card>` element. The actual layout of the form depends on the `Form` component you're using ([`<SimpleForm>`](./SimpleForm.md), [`<TabbedForm>`](./TabbedForm.md), or a custom form component).
@@ -160,9 +187,9 @@ const PostCreate = () => (
160187

161188
The `<Create>` view exposes a Save button, which perform a "mutation" (i.e. it creates the data). React-admin offers three modes for mutations. The mode determines when the side effects (redirection, notifications, etc.) are executed:
162189

163-
- `pessimistic` (default): The mutation is passed to the dataProvider first. When the dataProvider returns successfully, the mutation is applied locally, and the side effects are executed.
164-
- `optimistic`: The mutation is applied locally and the side effects are executed immediately. Then the mutation is passed to the dataProvider. If the dataProvider returns successfully, nothing happens (as the mutation was already applied locally). If the dataProvider returns in error, the page is refreshed and an error notification is shown.
165-
- `undoable`: The mutation is applied locally and the side effects are executed immediately. Then a notification is shown with an undo button. If the user clicks on undo, the mutation is never sent to the dataProvider, and the page is refreshed. Otherwise, after a 5 seconds delay, the mutation is passed to the dataProvider. If the dataProvider returns successfully, nothing happens (as the mutation was already applied locally). If the dataProvider returns in error, the page is refreshed and an error notification is shown.
190+
* `pessimistic` (default): The mutation is passed to the dataProvider first. When the dataProvider returns successfully, the mutation is applied locally, and the side effects are executed.
191+
* `optimistic`: The mutation is applied locally and the side effects are executed immediately. Then the mutation is passed to the dataProvider. If the dataProvider returns successfully, nothing happens (as the mutation was already applied locally). If the dataProvider returns in error, the page is refreshed and an error notification is shown.
192+
* `undoable`: The mutation is applied locally and the side effects are executed immediately. Then a notification is shown with an undo button. If the user clicks on undo, the mutation is never sent to the dataProvider, and the page is refreshed. Otherwise, after a 5 seconds delay, the mutation is passed to the dataProvider. If the dataProvider returns successfully, nothing happens (as the mutation was already applied locally). If the dataProvider returns in error, the page is refreshed and an error notification is shown.
166193

167194
By default, pages using `<Create>` use the `pessimistic` mutation mode as the new record identifier is often generated on the backend. However, should you decide to generate this identifier client side, you can change the `mutationMode` to either `optimistic` or `undoable`:
168195

@@ -315,6 +342,37 @@ Note that the `redirect` prop is ignored if you set [the `mutationOptions` prop]
315342

316343
If you want to allow the user to enter several records one after the other, setting `redirect` to `false` won't make it, as the form isn't emptied by default. You'll have to empty the form using the `mutationOptions`, and this option disables the `redirect` prop. Check [the Save And Add Another section](#save-and-add-another) for more details.
317344

345+
## `render`
346+
347+
Alternatively to `children`, you can pass a `render` prop to `<Create>`. It will receive the [`CreateContext`](./useCreateContext.md#return-value) as its argument, and should return a React node.
348+
349+
This allows to inline the render logic for the create page.
350+
351+
{% raw %}
352+
353+
```tsx
354+
const PostCreate = () => ()
355+
<Create render={({ save, saving }) => (
356+
<div>
357+
<h1>Create new Post</h1>
358+
<form onSubmit={save}>
359+
<input type="text" name="title" placeholder="Title" required />
360+
<textarea name="teaser" placeholder="Short description" rows={3} />
361+
<textarea name="body" placeholder="Body" rows={5} />
362+
<input type="date" name="published_at" defaultValue={new Date().toISOString().split('T')[0]} />
363+
<button type="submit" disabled={saving}>
364+
{saving ? 'Saving...' : 'Save'}
365+
</button>
366+
</form>
367+
</div>
368+
)} />
369+
);
370+
```
371+
372+
{% endraw %}
373+
374+
**Tip**: When receiving a `render` prop, the `<Create>` component will ignore the `children` prop.
375+
318376
## `resource`
319377

320378
Components based on `<Create>` are often used as `<Resource create>` props, and therefore rendered when the URL matches `/[resource]/create`. The `<Create>` component generates a call to `dataProvider.create()` using the resource name from the URL by default.

0 commit comments

Comments
 (0)