Skip to main content
Version: 4.xx.xx
Swizzle Ready

Create

<Create> provides us a layout to display the page. It does not contain any logic and just adds extra functionalities like action buttons and giving titles to the page.

We will show what <Create> does using properties with examples.

localhost:3000/posts/create
import React from "react";
import { Create, useAutocomplete } from "@refinedev/mui";
import { Autocomplete, Box, TextField } from "@mui/material";
import { useForm } from "@refinedev/react-hook-form";
import { Controller } from "react-hook-form";
import { Breadcrumb } from "@refinedev/mui";
import { useState } from "react";

const SampleCreate = () => {
const {
saveButtonProps,
refineCore: { formLoading },
register,
control,
formState: { errors },
} = useForm();

const { autocompleteProps: categoryAutocompleteProps } = useAutocomplete({
resource: "categories",
});

return (
<Create isLoading={formLoading} saveButtonProps={saveButtonProps}>
<Box
component="form"
sx={{ display: "flex", flexDirection: "column" }}
autoComplete="off"
>
<TextField
{...register("title", {
required: "This field is required",
})}
error={!!(errors as any)?.title}
helperText={(errors as any)?.title?.message}
margin="normal"
fullWidth
slotProps={{
InputLabelProps: { shrink: true },
}}
type="text"
label="Title"
name="title"
/>
<TextField
{...register("content", {
required: "This field is required",
})}
error={!!(errors as any)?.content}
helperText={(errors as any)?.content?.message}
margin="normal"
fullWidth
slotProps={{
InputLabelProps: { shrink: true },
}}
multiline
label="Content"
name="content"
/>
<Controller
control={control}
name="category"
rules={{ required: "This field is required" }}
// eslint-disable-next-line
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>
<Breadcrumb />
</Create>
);
};
Good to know:

You can swizzle this component with the Refine CLI to customize it.

Properties

title

You can customize the title of the <Create/> component by using the title property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
title="Custom Title"
>
<span>Rest of your page here</span>
</Create>
);
};

resource

resource property determines which resource to use for the form. By default, it uses the resource from the route. If you want to use a different resource, you can use the resource property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
resource="categories"
>
<span>Rest of your page here</span>
</Create>
);
};

saveButtonProps

You can customize the save button by using the saveButtonProps property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
saveButtonProps={{
size: "large",
variant: "contained",
color: "secondary",
}}
>
<span>Rest of your page here</span>
</Create>
);
};

For more information, refer to the <SaveButton> documentation

goBack

goBack property determines whether to display a back button in the header. If you want to hide the back button, you can set goBack to false.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
goBack={false}
>
<span>Rest of your page here</span>
</Create>
);
};

isLoading

To show a loading state, you can use the isLoading property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";
import { useState } from "react";

const PostCreate: React.FC = () => {
const [loading, setLoading] = useState(true);

return (
<Create
isLoading={loading}
>
<span>Rest of your page here</span>
</Create>
);
};

To customize or disable the breadcrumb, you can use the breadcrumb property. By default, it uses the Breadcrumb component from @refinedev/mui package.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";
import { Breadcrumb } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
breadcrumb={
<div
style={{
padding: "3px 6px",
border: "2px dashed #888",
}}
>
<Breadcrumb />
</div>
}
>
<span>Rest of your page here</span>
</Create>
);
};

For more information, refer to the Breadcrumb documentation

wrapperProps

If you want to customize the wrapper of the <Create/> component, you can use the wrapperProps property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
wrapperProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
>
<span>Rest of your page here</span>
</Create>
);
};

For more information, refer to the Card documentation from Material UI

headerProps

If you want to customize the header of the <Create/> component, you can use the headerProps property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
headerProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
>
<span>Rest of your page here</span>
</Create>
);
};

For more information, refer to the CardHeader documentation from Material UI

contentProps

If you want to customize the content of the <Create/> component, you can use the contentProps property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
contentProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
>
<span>Rest of your page here</span>
</Create>
);
};

For more information, refer to the CardContent documentation from Material UI

headerButtons

You can customize the buttons at the header by using the headerButtons property. It accepts false or a function that returns ReactNode.

By default, the <Create/> component has a list button in the header.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";
import { Button } from "@mui/material";

const PostCreate: React.FC = () => {
return (
<Create
headerButtons={({ defaultButtons }) => (
<>
{defaultButtons}
<Button
variant="contained"
onClick={() => {
console.log("Custom button clicked");
}}
>
Custom Button
</Button>
</>
)}
>
<span>Rest of your page here</span>
</Create>
);
};

headerButtonProps

You can customize the list button at the header by using the headerButtonProps property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
headerButtonProps={{
listButtonProps: {
size: "large",
variant: "contained",
color: "secondary",
},
}}
>
<span>Rest of your page here</span>
</Create>
);
};

footerButtons

You can customize the buttons at the footer by using the footerButtons property. It accepts false or a function that returns ReactNode.

By default, the <Create/> component has a save button at the footer.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";
import { Button } from "@mui/material";

const PostCreate: React.FC = () => {
return (
<Create
footerButtons={({ defaultButtons }) => (
<>
{defaultButtons}
<Button
variant="contained"
onClick={() => {
console.log("Custom button clicked");
}}
>
Custom Button
</Button>
</>
)}
>
<span>Rest of your page here</span>
</Create>
);
};

footerButtonProps

You can customize the save button at the footer by using the footerButtonProps property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
footerButtonProps={{
saveButtonProps: {
size: "large",
variant: "contained",
color: "secondary",
},
}}
>
<span>Rest of your page here</span>
</Create>
);
};

footerProps

If you want to customize the footer of the <Create/> component, you can use the footerProps property.

localhost:3000/posts/create
import { Create } from "@refinedev/mui";

const PostCreate: React.FC = () => {
return (
<Create
footerProps={{
sx: {
backgroundColor: "lightsteelblue",
},
}}
>
<span>Rest of your page here</span>
</Create>
);
};

API Reference

Properties