<Edit>
provides us a layout for displaying the page. It does not contain any logic but adds extra functionalities like a <RefreshButton>
.
We will show what <Edit>
does using properties with examples.
import React from "react";
import { Edit, useAutocomplete } from "@refinedev/mui";
import { TextField, Autocomplete, Box } from "@mui/material";
import { useForm } from "@refinedev/react-hook-form";
import { Controller } from "react-hook-form";
const SampleEdit = () => {
const {
saveButtonProps,
refineCore: { queryResult },
register,
control,
formState: { errors },
} = useForm();
const samplesData = queryResult?.data?.data;
const { autocompleteProps: categoryAutocompleteProps } = useAutocomplete({
resource: "categories",
defaultValue: samplesData?.category?.id,
});
return (
<Edit saveButtonProps={saveButtonProps}>
<Box
component="form"
sx={{ display: "flex", flexDirection: "column" }}
autoComplete="off"
>
<TextField
{...register("id", {
required: "This field is required",
})}
error={!!(errors as any)?.id}
helperText={(errors as any)?.id?.message}
margin="normal"
fullWidth
InputLabelProps={{ shrink: true }}
type="number"
label="Id"
name="id"
disabled
/>
<TextField
{...register("title", {
required: "This field is required",
})}
error={!!(errors as any)?.title}
helperText={(errors as any)?.title?.message}
margin="normal"
fullWidth
InputLabelProps={{ shrink: true }}
type="text"
label="Title"
name="title"
/>
<Controller
control={control}
name="category"
rules={{ required: "This field is required" }}
defaultValue={null as any}
render={({ field }) => (
<Autocomplete
{...categoryAutocompleteProps}
{...field}
onChange={(_, value) => {
field.onChange(value);
}}
getOptionLabel={(item) => {
return (
categoryAutocompleteProps?.options?.find(
(p) =>
p?.id?.toString() ===
item?.id?.toString(),
)?.title ?? ""
);
}}
isOptionEqualToValue={(option, value) =>
value === undefined || option?.id?.toString() === (value?.id ?? value)?.toString()
}
renderInput={(params) => (
<TextField
{...params}
label="Category"
margin="normal"
variant="outlined"
error={!!(errors as any)?.category?.id}
helperText={
(errors as any)?.category?.id?.message
}
required
/>
)}
/>
)}
/>
</Box>
</Edit>
);
};
You can swizzle this component to customize it with the refine CLI
Properties
title
It allows adding title inside the <Edit>
component. if you don't pass title props it uses "Edit" prefix and singular resource name by default. For example, for the /posts/edit
resource, it will be "Edit post".
import { Edit } from "@refinedev/mui";
import { Typography } from "@mui/material";
const EditPage: React.FC = () => {
return (
<Edit
title={<Typography variant="h5">Custom Title</Typography>}
>
<span>Rest of your page here</span>
</Edit>
);
};
resource
The <Edit>
component reads the resource
information from the route by default. If you want to use a custom resource for the <Edit>
component, you can use the resource
prop.
import { Edit } from "@refinedev/mui";
const CustomPage: React.FC = () => {
return (
<Edit resource="posts" recordItemId={123}>
<span>Rest of your page here</span>
</Edit>
);
};
The <Edit>
component has a save button by default. If you want to customize this button you can use the saveButtonProps
property like the code below.
Clicking on the save button will submit your form.
Refer to the <SaveButton>
documentation for detailed usage. →
import { Edit } from "@refinedev/mui";
const PostEdit: React.FC = () => {
return (
<Edit saveButtonProps={{ size: "small" }}>
<span>Rest of your page here</span>
</Edit>
);
};
canDelete
allows us to add the delete button inside the <Edit>
component. If the resource has the canDelete
property, refine adds the delete button by default. If you want to customize this button you can use the deleteButtonProps
property like the code below.
When clicked on, the delete button executes the useDelete
method provided by the dataProvider
.
Refer to the <DeleteButton>
documentation for detailed usage. →
import { Edit } from "@refinedev/mui";
import { usePermissions } from "@refinedev/core";
const PostEdit: React.FC = () => {
const { data: permissionsData } = usePermissions();
return (
<Edit
canDelete={permissionsData?.includes("admin")}
deleteButtonProps={{ size: "small" }}
saveButtonProps={{ size: "small" }}
>
<p>Rest of your page here</p>
</Edit>
);
};
Refer to the usePermission
documentation for detailed usage. →
recordItemId
The <Edit>
component reads the id
information from the route by default. recordItemId
is used when it cannot read from the URL(when used on a custom page, modal or drawer).
import { Edit } from "@refinedev/mui";
const CustomPage: React.FC = () => {
return (
<Edit resource="posts" recordItemId={123}>
<span>Rest of your page here</span>
</Edit>
);
};
The <Edit>
component needs the id
information for the <RefreshButton>
to work properly.
mutationMode
Determines which mode mutation will have while executing <DeleteButton>
.
Refer to the mutation mode docs for further information. →
import React from "react";
import { Edit, useAutocomplete } from "@refinedev/mui";
import { TextField, Autocomplete, Box } from "@mui/material";
import { useForm } from "@refinedev/react-hook-form";
import { Controller } from "react-hook-form";
const SampleEdit = () => {
const {
saveButtonProps,
refineCore: { queryResult },
register,
control,
formState: { errors },
} = useForm();
const samplesData = queryResult?.data?.data;
const { autocompleteProps: categoryAutocompleteProps } = useAutocomplete({
resource: "categories",
defaultValue: samplesData?.category?.id,
});
return (
<Edit
saveButtonProps={saveButtonProps}
canDelete
mutationMode="undoable"
>
<Box
component="form"
sx={{ display: "flex", flexDirection: "column" }}
autoComplete="off"
>
<TextField
{...register("id", {
required: "This field is required",
})}
error={!!(errors as any)?.id}
helperText={(errors as any)?.id?.message}
margin="normal"
fullWidth
InputLabelProps={{ shrink: true }}
type="number"
label="Id"
name="id"
disabled
/>
<TextField
{...register("title", {
required: "This field is required",
})}
error={!!(errors as any)?.title}
helperText={(errors as any)?.title?.message}
margin="normal"
fullWidth
InputLabelProps={{ shrink: true }}
type="text"
label="Title"
name="title"
/>
<Controller
control={control}
name="category"
rules={{ required: "This field is required" }}
defaultValue={null as any}
render={({ field }) => (
<Autocomplete
{...categoryAutocompleteProps}
{...field}
onChange={(_, value) => {
field.onChange(value);
}}
getOptionLabel={(item) => {
return (
categoryAutocompleteProps?.options?.find(
(p) =>
p?.id?.toString() ===
item?.id?.toString(),
)?.title ?? ""
);
}}
isOptionEqualToValue={(option, value) =>
value === undefined || option?.id?.toString() === (value?.id ?? value)?.toString()
}
renderInput={(params) => (
<TextField
{...params}
label="Category"
margin="normal"
variant="outlined"
error={!!(errors as any)?.category?.id}
helperText={
(errors as any)?.category?.id?.message
}
required
/>
)}
/>
)}
/>
</Box>
</Edit>
);
};
dataProviderName
If not specified, Refine will use the default data provider. If you have multiple data providers and want to use a different one, you can use the dataProviderName
property.
import { Refine } from "@refinedev/core";
import dataProvider from "@refinedev/simple-rest";
import { Edit } from "@refinedev/mui";
const PostEdit = () => {
return <Edit dataProviderName="other">...</Edit>;
};
export const App: React.FC = () => {
return (
<Refine
dataProvider={{
default: dataProvider("https://api.fake-rest.refine.dev/"),
other: dataProvider("https://other-api.fake-rest.refine.dev/"),
}}
>
{}
</Refine>
);
};
goBack
To customize the back button or to disable it, you can use the goBack
property.
import { Edit } from "@refinedev/mui";
import { Button } from "@mui/material";
import { useBack } from "@refinedev/core";
const BackButton = () => {
const goBack = useBack();
return <Button onClick={() => goBack()}>BACK!</Button>;
};
const PostEdit: React.FC = () => {
return (
<Edit
goBack={<BackButton />}
>
<span>Rest of your page here</span>
</Edit>
);
};
isLoading
To toggle the loading state of the <Edit/>
component, you can use the isLoading
property.
import { Edit } from "@refinedev/mui";
const PostEdit: React.FC = () => {
const [loading, setLoading] = React.useState(true);
return (
<Edit
isLoading={loading}
>
<span>Rest of your page here</span>
</Edit>
);
};
breadcrumb
To customize or disable the breadcrumb, you can use the breadcrumb
property. By default it uses the Breadcrumb
component from @refinedev/mui
package.
Refer to the Breadcrumb
documentation for detailed usage. →
This feature can be managed globally via the <Refine>
component's options
import { Edit, Breadcrumb } from "@refinedev/mui";
const PostEdit: React.FC = () => {
return (
<Edit
breadcrumb={
<div
style={{
padding: "3px 6px",
border: "2px dashed cornflowerblue",
}}
>
<Breadcrumb />
</div>
}
>
<span>Rest of your page here</span>
</Edit>
);
};
wrapperProps
If you want to customize the wrapper of the <Edit/>
component, you can use the wrapperProps
property.
Refer to the Card
documentation from Material UI for detailed usage. →
import { Edit } from "@refinedev/mui";
const PostEdit: React.FC = () => {
const [loading, setLoading] = React.useState(true);
return (
<Edit
wrapperProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
>
<span>Rest of your page here</span>
</Edit>
);
};
If you want to customize the header of the <Edit/>
component, you can use the headerProps
property.
Refer to the CardHeader
documentation from Material UI for detailed usage. →
import { Edit } from "@refinedev/mui";
const PostEdit: React.FC = () => {
const [loading, setLoading] = React.useState(true);
return (
<Edit
headerProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
>
<span>Rest of your page here</span>
</Edit>
);
};
contentProps
If you want to customize the content of the <Edit/>
component, you can use the contentProps
property.
Refer to the CardContent
documentation from Material UI for detailed usage. →
import { Edit } from "@refinedev/mui";
const PostEdit: React.FC = () => {
const [loading, setLoading] = React.useState(true);
return (
<Edit
contentProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
>
<span>Rest of your page here</span>
</Edit>
);
};
You can customize the buttons at the header by using the headerButtons
property. It accepts React.ReactNode
or a render function ({ defaultButtons }) => React.ReactNode
which you can use to keep the existing buttons and add your own.
import { Edit } from "@refinedev/mui";
import { Button } from "@mui/material";
const PostEdit: React.FC = () => {
const [loading, setLoading] = React.useState(true);
return (
<Edit
headerButtons={({ defaultButtons }) => (
<>
{defaultButtons}
<Button type="primary">Custom Button</Button>
</>
)}
>
<span>Rest of your page here</span>
</Edit>
);
};
You can customize the wrapper element of the buttons at the header by using the headerButtonProps
property.
Refer to the Box
documentation from Material UI for detailed usage. →
import { Edit } from "@refinedev/mui";
import { Button } from "@mui/material";
const PostEdit: React.FC = () => {
const [loading, setLoading] = React.useState(true);
return (
<Edit
headerButtonProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
headerButtons={({ defaultButtons }) => (
<>
{defaultButtons}
<Button type="primary">Custom Button</Button>
</>
)}
>
<span>Rest of your page here</span>
</Edit>
);
};
You can customize the buttons at the footer by using the footerButtons
property. It accepts React.ReactNode
or a render function ({ defaultButtons }) => React.ReactNode
which you can use to keep the existing buttons and add your own.
import { Edit } from "@refinedev/mui";
import { Button } from "@mui/material";
const PostEdit: React.FC = () => {
const [loading, setLoading] = React.useState(true);
return (
<Edit
footerButtons={({ defaultButtons }) => (
<>
{defaultButtons}
<Button type="primary">Custom Button</Button>
</>
)}
>
<span>Rest of your page here</span>
</Edit>
);
};
You can customize the wrapper element of the buttons at the footer by using the footerButtonProps
property.
Refer to the CardActions
documentation from Material UI for detailed usage. →
import { Edit } from "@refinedev/mui";
import { Button } from "@mui/material";
const PostEdit: React.FC = () => {
const [loading, setLoading] = React.useState(true);
return (
<Edit
footerButtonProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
footerButtons={({ defaultButtons }) => (
<>
{defaultButtons}
<Button type="primary">Custom Button</Button>
</>
)}
>
<span>Rest of your page here</span>
</Edit>
);
};
API Reference
Properties
const SampleList = () => {
const { dataGridProps } = RefineMui.useDataGrid();
const { data: categoryData, isLoading: categoryIsLoading } =
RefineCore.useMany({
resource: "categories",
ids:
dataGridProps?.rows?.map((item: any) => item?.category?.id) ??
[],
queryOptions: {
enabled: !!dataGridProps?.rows,
},
});
const columns = React.useMemo<GridColumns<any>>(
() => [
{
field: "id",
headerName: "Id",
type: "number",
minWidth: 50,
},
{
field: "title",
headerName: "Title",
minWidth: 200,
},
{
field: "category",
headerName: "Category",
valueGetter: ({ row }) => {
const value = row?.category?.id;
return value;
},
minWidth: 300,
renderCell: function render({ value }) {
return categoryIsLoading ? (
<>Loading...</>
) : (
categoryData?.data?.find((item) => item.id === value)
?.title
);
},
},
{
field: "createdAt",
headerName: "Created At",
minWidth: 250,
renderCell: function render({ value }) {
return <RefineMui.DateField value={value} />;
},
},
{
field: "actions",
headerName: "Actions",
renderCell: function render({ row }) {
return (
<>
<RefineMui.EditButton
hideText
recordItemId={row.id}
/>
</>
);
},
align: "center",
headerAlign: "center",
minWidth: 80,
},
],
[categoryData?.data],
);
return (
<RefineMui.List>
<MuiXDataGrid.DataGrid
{...dataGridProps}
columns={columns}
autoHeight
/>
</RefineMui.List>
);
};
const Wrapper = ({ children }) => {
return (
<MuiMaterial.ThemeProvider theme={RefineMui.LightTheme}>
<MuiMaterial.CssBaseline />
<MuiMaterial.GlobalStyles
styles={{ html: { WebkitFontSmoothing: "auto" } }}
/>
{children}
</MuiMaterial.ThemeProvider>
);
};