Skip to main content
Version: 4.xx.xx

Theme

Theme specifies the color of the components, the darkness of the surfaces, level of shadow, appropriate opacity of ink elements, etc. You can either create your own Theme or use Themes that provide from refine. Theme provides a way to your app's design to meet them.

Refer to the Material UI documentation for more information about Material UI Theming. &#8594

Predefined Themes

RefineThemes has predefined themes for you. You can use them by importing them from @refinedev/mui package. It is not required if you decide to use the default MUI theme.

// light themes
const { Blue, Purple, Magenta, Red, Orange, Yellow } = RefineThemes;

// dark themes
const { BlueDark, PurpleDark, MagentaDark, RedDark, OrangeDark, YellowDark } =
RefineThemes;
import { Refine } from "@refinedev/core";
import { ThemedLayout, RefineThemes } from "@refinedev/mui";

import { ThemeProvider } from "@mui/material/styles";

const App: React.FC = () => {
return (
<ThemeProvider theme={RefineThemes.Blue}>
<Refine
/* ... */
>
<ThemedLayout>{/* ... */}</ThemedLayout>
</Refine>
</ThemeProvider>
);
};

Theme Provider

The ThemeProvider component is a simple wrapper around React's Context API that allows you to inject a Theme object into your application. By default, Material-UI components come with a default Theme. In addition, you can also use the ThemeProvider component to inject a custom theme that you have created yourself. This is a feature that allows for great flexibility in how you design your application.

import { Refine } from "@refinedev/core";
import { ThemedLayout, ErrorComponent } from "@refinedev/mui";
import { CssBaseline, GlobalStyles, ThemeProvider } from "@mui/material";

import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import { PostsList, PostCreate, PostEdit } from "pages/posts";

const App: React.FC = () => {
return (
<ThemeProvider theme={YOUR_THEME_OBJECT}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<BrowserRouter>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
resources={[
{
name: "posts",
list: "/posts",
create: "/posts/create",
edit: "/posts/edit/:id",
},
]}
>
<Routes>
<Route
element={
<ThemedLayout>
<Outlet />
</ThemedLayout>
}
>
<Route path="posts">
<Route index element={<PostList />} />
<Route path="create" element={<PostCreate />} />
<Route path="edit/:id" element={<PostEdit />} />
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</ThemeProvider>
);
};

export default App;
tip

We recommend using create refine-app to initialize your Refine projects. It configures the project according to your needs including SSR and Theme with Next.js.

Passing the Theme to ThemeProvider

In order to use the theme in your app, you just have one choice: pass it on!

If you don't wrap your app with ThemeProvider and theme, it looks like this when using the Material UI default:

http://localhost:3000
import { Refine } from "@refinedev/core";
import { ThemedLayout, ErrorComponent } from "@refinedev/mui";
import { CssBaseline, GlobalStyles, ThemeProvider } from "@mui/material";
import dataProvider from "@refinedev/simple-rest";
import routerProvider, { NavigateToResource } from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import { SampleList, SampleCreate, SampleEdit } from "pages/samples";

const App: React.FC = () => {
return (
<>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<BrowserRouter>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
resources={[
{
name: "samples",
list: "/samples",
create: "/samples/create",
edit: "/samples/edit/:id",
},
]}
>
<Routes>
<Route index element={<NavigateToResource />} />
<Route
element={
<ThemedLayout>
<Outlet />
</ThemedLayout>
}
>
<Route path="samples">
<Route index element={<SampleList />} />
<Route
path="create"
element={<SampleCreate />}
/>
<Route
path="edit/:id"
element={<SampleEdit />}
/>
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</>
);
};

render(<App />);

In our example, we will be using RefineThemes.

The design will change to match the RefineThemes.Blue, so you can enjoy these amazing interfaces without any hassle!

http://localhost:3000
import { Refine } from "@refinedev/core";
import {
ThemedLayout,
ErrorComponent,
RefineThemes,
} from "@refinedev/mui";
import { CssBaseline, GlobalStyles, ThemeProvider } from "@mui/material";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import { SampleList, SampleCreate, SampleEdit } from "pages/samples";

const App: React.FC = () => {
return (
<ThemeProvider theme={RefineThemes.Blue}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<BrowserRouter>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
resources={[
{
name: "samples",
list: "/samples",
create: "/samples/create",
edit: "/samples/edit/:id",
show: "/samples/show/:id",
},
]}
>
<Routes>
<Route
element={
<ThemedLayout>
<Outlet />
</ThemedLayout>
}
>
<Route path="samples">
<Route index element={<SampleList />} />
<Route
path="create"
element={<SampleCreate />}
/>
<Route
path="edit/:id"
element={<SampleEdit />}
/>
<Route
path="show/:id"
element={<SampleShow />}
/>
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</ThemeProvider>
);
};

Overriding Variables

The best way to customize your theme is by changing the configuration variables. These sections cover some of those most important options, like palette and typography!

src/App.tsx
import { RefineThemes } from "@refinedev/mui";

const overridedLightTheme = {
...RefineThemes.Blue,
palette: {
...RefineThemes.Blue.palette,
primary: {
main: "#44d0c7",
},
secondary: {
main: "#2f82f1",
},
},
};
tip

Get a designer's opinion anyway - you'll be happy with the end result!

When we easy-override our RefineThemes, it's going to look like this:

http://localhost:3000
import { Refine } from "@refinedev/core";
import {
ThemedLayout,
ErrorComponent,
RefineThemes,
} from "@refinedev/mui";
import { CssBaseline, GlobalStyles, ThemeProvider } from "@mui/material";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import { SampleList } from "./pages/samples";

const overridedLightTheme = {
...RefineThemes.Blue,
palette: {
...RefineThemes.Blue.palette,
primary: {
main: "#44d0c7",
},
secondary: {
main: "#2f82f1",
},
},
};

const App: React.FC = () => {
return (
<ThemeProvider theme={overridedLightTheme}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<BrowserRouter>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
resources={[
{
name: "samples",
list: "/samples",
},
]}
>
<Routes>
<Route
element={
<ThemedLayout>
<Outlet />
</ThemedLayout>
}
>
<Route path="samples">
<Route index element={<SampleList />} />
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</ThemeProvider>
);
};

You can also change the Default Font Family.

src/App.tsx
import { RefineThemes } from "@refinedev/mui";
import { TypographyVariantsOptions } from "@mui/material";

const typography: TypographyVariantsOptions = {
fontFamily: [
"Montserrat",
"-apple-system",
"BlinkMacSystemFont",
'"Segoe UI"',
"Roboto",
'"Helvetica Neue"',
"Arial",
"sans-serif",
'"Apple Color Emoji"',
'"Segoe UI Emoji"',
'"Segoe UI Symbol"',
].join(","),
};

const overridedLightTheme = {
...RefineThemes.Blue,
...typography,
};

Refer to the Material UI documentation for more information about Material UI Theme Configuration Variables. &#8594

info

If you are overriding the fontFamily in typography, you can add the <link> tags in your index.html like the following:

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<link
href="https://fonts.googleapis.com/css2?family=Montserrat:wght@100;200;300;400;500;600;700;800;900&display=swap"
rel="stylesheet"
/>
<title>refine adding font family example</title>
</head>

<body>
...
</body>
</html>

Create Custom Theme

With the help of Refine's themes, you can customize your site in a matter of minutes. Alternatively, there is also an option to create a custom theme with the createTheme() method so you can create a custom theme with the configuration variables and use it in the whole application.

tip

You can use the responsiveFontSizes() helper to make Typography font sizes in your theme automated. For more information, you can review responsiveFontSizes() in the mui document.

http://localhost:3000
import { Refine } from "@refinedev/core";
import { ThemedLayout, ErrorComponent } from "@refinedev/mui";
import { CssBaseline, GlobalStyles, ThemeProvider } from "@mui/material";
import { createTheme, responsiveFontSizes } from "@mui/material";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import { SampleList } from "./pages/samples";

let customTheme = createTheme({
palette: {
primary: {
main: "#330f49",
},
secondary: {
main: "#45d0c8",
},
},
});

customTheme = responsiveFontSizes(customTheme);

const App: React.FC = () => {
return (
<ThemeProvider theme={customTheme}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<BrowserRouter>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
resources={[
{
name: "samples",
list: "/samples",
},
]}
>
<Routes>
<Route
element={
<ThemedLayout>
<Outlet />
</ThemedLayout>
}
>
<Route path="samples">
<Route index element={<SampleList />} />
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</ThemeProvider>
);
};

Create Theme with Custom Variables

Creating a theme with default variables is easy and we can see it in the example above. You can also create your theme with custom variables, you can check it out our example

src/App.tsx
const customTheme = createTheme({
customVariable: {
custom: "#330f49",
},
});

You need to use module augmentation for the theme to accept your custom values.

src/interfaces/theme.d.ts
import "@refinedev/mui";

export interface CustomTheme {
customVariable: {
custom: string;
};
}

declare module "@mui/material/styles" {
interface Theme extends import("@mui/material/styles").Theme, CustomTheme {}
interface ThemeOptions
extends import("@mui/material/styles").ThemeOptions,
CustomTheme {}
}

You can see an example of how to create your own theme with custom variables and its interface by accessing the links.

Dark Mode

You might prefer to use dark mode in your applications. If you want to add dark mode to your application, you can easily use useMediaQuery to set your color mode or dynamic toggle to switch your mode by using a context. This will help you maintain a consistent look and feel throughout your app.

System Preference

With the useMediaQuery hook, you can query a user's preference for light or dark mode and then adjust your site accordingly. This will make things easier on those who prefer darker colors as it simplifies their experience by eliminating any confusion about what browser they are using! For example:

src/App.tsx
import { Refine } from "@refinedev/core";
import { ThemedLayout, ErrorComponent, RefineThemes } from "@refinedev/mui";
import { CssBaseline, GlobalStyles, ThemeProvider } from "@mui/material";
import { useMediaQuery } from "@mui/material";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import {
SampleList,
SampleCreate,
SampleEdit,
SampleShow,
} from "pages/samples";

const App: React.FC = () => {
const prefersDarkMode = useMediaQuery("(prefers-color-scheme: dark)");

return (
<ThemeProvider
theme={prefersDarkMode ? RefineThemes.BlueDark : RefineThemes.Blue}
>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<BrowserRouter>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
resources={[
{
name: "samples",
list: "/samples",
create: "/samples/create",
edit: "/samples/edit/:id",
},
]}
>
<Routes>
<Route
element={
<ThemedLayout>
<Outlet />
</ThemedLayout>
}
>
<Route path="samples">
<Route index element={<SampleList />} />
<Route
path="create"
element={<SampleCreate />}
/>
<Route
path="edit/:id"
element={<SampleEdit />}
/>
<Route
path="show/:id"
element={<SampleShow />}
/>
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</ThemeProvider>
);
};

export default App;

Dark Mode Toggle

Control the Dark Mode with just one click! We prepared an example that shows how you can manage to toggle Dark Mode with help of a context in your Header component, which is given as a prop to Refine.

Dark Mode Toggle Code Example

ColorModeContext

src/contexts/index.tsx
import React, {
PropsWithChildren,
createContext,
useEffect,
useState,
} from "react";
import { ThemeProvider } from "@mui/material";
import { RefineThemes } from "@refinedev/mui";

type ColorModeContextType = {
mode: string;
setMode: () => void;
};

export const ColorModeContext = createContext<ColorModeContextType>(
{} as ColorModeContextType,
);

export const ColorModeContextProvider: React.FC<PropsWithChildren> = ({
children,
}) => {
const colorModeFromLocalStorage = localStorage.getItem("colorMode");
const isSystemPreferenceDark = window?.matchMedia(
"(prefers-color-scheme: dark)",
).matches;

const systemPreference = isSystemPreferenceDark ? "dark" : "light";
const [mode, setMode] = useState(
colorModeFromLocalStorage || systemPreference,
);

useEffect(() => {
window.localStorage.setItem("colorMode", mode);
}, [mode]);

const setColorMode = () => {
if (mode === "light") {
setMode("dark");
} else {
setMode("light");
}
};

return (
<ColorModeContext.Provider
value={{
setMode: setColorMode,
mode,
}}
>
<ThemeProvider
theme={
mode === "light" ? RefineThemes.Blue : RefineThemes.BlueDark
}
>
{children}
</ThemeProvider>
</ColorModeContext.Provider>
);
};

App.tsx

src/App.tsx
import { Refine } from "@refinedev/core";
import {
ThemedLayout,
ErrorComponent,
RefineSnackbarProvider,
notificationProvider,
} from "@refinedev/mui";
import { CssBaseline, AppBar, IconButton, Box, Stack } from "@mui/material";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import { LightModeOutlined, DarkModeOutlined } from "@mui/icons-material";

import {
SampleList,
SampleCreate,
SampleEdit,
SampleShow,
} from "pages/samples";

import { ColorModeContextProvider, ColorModeContext } from "./contexts";

const Header = () => {
const { mode, setMode } = useContext(ColorModeContext);
return (
<AppBar color="default" position="sticky">
<Stack width="100%" direction="row" justifyContent="end">
<Box marginRight="20px">
<IconButton
onClick={() => {
setMode();
}}
>
{mode === "dark" ? (
<LightModeOutlined />
) : (
<DarkModeOutlined />
)}
</IconButton>
</Box>
</Stack>
</AppBar>
);
};

const App: React.FC = () => {
return (
<ColorModeContextProvider>
<CssBaseline />
<RefineSnackbarProvider>
<BrowserRouter>
<Refine
notificationProvider={notificationProvider}
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
resources={[
{
name: "samples",
list: "/samples",
create: "/samples/create",
edit: "/samples/edit/:id",
},
]}
>
<Routes>
<Route
element={
<ThemedLayout Header={Header}>
<Outlet />
</ThemedLayout>
}
>
<Route path="samples">
<Route index element={<SampleList />} />
<Route
path="create"
element={<SampleCreate />}
/>
<Route
path="edit/:id"
element={<SampleEdit />}
/>
<Route
path="show/:id"
element={<SampleShow />}
/>
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</RefineSnackbarProvider>
</ColorModeContextProvider>
);
};

export default App;

You can use this CodeSandbox link to access this example. &#8594

http://localhost:3000
import { Refine } from "@refinedev/core";
import {
ThemedLayout,
ErrorComponent,
RefineSnackbarProvider,
notificationProvider,
} from "@refinedev/mui";
import { CssBaseline, AppBar, IconButton, Box, Stack } from "@mui/material";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import { LightModeOutlined, DarkModeOutlined } from "@mui/icons-material";

import {
SampleList,
SampleCreate,
SampleEdit,
SampleShow,
} from "pages/samples";

import { ColorModeContextProvider, ColorModeContext } from "./contexts";

const Header = () => {
const { mode, setMode } = useContext(ColorModeContext);
return (
<AppBar position="sticky">
<Stack width="100%" direction="row" justifyContent="end">
<Box
marginRight="20px"
sx={{ display: "flex", alignItems: "center" }}
>
<div style={{ fontSize: "12px" }}>
click to toggle the theme →
</div>
<IconButton
onClick={() => {
setMode();
}}
>
{mode === "dark" ? (
<LightModeOutlined />
) : (
<DarkModeOutlined />
)}
</IconButton>
</Box>
</Stack>
</AppBar>
);
};

const App: React.FC = () => {
return (
<ColorModeContextProvider>
<CssBaseline />
<RefineSnackbarProvider>
<BrowserRouter>
<Refine
notificationProvider={notificationProvider}
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
resources={[
{
name: "samples",
list: "/samples",
create: "/samples/create",
edit: "/samples/edit/:id",
},
]}
>
<Routes>
<Route
element={
<ThemedLayout Header={Header}>
<Outlet />
</ThemedLayout>
}
>
<Route path="samples">
<Route index element={<SampleList />} />
<Route
path="create"
element={<SampleCreate />}
/>
<Route
path="edit/:id"
element={<SampleEdit />}
/>
<Route
path="show/:id"
element={<SampleShow />}
/>
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</RefineSnackbarProvider>
</ColorModeContextProvider>
);
};

Notification Snackbars compatible with Theme

We use the notistack library for notifications in our Material UI package provides an elegant way to engage with your users. The main motivation for us to use the Notistack was that while the Notistack provider ( <SnackbarProvider> ) is a child of our ThemeProvider, it works in harmony with the theme.

We provide <RefineSnackbarProvider> that extended <SnackbarProvider> with theme style. You have to wrap Refine with <RefineSnackbarProvider> and also pass the notificationProvider as props.

http://localhost:3000
import { Refine } from "@refinedev/core";
import {
ThemedLayout,
ErrorComponent,
RefineThemes,
RefineSnackbarProvider,
notificationProvider,
} from "@refinedev/mui";
import { ThemeProvider, CssBaseline, GlobalStyles } from "@mui/material";
import dataProvider from "@refinedev/simple-rest";
import routerProvider from "@refinedev/react-router-v6";

import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import {
SampleList,
SampleCreate,
SampleEdit,
SampleShow,
} from "pages/samples";

const App: React.FC = () => {
return (
<ThemeProvider theme={RefineThemes.Blue}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<RefineSnackbarProvider>
<BrowserRouter>
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
notificationProvider={notificationProvider}
resources={[
{
name: "samples",
list: "/samples",
create: "/samples/create",
edit: "/samples/edit/:id",
},
]}
>
<Routes>
<Route
element={
<ThemedLayout>
<Outlet />
</ThemedLayout>
}
>
<Route path="samples">
<Route index element={<SampleList />} />
<Route
path="create"
element={<SampleCreate />}
/>
<Route
path="edit/:id"
element={<SampleEdit />}
/>
</Route>
<Route path="*" element={<ErrorComponent />} />
</Route>
</Routes>
</Refine>
</BrowserRouter>
</RefineSnackbarProvider>
</ThemeProvider>
);
};
tip

If you want to use notistack snackbars with the default style, simply wrap Refine in <SnackbarProvider>.

tip

If you want to customize the default layout elements provided with @refinedev/mui package, check out the Custom ThemedLayout tutorial.

http://localhost:3000
import React, {
PropsWithChildren,
createContext,
useEffect,
useState,
} from "react";
import {
Create,
useDataGrid,
EditButton,
ShowButton,
DeleteButton,
List,
MarkdownField,
Show,
DateField,
NumberField,
TextFieldComponent,
Edit,
useAutocomplete,
} from "@refinedev/mui";
import { DataGrid, GridColumns } from "@mui/x-data-grid";
import { Typography, TextField, Autocomplete } from "@mui/material";
import { useForm } from "@refinedev/react-hook-form";
import { Controller } from "react-hook-form";

import { useMany, useShow, useOne } from "@refinedev/core";

type ColorModeContextType = {
mode: string;
setMode: () => void;
};

const ColorModeContext = createContext<ColorModeContextType>(
{} as ColorModeContextType,
);

const ColorModeContextProvider: React.FC<PropsWithChildren> = ({
children,
}) => {
const colorModeFromLocalStorage = localStorage.getItem("colorMode");
const isSystemPreferenceDark = window?.matchMedia(
"(prefers-color-scheme: dark)",
).matches;

const systemPreference = isSystemPreferenceDark ? "dark" : "light";
const [mode, setMode] = useState(
colorModeFromLocalStorage || systemPreference,
);

useEffect(() => {
window.localStorage.setItem("colorMode", mode);
}, [mode]);

const setColorMode = () => {
if (mode === "light") {
setMode("dark");
} else {
setMode("light");
}
};

return (
<ColorModeContext.Provider
value={{
setMode: setColorMode,
mode,
}}
>
<MuiMaterial.ThemeProvider
theme={
mode === "light"
? RefineMui.RefineThemes.Blue
: RefineMui.RefineThemes.BlueDark
}
>
{children}
</MuiMaterial.ThemeProvider>
</ColorModeContext.Provider>
);
};

const SampleList = () => {
const { dataGridProps } = useDataGrid();

const { data: categoryData, isLoading: categoryIsLoading } = 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 <DateField value={value} />;
},
},
{
field: "actions",
headerName: "Actions",
renderCell: function render({ row }) {
return (
<>
<EditButton hideText recordItemId={row.id} />
<ShowButton hideText recordItemId={row.id} />
</>
);
},
align: "center",
headerAlign: "center",
minWidth: 80,
},
],
[categoryData?.data],
);

return (
<List>
<DataGrid {...dataGridProps} columns={columns} autoHeight />
</List>
);
};

const SampleShow = () => {
const { queryResult } = useShow();
const { data, isLoading } = queryResult;

const record = data?.data;

const { data: categoryData, isLoading: categoryIsLoading } = useOne({
resource: "categories",
id: record?.category?.id || "",
queryOptions: {
enabled: !!record,
},
});

return (
<Show isLoading={isLoading}>
<MuiMaterial.Stack gap={1}>
<Typography variant="body1" fontWeight="bold">
Id
</Typography>
<NumberField value={record?.id ?? ""} />
<Typography variant="body1" fontWeight="bold">
Title
</Typography>
<TextFieldComponent value={record?.title} />
<Typography variant="body1" fontWeight="bold">
Content
</Typography>
<MarkdownField value={record?.content} />
<Typography variant="body1" fontWeight="bold">
Category
</Typography>
{categoryIsLoading ? (
<>Loading...</>
) : (
<>{categoryData?.data?.title}</>
)}
<Typography variant="body1" fontWeight="bold">
Created At
</Typography>
<DateField value={record?.createdAt} />
</MuiMaterial.Stack>
</Show>
);
};

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}>
<MuiMaterial.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"
/>
<TextField
{...register("content", {
required: "This field is required",
})}
error={!!(errors as any)?.content}
helperText={(errors as any)?.content?.message}
margin="normal"
fullWidth
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
/>
)}
/>
)}
/>
</MuiMaterial.Box>
</Edit>
);
};

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

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

return (
<Create isLoading={formLoading} saveButtonProps={saveButtonProps}>
<MuiMaterial.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
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
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
/>
)}
/>
)}
/>
</MuiMaterial.Box>
</Create>
);
};