Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doc] Document <EditInDialogButton> deletion side effect #9425

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
311 changes: 297 additions & 14 deletions docs/EditInDialogButton.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,47 @@ title: "EditInDialogButton"

# `<EditInDialogButton>`

This [Enterprise Edition](https://marmelab.com/ra-enterprise)<img class="icon" src="./img/premium.svg" /> component offers a way to open an `<Edit>` view inside a dialog, hence allowing to edit a record without leaving the current view.

It can be useful in case you want the ability to edit a record linked by a reference to the currently edited record, or if you have a nested `<Datagrid>` inside a `<Show>` or an `<Edit>` view.
This [Enterprise Edition](https://marmelab.com/ra-enterprise)<img class="icon" src="./img/premium.svg" /> component renders a button opening an `<Edit>` view inside a dialog, hence allowing to edit a record without leaving the current view.

<video controls autoplay playsinline muted loop>
<source src="https://marmelab.com/ra-enterprise/modules/assets/ra-form-layout/latest/InDialogButtons.webm" type="video/webm" />
<source src="https://marmelab.com/ra-enterprise/modules/assets/ra-form-layout/latest/InDialogButtons.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>

It can be useful in case you want the ability to edit a record linked by a reference to the currently edited record, or if you have a nested `<Datagrid>` inside a `<Show>` or an `<Edit>` view.

Note that this component doesn't use routing, so it doesn't change the URL. It's therefore not possible to bookmark the edit dialog, or to link to it from another page. If you need that functionality, use [`<EditDialog>`](./EditDialog.md) instead.

## Usage

Put `<EditInDialogButton>` wherever you would put an `<EditButton>`, and use the same children as you would for an [`<Edit>`](./Edit.md) component (e.g. a `<SimpleForm>`):
First, install the `@react-admin/ra-form-layout` package:

```sh
npm install --save @react-admin/ra-form-layout
# or
yarn add @react-admin/ra-form-layout
```

**Tip**: [`ra-form-layout`](https://marmelab.com/ra-enterprise/modules/ra-form-layout#createindialogbutton-editindialogbutton-and-EditInDialogButton) is hosted in a private npm registry. You need to subscribe to one of the [Enterprise Edition](https://marmelab.com/ra-enterprise/) plans to access this package.

Then, put `<EditInDialogButton>` wherever you would put an `<EditButton>`, and use the same children as you would for an [`<Edit>`](./Edit.md) component (e.g. a `<SimpleForm>`):

```jsx
import {
Datagrid,
ReferenceManyField,
Show,
SimpleForm,
SimpleShowLayout,
SimpleForm,
TextField,
TextInput,
} from "react-admin";
import { EditInDialogButton } from "@react-admin/ra-form-layout";

const CompanyShow = () => (
<Show>
<SimpleShowLayout>
<SimpleForm>
<TextField source="name" />
<TextField source="address" />
<TextField source="city" />
Expand All @@ -51,20 +61,293 @@ const CompanyShow = () => (
</EditInDialogButton>
</Datagrid>
</ReferenceManyField>
</SimpleShowLayout>
</SimpleForm>
</Show>
);
```

It accepts the following props:
## Props

`<EditInDialogButton>` accepts the following props:

| Prop | Required | Type | Default | Description |
| -------------- | -------- | ----------------- | ------- | ----------- |
| `children` | Required | `ReactNode` | | The content of the dialog. |
| `ButtonProps` | Optional | `object` | | Object containing props to pass to Material UI's `<Button>`. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| `ButtonProps` | Optional | `object` | | Object containing props to pass to Material UI's `<Button>`. |
| `ButtonProps` | Optional | `object` | | Object containing props to pass to Material UI's [`<Button>`](https://mui.com/material-ui/react-button/). |

| `empty WhileLoading` | Optional | `boolean` | | Set to `true` to return `null` while the list is loading. |
| `fullWidth` | Optional | `boolean` | `false` | If `true`, the dialog stretches to the full width of the screen. |
| `icon` | Optional | `ReactElement` | | Allows to override the default icon. |
| `id` | Optional | `string | number` | | The record id. If not provided, it will be deduced from the record context. |
| `inline` | Optional | `boolean` | | Set to true to display only a Material UI `<IconButton>` instead of the full `<Button>`. |
| `label` | Optional | `string` | | Allows to override the default button label. I18N is supported. |
| `maxWidth` | Optional | `string` | `sm` | The max width of the dialog. |
| `mutation Options` | Optional | `object` | | The options to pass to the `useMutation` hook. |
| `queryOptions` | Optional | `object` | | The options to pass to the `useQuery` hook.
| `resource` | Optional | `string` | | The resource name, e.g. `posts`
| `sx` | Optional | `object` | | Override the styles applied to the dialog component. |

## `children`

`<EditInDialogButton>` doesn't render any field by default - it delegates this to its children, usually a Form component.

React-admin provides several built-in form layout components:

- [`SimpleForm`](./SimpleForm.md) for a single-column layout
- [`TabbedForm`](./TabbedForm.md) for a tabbed layout
- [`AccordionForm`](./AccordionForm.md) for long forms with collapsible sections
- [`LongForm`](./LongForm.md) for long forms with a navigation sidebar
- [`WizardForm`](./WizardForm.md) for multi-step forms
- [`EditDialog`](./EditDialog.md) for sub-forms in a modal dialog
- and [`Form`](./Form.md), a headless component to use as a base for your custom layouts

To use an alternative form layout, switch the `<EditInDialogButton>` child component:

```diff
const EditButton = () => (
<EditInDialogButton fullWidth maxWidth="md">
- <SimpleForm>
+ <TabbedForm>
+ <TabbedForm.Tab label="Identity">
<TextInput source="first_name" fullWidth />
<TextInput source="last_name" fullWidth />
+ </TabbedForm.Tab>
+ <TabbedForm.Tab label="Informations">
<DateInput source="dob" label="born" fullWidth />
<SelectInput source="sex" choices={sexChoices} fullWidth />
+ </TabbedForm.Tab>
- </SimpleForm>
+ </TabbedForm>
</EditInDialogButton>
);
```

## `ButtonProps`

The `ButtonProps` prop allows you to pass props to the MUI `<Button>` component. For instance, to change the color and size of the button:

{% raw %}
```jsx
const EditButton = () => (
<EditInDialogButton ButtonProps={{ color: 'primary', fullWidth: true }}>
<SimpleForm>
...
</SimpleForm>
</EditInDialogButton>
);
```
{% endraw %}

## `emptyWhileLoading`

By default, `<EditInDialogButton>` renders its child component even before the `dataProvider.getOne()` call returns. If you use `<SimpleForm>` or `<TabbedForm>`, this isn't a problem as these components only render when the record has been fetched. But if you use a custom child component that expects the record context to be defined, your component will throw an error.

To avoid this, set the `emptyWhileLoading` prop to `true`:

```jsx
const EditButton = () => (
<EditInDialogButton emptyWhileLoading>
...
</EditInDialogButton>
);
```

## `fullWidth`

By default, `<EditInDialogButton>` renders a [Material UI `<Dialog>`](https://mui.com/material-ui/react-dialog/#full-screen-dialogs) component that takes the width of its content.

You can make the dialog full width by setting the `fullWidth` prop to `true`:

```jsx
const EditButton = () => (
<EditInDialogButton fullWidth>
...
</EditInDialogButton>
);
```

In addition, you can set a dialog maximum width by using the `maxWidth` enumerable in combination with the `fullWidth` boolean. When the `fullWidth` prop is true, the dialog will adapt based on the `maxWidth` value.

```jsx
const EditButton = () => (
<EditInDialogButton fullWidth maxWidth="sm">
...
</EditInDialogButton>
);
```

## `icon`

The `icon` prop allows you to pass an icon to the button. It can be a MUI icon component, or a custom icon component.

```jsx
import { Edit } from '@mui/icons-material';

const EditButton = () => (
<EditInDialogButton icon={<Edit />}>
...
</EditInDialogButton>
);
```

## `id`

The `id` prop allows you to pass the record id to the `<EditInDialogButton>` component. If not provided, it will be deduced from the record context.

This is useful to link to a related record. For instance, the following button lets you show the author of a book:

```jsx
const EditAuthorButton = () => {
const book = useRecordContext();
return (
<EditInDialogButton resource="authors" id={book.author_id}>
...
</EditInDialogButton>
);
};
```

## `inline`

By default, `<EditInDialogButton>` renders a `<Button>` component. If you want to display only an `<IconButton>`, set the `inline` prop to `true`:

```jsx
const EditButton = () => (
<EditInDialogButton inline>
...
</EditInDialogButton>
);
```

## `label`

The `label` prop allows you to pass a custom label to the button, instead of the default ("Edit"). It can be a string, or a React element.

```jsx
const EditButton = () => (
<EditInDialogButton label="Edit details">
...
</EditInDialogButton>
);
```

## `maxWidth`

The `maxWidth` prop allows you to set the max width of the dialog. It can be one of the following values: `xs`, `sm`, `md`, `lg`, `xl`, `false`. The default is `sm`.

For example, you can use that prop to make the dialog full width:

```jsx
const EditButton = () => (
<EditInDialogButton fullWidth maxWidth={false}>
...
</EditInDialogButton>
);
```

## `mutationOptions`

The `queryOptions` prop allows you to pass options to the `useMutation` hook.

This can be useful e.g. to pass [a custom `meta`](./Actions.md#meta-parameter) to the `dataProvider.update()` call.

{% raw %}
```jsx
const EditButton = () => (
<EditInDialogButton mutationOptions={{ meta: { fetch: 'author' } }}>
...
</EditInDialogButton>
);
```
{% endraw %}

## `queryOptions`

The `queryOptions` prop allows you to pass options to the `useQuery` hook.

This can be useful e.g. to pass [a custom `meta`](./Actions.md#meta-parameter) to the `dataProvider.getOne()` call.

{% raw %}
```jsx
const EditButton = () => (
<EditInDialogButton queryOptions={{ meta: { fetch: 'author' } }}>
...
</EditInDialogButton>
);
```
{% endraw %}

## `resource`

* `inline`: set to true to display only a Material UI `<IconButton>` instead of the full `<Button>`. The label will still be available as a `<Tooltip>` though.
* `icon`: allows to override the default icon.
* `label`: allows to override the default button label. I18N is supported.
* `ButtonProps`: object containing props to pass to Material UI's `<Button>`.
* remaining props will be passed to the [`<EditDialog>`](./EditDialog.md) dialog component.
The `resource` prop allows you to pass the resource name to the `<EditInDialogButton>` component. If not provided, it will be deduced from the resource context.

Check out [the `ra-form-layout` documentation](https://marmelab.com/ra-enterprise/modules/ra-form-layout#createindialogbutton-editindialogbutton-and-showindialogbutton) for more details.
This is useful to link to a related record. For instance, the following button lets you show the author of a book:

```jsx
const EditAuthorButton = () => {
const book = useRecordContext();
return (
<EditInDialogButton resource="authors" id={book.author_id}>
...
</EditInDialogButton>
);
};
```

## `sx`

Customize the styles applied to the Material UI `<Dialog>` component:

{% raw %}
```jsx
const EditButton = () => (
<EditInDialogButton sx={{ backgroundColor: 'paper' }}>
...
</EditInDialogButton>
);
```
{% endraw %}

## Redirection After Deletion

If you use `<SimpleForm>` as child of `<EditInDialogButton>`, the default form toolbar includes a `<DeleteButton>`. And upon deletion, this button redirects to the current resource list. This is probably not what you want, so it's common to customize the form toolbar to disable the redirection after deletion:

{% raw %}
```tsx
// src/CustomToolbar.tsx
import { Toolbar, SaveButton, DeleteButton } from 'react-admin';

export const CustomToolbar = () => (
<Toolbar sx={{ justifyContent: 'space-between' }}>
<SaveButton />
<DeleteButton redirect={false} />
</Toolbar>
);

// src/EmployerEdit.tsx
import { Edit, SimpleForm, TextInput, ReferenceManyField } from 'react-admin';
import { EditInDialogButton } from '@react-admin/ra-form-layout';
import { CustomToolbar } from './CustomToolbar';

const EmployerEdit = () => (
<Edit>
<SimpleForm>
...
<ReferenceManyField target="employer_id" reference="customers">
<Datagrid>
...
<EditInDialogButton fullWidth maxWidth="sm">
<SimpleForm toolbar={<CustomToolbar />}>
<TextInput source="first_name" />
<TextInput source="last_name" />
</SimpleForm>
</EditInDialogButton>
</Datagrid>
</ReferenceManyField>
</SimpleForm>
</Edit>
);
```
{% endraw %}

## Combining With `<CreateInDialogButton>`

Expand Down