zaro

How do I customize the React-Admin Edit component?

Published in React Admin Edit Component 3 mins read

To customize the way you edit data records using the React-Admin framework, you typically work with the built-in <Edit> component. Customizing this component involves configuring its appearance, behavior, and the form fields it contains by using various props.

The <Edit> component is a fundamental building block in React-Admin applications for creating interfaces where users can modify existing data entries. By passing different properties (props) to it, you can control aspects like the actions available, the layout, and the form elements displayed.

Key Customization Props for the Edit Component

The provided reference highlights several important props you can use to tailor the <Edit> component to your specific needs. These props allow you to override default behaviors or inject custom elements.

Here's a breakdown of these key props:

Prop Description How to Use
actions Override the default actions toolbar shown at the top of the edit page. Pass a custom React component to this prop.
aside Render a component alongside the main content, typically in a sidebar. Pass a React component to render in the aside area.
children Define the form fields that render inside the <Edit> component. Place <Input> components (or other form elements) as children of <Edit>.
className Add custom CSS classes to the root element of the component. Pass a string with one or more CSS class names.
component Override the root HTML component or render prop used by <Edit>. Pass an HTML tag name (e.g., 'div') or a custom component.

Defining the Edit Form Fields

The most crucial customization is typically defining what fields appear in the edit form. This is done using the children prop. You place React-Admin <Input> components (like <TextInput>, <SelectInput>, <DateInput>, etc.) inside the <Edit> component.

import { Edit, SimpleForm, TextInput } from 'react-admin';

const PostEdit = (props) => (
    <Edit {...props}>
        <SimpleForm>
            <TextInput source="title" />
            <TextInput source="body" multiline />
            {/* Add more input fields as needed */}
        </SimpleForm>
    </Edit>
);

In this example, <SimpleForm> is used as a wrapper for the input fields, simplifying layout.

Overriding Default Actions

Sometimes you need custom buttons or controls in the header of the edit page, instead of the default "Save" or "Delete" buttons. You can achieve this by providing a custom component to the actions prop.

import { Edit, SimpleForm, TextInput, TopToolbar, ListButton, ShowButton } from 'react-admin';

const PostEditActions = ({ basePath, data, resource }) => (
    <TopToolbar>
        <ShowButton record={data} basePath={basePath} />
        <ListButton basePath={basePath} />
        {/* Add your custom action buttons here */}
    </TopToolbar>
);

const PostEdit = (props) => (
    <Edit actions={<PostEditActions />} {...props}>
        <SimpleForm>
            <TextInput source="title" />
        </SimpleForm>
    </Edit>
);

Here, PostEditActions is a custom component replacing the default actions toolbar. TopToolbar is a utility component provided by React-Admin for styling action buttons.

Adding an Aside (Sidebar)

The aside prop is useful for displaying related information or components that don't fit directly within the main edit form, such as recent activity logs, related items, or instructions.

import { Edit, SimpleForm, TextInput, Card } from 'react-admin';
import CardContent from '@material-ui/core/CardContent';
import Typography from '@material-ui/core/Typography';

const PostAside = () => (
    <Card style={{ width: 200, margin: '1em' }}>
        <CardContent>
            <Typography variant="h6">Instructions</Typography>
            <Typography variant="body2">
                Fill out all required fields carefully.
            </Typography>
        </CardContent>
    </Card>
);

const PostEdit = (props) => (
    <Edit aside={<PostAside />} {...props}>
        <SimpleForm>
            <TextInput source="title" />
        </SimpleForm>
    </Edit>
);

This adds a simple card with instructions to the side of the edit form.

Applying Custom Styles

Use the className prop to add specific CSS classes to the root element of the <Edit> component for styling purposes.

import { Edit, SimpleForm, TextInput } from 'react-admin';

const PostEdit = (props) => (
    <Edit className="post-edit-page" {...props}>
        <SimpleForm>
            <TextInput source="title" />
        </SimpleForm>
    </Edit>
);

You would then define CSS rules for the .post-edit-page class in your application's stylesheet.

Overriding the Root Component

The component prop allows you to change the fundamental HTML element used for the root of the <Edit> view or even replace it with a completely custom render prop. While less common for basic customization, it offers advanced control over the component's rendering structure.

For more detailed information and additional customization options, you can refer to the official React-Admin documentation for the Edit component.