Skip to main content
Version: 3.xx.xx

Your First App using Mantine

Introduction​

This tutorial will go through process of building a simple admin panel for a CMS-like application with headless.

Step by step, you're going to learn how to consume a REST API and add basic CRUD functionality to your panel leveraging the unique capabilities of refine.

Let's begin by setting up a new refine project.

Setting up​

There are two alternative methods to set up a refine application.

The recommended way is using the create refine-app tool. create refine-app's CLI wizard will let you create and customize your application in seconds.

Alternatively, you may use the create-react-app tool to create an empty React application and then add refine module via npm.

First, run the create refine-app with the following command:

npm create refine-app -- -o refine-mantine tutorial

About Fake REST API​

refine is designed to consume data from APIs.

For the sake of this tutorial, we will provide you a fully working, fake REST API located at https://api.fake-rest.refine.dev/. You may take a look at available resources and routes of the API before proceeding to the next step.

Using a Dataprovider​

Dataproviders are refine components making it possible to consume different API's and data services conveniently. To consume our Fake REST API, we'll use the "Simple REST Dataprovider".

Next, navigate to the project folder and run the following command to install the required package:

npm i @pankod/refine-simple-rest
note

If you used create refine-app to bootstrap the project, you can skip issuing this command as create refine-app already installs the selected data provider.

note

Fake REST API is based on JSON Server Project. Simple REST Dataprovider is fully compatible with the REST rules and methods of the JSON Server.

Bootstrapping the Application​

Replace the contents of App.tsx with the following code:

src/App.tsx
import { Refine } from "@pankod/refine-core";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import {
MantineProvider,
Global,
NotificationsProvider,
notificationProvider,
LightTheme,
Layout,
ReadyPage,
ErrorComponent,
} from "@pankod/refine-mantine";

const App = () => {
return (
<MantineProvider theme={LightTheme} withNormalizeCSS withGlobalStyles>
<Global styles={{ body: { WebkitFontSmoothing: "auto" } }} />
<NotificationsProvider position="top-right">
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
notificationProvider={notificationProvider}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
Layout={Layout}
/>
</NotificationsProvider>
</MantineProvider>
);
};
info

<GlobalStyles> is a component that is used to apply the global styles to the application. You may refer to the GlobalStyles documentation for more information.

info

Refine application uses Montserrat font by default as it is defined in the typography property of the theme. But to use Montserrat, you need to embed it to your index.html file. For more information about adding font family in your Refine application, you can look at Mantine Theme Customization.

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>
tip

refine comes native with Light/Dark theme support. Check out the theme documentation for more information.


<Refine/> is the root component of a refine application. Using the dataProvider prop, we made our Simple REST Dataprovider available to the entire application.

Run the following command to launch the app in development mode:

npm run dev

Your refine application should be up and running!
Point your browser to http://localhost:3000 to access it. You will see the welcome page.

http://localhost:3000

Adding Resources​

Now we are ready to start connecting to our API by adding a resource to our application.

Let's add /posts/ endpoint from our API as a resource. First take a look to the raw API response for the request made to the /posts/ route:

Show response

GET https://api.fake-rest.refine.dev/posts/
[
{
"id": 1,
"title": "Eius ea autem sapiente placeat fuga voluptas quos quae.",
"slug": "beatae-esse-dolor",
"content": "Explicabo nihil delectus. Nam aliquid sunt numquam...",
"category": {
"id": 24
},
"user": {
"id": 7
},
"status": "draft",
"createdAt": "2021-03-13T03:09:30.186Z",
"image": [],
"tags": [
7,
4
],
"language": 2
},
...
]


Now, add the highlighted code to your App.tsx to connect to the endpoint.

src/App.tsx
import { Refine } from "@pankod/refine-core";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import {
MantineProvider,
Global,
NotificationsProvider,
notificationProvider,
LightTheme,
Layout,
ReadyPage,
ErrorComponent,
} from "@pankod/refine-mantine";

const App = () => {
return (
<MantineProvider theme={LightTheme} withNormalizeCSS withGlobalStyles>
<Global styles={{ body: { WebkitFontSmoothing: "auto" } }} />
<NotificationsProvider position="top-right">
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
notificationProvider={notificationProvider}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
Layout={Layout}
resources={[{ name: "posts" }]}
/>
</NotificationsProvider>
</MantineProvider>
);
};
info

resources is a property of <Refine/> representing API Endpoints. The name property of every single resource should match one of the endpoints in your API!

Instead of showing the welcome page, the application should redirect now to an URL defined by the name property. Open your application to check that the URL is routed to /posts:

http://localhost:3000/posts

You'll still see a 404 error page because no Page component is assigned to our resource yet.

note

resources use Page components to handle data and perform rendering. Page components are passed to resources as an array of objects. For basic CRUD operations, there are four predefined props: list, create, edit and show.

Let's create a Page component to fetch posts and display them as a table. Later, we will pass the component as the list prop to our resource.

Creating a List Page​

First, we'll need an interface to work with the data from the API endpoint.

tip

We'll use the @pankod/refine-react-table for benefit of the TanStack Table v8 library. However, you can use useTable without the @pankod/refine-react-table package.

Create a new folder named "interfaces" under "/src" if you don't already have one. Then create a "index.d.ts" file with the following code:

interfaces/index.d.ts
export interface IPost {
id: number;
title: string;
status: "published" | "draft" | "rejected";
createdAt: string;
}

We'll be using title, status and createdAt fields of every post record.

Now, create a new folder named "pages/posts" under "/src". Under that folder, create a "list.tsx" file with the following code:

src/pages/posts/list.tsx
import { useTable, ColumnDef, flexRender } from "@pankod/refine-react-table";
import {
Box,
Group,
List,
ScrollArea,
Table,
Pagination,
DateField,
} from "@pankod/refine-mantine";

import { IPost } from "../../interfaces";

export const PostList: React.FC = () => {
const columns = React.useMemo<ColumnDef<IPost>[]>(
() => [
{
id: "id",
header: "ID",
accessorKey: "id",
},
{
id: "title",
header: "Title",
accessorKey: "title",
},
{
id: "status",
header: "Status",
accessorKey: "status",
},
{
id: "createdAt",
header: "Created At",
accessorKey: "createdAt",
cell: function render({ getValue }) {
return (
<DateField value={getValue() as string} format="LLL" />
);
},
},
],
[],
);

const {
getHeaderGroups,
getRowModel,
refineCore: { setCurrent, pageCount, current },
} = useTable({
columns,
});

return (
<ScrollArea>
<List>
<Table highlightOnHover>
<thead>
{getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<th key={header.id}>
{!header.isPlaceholder && (
<Group spacing="xs" noWrap>
<Box>
{flexRender(
header.column
.columnDef
.header,
header.getContext(),
)}
</Box>
</Group>
)}
</th>
);
})}
</tr>
))}
</thead>
<tbody>
{getRowModel().rows.map((row) => {
return (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => {
return (
<td key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</td>
);
})}
</tr>
);
})}
</tbody>
</Table>
<br />
<Pagination
position="right"
total={pageCount}
page={current}
onChange={setCurrent}
/>
</List>
</ScrollArea>
);
};

@pankod/refine-react-table hook uses useTable() fetches data from API. Normally, TanStack-table's useReactTable expects a data prop but @pankod/refine-react-table's useTable doesn't expect a data prop.

Refer to the @pankod/refine-react-table for more information. β†’

note

We didn't use arrow functions for rendering cell because of the react/display-name is not compatible with arrow functions. If you want to use arrow functions, you can use like this:

pages/posts/list.tsx
// eslint-disable-next-line
renderCell: ({ getValue }) => (
<DateField value={getValue() as string} format="LLL" />
);

Refer to <DateField /> for more information. β†’


Finally, we are ready to add <PostList> to our resource. Add the highlighted line to your App.tsx:

src/App.tsx
import { Refine } from "@pankod/refine-core";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import {
MantineProvider,
Global,
NotificationsProvider,
notificationProvider,
LightTheme,
Layout,
ReadyPage,
ErrorComponent,
} from "@pankod/refine-mantine";

import { PostList } from "./pages/posts";

const App = () => {
return (
<MantineProvider theme={LightTheme} withNormalizeCSS withGlobalStyles>
<Global styles={{ body: { WebkitFontSmoothing: "auto" } }} />
<NotificationsProvider position="top-right">
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
notificationProvider={notificationProvider}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
Layout={Layout}
resources={[{ name: "posts", list: PostList }]}
/>
</NotificationsProvider>
</MantineProvider>
);
};

Note you will need a few more files which help src/App.tsx to find your pages and posts. In the /pages folder, put this index.tsx file in it which allows everything in the posts folder to be used elsewhere.

src/pages/index.tsx
export * from "./posts";

Similarly, put a file in the /src/pages/posts folder which accomplishes the same function. We will use the commented out code later as we add more capabilities to our app. Remember as you add functions, uncomment each appropriate line.

src/pages/posts/index.tsx
export * from "./list";
// export * from "./edit";
// export * from "./create";
// export * from "./show";

Open your application in your browser. You will see posts are displayed correctly in a table structure and even the pagination works out-of-the box.

On the next step, we are going to add a category field to the table which involves handling data relationships.

http://localhost:3000/posts

Handling relationships​

Remember the records from /posts endpoint that had a category id field.

https://api.fake-rest.refine.dev/posts/1
...
"category": {
"id": 26
}
...

To display category titles on our table, we need to category id to their corresponding titles. The category title data can be obtained from the /categories endpoint for each record.

https://api.fake-rest.refine.dev/categories/26
  {
"id": 26,
"title": "mock category title",
}

At this point, we need to join records from different resources. For this, we're going to use the refine hook useMany.

Before we start, just edit our interface for the new ICategory type:

interfaces/index.d.ts
export interface ICategory {
id: number;
title: string;
}

export interface IPost {
id: number;
title: string;
status: "published" | "draft" | "rejected";
category: { id: number };
createdAt: string;
}

So we can update our list.tsx with the highlighted lines:

src/pages/posts/list.tsx
import { useTable, ColumnDef, flexRender } from "@pankod/refine-react-table";
import {
Box,
Group,
List,
ScrollArea,
Select,
Table,
Pagination,
DateField,
} from "@pankod/refine-mantine";
import { GetManyResponse, useMany } from "@pankod/refine-core";

import { IPost, ICategory } from "../../interfaces";

export const PostList: React.FC = () => {
const columns = React.useMemo<ColumnDef<IPost>[]>(
() => [
{
id: "id",
header: "ID",
accessorKey: "id",
},
{
id: "title",
header: "Title",
accessorKey: "title",
},
{
id: "status",
header: "Status",
accessorKey: "status",
},
{
id: "category.id",
header: "Category",
enableColumnFilter: false,
accessorKey: "category.id",
cell: function render({ getValue, table }) {
const meta = table.options.meta as {
categoriesData: GetManyResponse<ICategory>;
};
const category = meta.categoriesData?.data.find(
(item) => item.id === getValue(),
);
return category?.title ?? "Loading...";
},
},
{
id: "createdAt",
header: "Created At",
accessorKey: "createdAt",
cell: function render({ getValue }) {
return (
<DateField value={getValue() as string} format="LLL" />
);
},
},
],
[],
);

const {
getHeaderGroups,
getRowModel,
setOptions,
refineCore: {
setCurrent,
pageCount,
current,
tableQueryResult: { data: tableData },
},
} = useTable({
columns,
});

const categoryIds = tableData?.data?.map((item) => item.category.id) ?? [];
const { data: categoriesData } = useMany<ICategory>({
resource: "categories",
ids: categoryIds,
queryOptions: {
enabled: categoryIds.length > 0,
},
});

setOptions((prev) => ({
...prev,
meta: {
...prev.meta,
categoriesData,
},
}));

return (
<ScrollArea>
<List>
<Table highlightOnHover>
<thead>
{getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<th key={header.id}>
{!header.isPlaceholder && (
<Group spacing="xs" noWrap>
<Box>
{flexRender(
header.column
.columnDef
.header,
header.getContext(),
)}
</Box>
</Group>
)}
</th>
);
})}
</tr>
))}
</thead>
<tbody>
{getRowModel().rows.map((row) => {
return (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => {
return (
<td key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</td>
);
})}
</tr>
);
})}
</tbody>
</Table>
<br />
<Pagination
position="right"
total={pageCount}
page={current}
onChange={setCurrent}
/>
</List>
</ScrollArea>
);
};

We construct an array of categoryId's from /posts endpoint and pass it to the useMany hook. categoriesData will be filled with id-title tuples to be used for rendering our component.

Try the result on your browser and you'll notice that the category column is filled correctly with the matching category titles for the each record's category id's. Even the loading state is managed by refine.

To get more detailed information about this hook, please refer the useMany Documentation.

http://localhost:3000/posts

Adding Sorting and Filtering​

The @pankod/refine-react-table package also listens for changes in filters and sorting states of the Tanstack Table and updates the table accordingly. The change in these states triggers the fetch of the new data.

So, we can add filters and sorting features to our table as suggested by TanStack Table with the following code:

src/pages/posts/list.tsx
import { useTable, ColumnDef, flexRender } from "@pankod/refine-react-table";
import {
Box,
Group,
List,
ScrollArea,
Select,
Table,
Pagination,
DateField,
} from "@pankod/refine-mantine";
import { GetManyResponse, useMany } from "@pankod/refine-core";

import { ColumnFilter, ColumnSorter } from "../../components/table";
import { IPost, ICategory } from "../../interfaces";

export interface FilterElementProps {
value: any;
onChange: (value: any) => void;
}

export const PostList: React.FC = () => {
const columns = React.useMemo<ColumnDef<IPost>[]>(
() => [
{
id: "id",
header: "ID",
accessorKey: "id",
},
{
id: "title",
header: "Title",
accessorKey: "title",
meta: {
filterOperator: "contains",
},
},
{
id: "status",
header: "Status",
accessorKey: "status",
meta: {
filterElement: function render(props: FilterElementProps) {
return (
<Select
defaultValue="published"
data={[
{ label: "Published", value: "published" },
{ label: "Draft", value: "draft" },
{ label: "Rejected", value: "rejected" },
]}
{...props}
/>
);
},
filterOperator: "eq",
},
},
{
id: "category.id",
header: "Category",
enableColumnFilter: false,
accessorKey: "category.id",
cell: function render({ getValue, table }) {
const meta = table.options.meta as {
categoriesData: GetManyResponse<ICategory>;
};
const category = meta.categoriesData?.data.find(
(item) => item.id === getValue(),
);
return category?.title ?? "Loading...";
},
},
{
id: "createdAt",
header: "Created At",
accessorKey: "createdAt",
enableColumnFilter: false,
cell: function render({ getValue }) {
return (
<DateField value={getValue() as string} format="LLL" />
);
},
},
],
[],
);

const {
getHeaderGroups,
getRowModel,
setOptions,
refineCore: {
setCurrent,
pageCount,
current,
tableQueryResult: { data: tableData },
},
} = useTable({
columns,
});

const categoryIds = tableData?.data?.map((item) => item.category.id) ?? [];
const { data: categoriesData } = useMany<ICategory>({
resource: "categories",
ids: categoryIds,
queryOptions: {
enabled: categoryIds.length > 0,
},
});

setOptions((prev) => ({
...prev,
meta: {
...prev.meta,
categoriesData,
},
}));

return (
<ScrollArea>
<List>
<Table highlightOnHover>
<thead>
{getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<th key={header.id}>
{!header.isPlaceholder && (
<Group spacing="xs" noWrap>
<Box>
{flexRender(
header.column
.columnDef
.header,
header.getContext(),
)}
</Box>
<Group spacing="xs" noWrap>
<ColumnSorter
column={
header.column
}
/>
<ColumnFilter
column={
header.column
}
/>
</Group>
</Group>
)}
</th>
);
})}
</tr>
))}
</thead>
<tbody>
{getRowModel().rows.map((row) => {
return (
<tr key={row.id}>
{row.getVisibleCells().map((cell) => {
return (
<td key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</td>
);
})}
</tr>
);
})}
</tbody>
</Table>
<br />
<Pagination
position="right"
total={pageCount}
page={current}
onChange={setCurrent}
/>
</List>
</ScrollArea>
);
};
Show ColumnFilter
src/components/table/columnFilter.tsx
import React, { useState } from "react";
import {
TextInput,
Menu,
ActionIcon,
Stack,
Group,
} from "@pankod/refine-mantine";
import { IconFilter, IconX, IconCheck } from "@tabler/icons";

export const ColumnFilter: React.FC<{ column: Column<any, any> }> = ({
column,
}) => {
const [state, setState] = useState(null as null | { value: any });

if (!column.getCanFilter()) {
return null;
}

const open = () =>
setState({
value: column.getFilterValue(),
});

const close = () => setState(null);

const change = (value: any) => setState({ value });

const clear = () => {
column.setFilterValue(undefined);
close();
};

const save = () => {
if (!state) return;
column.setFilterValue(state.value);
close();
};

const renderFilterElement = () => {
const FilterComponent = (column.columnDef?.meta as any)?.filterElement;

if (!FilterComponent && !!state) {
return (
<TextInput
autoComplete="off"
value={state.value}
onChange={(e) => change(e.target.value)}
/>
);
}

return <FilterComponent value={state?.value} onChange={change} />;
};

return (
<Menu
opened={!!state}
position="bottom"
withArrow
transition="scale-y"
shadow="xl"
onClose={close}
width="256px"
withinPortal
>
<Menu.Target>
<ActionIcon
size="xs"
onClick={open}
variant={column.getIsFiltered() ? "light" : "transparent"}
color={column.getIsFiltered() ? "primary" : "gray"}
>
<IconFilter size={18} />
</ActionIcon>
</Menu.Target>
<Menu.Dropdown>
{!!state && (
<Stack p="xs" spacing="xs">
{renderFilterElement()}
<Group position="right" spacing={6} noWrap>
<ActionIcon
size="md"
color="gray"
variant="outline"
onClick={clear}
>
<IconX size={18} />
</ActionIcon>
<ActionIcon
size="md"
onClick={save}
color="primary"
variant="outline"
>
<IconCheck size={18} />
</ActionIcon>
</Group>
</Stack>
)}
</Menu.Dropdown>
</Menu>
);
};
Show ColumnSorter
src/components/table/columnSorter.tsx
import { ActionIcon } from "@pankod/refine-mantine";
import { IconChevronDown, IconSelector } from "@tabler/icons";

export const ColumnSorter: React.FC<{ column: Column<any, any> }> = ({
column,
}) => {
if (!column.getCanSort()) {
return null;
}

const sorted = column.getIsSorted();

return (
<ActionIcon
size="xs"
onClick={column.getToggleSortingHandler()}
style={{
transition: "transform 0.25s",
transform: `rotate(${sorted === "asc" ? "180" : "0"}deg)`,
}}
variant={sorted ? "light" : "transparent"}
color={sorted ? "primary" : "gray"}
>
{sorted ? (
<IconChevronDown size={18} />
) : (
<IconSelector size={18} />
)}
</ActionIcon>
);
};
http://localhost:3000/posts

Showing a single record​

At this point we are able to list all post records on the table component with pagination, sorting and filtering functionality. Next, we are going to add a details page to fetch and display data from a single record.

Let's create a <PostShow> component on /pages/posts folder:

/pages/posts/show.tsx
import { useShow, useOne } from "@pankod/refine-core";
import { Show, Title, Text } from "@pankod/refine-mantine";

import { ICategory, IPost } from "../../interfaces";

const PostShow: React.FC = () => {
const { queryResult } = useShow<IPost>();
const { data, isLoading } = queryResult;
const record = data?.data;

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

return (
<Show isLoading={isLoading}>
<Title order={5}>Id</Title>
<Text mt="xs">{record?.id}</Text>

<Title mt="xs" order={5}>
Title
</Title>
<Text mt="xs">{record?.title}</Text>

<Title mt="xs" order={5}>
Status
</Title>
<Text mt="xs">{record?.status}</Text>

<Title mt="xs" order={5}>
Category
</Title>
<Text mt="xs">{categoryData?.data?.title}</Text>
</Show>
);
};

✳️ useShow() is a refine hook used to fetch a single record of data. The queryResult has the response and also isLoading state.

Refer to the useShow documentation for detailed usage information. β†’

✳️ To retrieve the category title, again we need to make a call to /categories endpoint. This time we used useOne() hook to get a single record from another resource.

Refer to the useOne documentation for detailed usage information. β†’

attention

useShow() is the preferred hook for fetching data from the current resource. To query foreign resources you may use the low-level useOne() hook.

Since we've got access to raw data returning from useShow(), there is no restriction on how it's displayed on your components. If you prefer presenting your content with a nicer wrapper, refine provides you the <Show> component which has extra features like list and refresh buttons.

Refer to the <Show> documentation for detailed usage information. β†’


Now we can add the newly created component to our resource with show prop:

src/App.tsx
import { Refine } from "@pankod/refine-core";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import {
MantineProvider,
Global,
NotificationsProvider,
notificationProvider,
LightTheme,
Layout,
ReadyPage,
ErrorComponent,
} from "@pankod/refine-mantine";

import { PostList, PostShow } from "./pages/posts";

const App = () => {
return (
<MantineProvider theme={LightTheme} withNormalizeCSS withGlobalStyles>
<Global styles={{ body: { WebkitFontSmoothing: "auto" } }} />
<NotificationsProvider position="top-right">
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
notificationProvider={notificationProvider}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
Layout={Layout}
resources={[
{ name: "posts", list: PostList, show: PostShow },
]}
/>
</NotificationsProvider>
</MantineProvider>
);
};

And then we can add a <ShowButton> on the list page to make it possible for users to navigate to detail pages:

src/pages/posts/list.tsx
import {
...
ShowButton,
} from "@pankod/refine-mantine";

const PostList: React.FC = () => {
const columns = React.useMemo<ColumnDef<IPost>[]>(
() => [
...
{
id: "actions",
header: "Actions",
accessorKey: "id",
enableColumnFilter: false,
enableSorting: false,
cell: function render({ getValue }) {
return (
<ShowButton
hideText
recordItemId={getValue() as number}
/>
);
},
},
],
[],
);


const { ... } = useTable<IPost>({ columns });

return (
...
);
};
http://localhost:3000/posts

Editing a record​

Until this point, we were basically working with reading operations such as fetching and displaying data from resources. From now on, we are going to start creating and updating records by using useForm.

Let's start by creating a new <PostEdit> page responsible for editing a single record:

src/pages/posts/edit.tsx
import {
Edit,
Select,
TextInput,
useForm,
useSelect,
} from "@pankod/refine-mantine";

import { ICategory } from "../../interfaces";

export const PostEdit: React.FC = () => {
const {
saveButtonProps,
getInputProps,
refineCore: { queryResult },
} = useForm({
initialValues: {
title: "",
status: "",
category: {
id: "",
},
},
validate: {
title: (value) => (value.length < 2 ? "Too short title" : null),
status: (value) =>
value.length <= 0 ? "Status is required" : null,
category: {
id: (value) =>
value.length <= 0 ? "Category is required" : null,
},
},
});

const { selectProps } = useSelect<ICategory>({
resource: "categories",
defaultValue: queryResult?.data?.data.category.id,
});

return (
<Edit saveButtonProps={saveButtonProps}>
<form>
<TextInput
mt={8}
label="Title"
placeholder="Title"
{...getInputProps("title")}
/>
<Select
mt={8}
label="Status"
placeholder="Pick one"
{...getInputProps("status")}
data={[
{ label: "Published", value: "published" },
{ label: "Draft", value: "draft" },
{ label: "Rejected", value: "rejected" },
]}
/>
<Select
mt={8}
label="Category"
placeholder="Pick one"
{...getInputProps("category.id")}
{...selectProps}
/>
</form>
</Edit>
);
};

Let's see what's going on our <PostEdit> component in detail:

✳️ useForm is a refine hook for handling form data. In edit page, useForm hook initializes the form with current record values.

Attention

✳️ <TextInput> is a Mantine component to build form inputs.

✳️ <Select> is a text input that helps you find what you're looking for by suggesting options. useSelect is a refine hook for handling <Select> data. It returns selectProps which includes all necessary props to build the autocomplete. You may refer to the useSelect to get the full information about the hook.

✳️ <Edit> is a wrapper refine component for <form>. It provides save, delete and refresh buttons that can be used for form actions.

✳️ Form data is set automatically with getInputProps coming out of theuseForm hook.

✳️ Save button submits the form by executing the useUpdate method provided by the dataProvider. After a successful response, the application will be redirected to the listing page.

Now we can add the newly created component to our resource with edit prop:

src/App.tsx
import { Refine } from "@pankod/refine-core";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import {
MantineProvider,
Global,
NotificationsProvider,
notificationProvider,
LightTheme,
Layout,
ReadyPage,
ErrorComponent,
} from "@pankod/refine-mantine";

import { PostList, PostShow, PostEdit } from "./pages/posts";

const App = () => {
return (
<MantineProvider theme={LightTheme} withNormalizeCSS withGlobalStyles>
<Global styles={{ body: { WebkitFontSmoothing: "auto" } }} />
<NotificationsProvider position="top-right">
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
notificationProvider={notificationProvider}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
Layout={Layout}
resources={[
{
name: "posts",
list: PostList,
show: PostShow,
edit: PostEdit,
},
]}
/>
</NotificationsProvider>
</MantineProvider>
);
};

We are going to need an edit button on each row to display the <PostEdit> component. refine doesn't automatically add one, so we have to update our <PostList> component to add a <EditButton> for each record:

src/pages/posts/list.tsx
import {
...
ShowButton,
EditButton
} from "@pankod/refine-mantine";

const PostList: React.FC = () => {
const columns = React.useMemo<ColumnDef<IPost>[]>(
() => [
...
{
id: "actions",
header: "Actions",
accessorKey: "id",
enableColumnFilter: false,
enableSorting: false,
cell: function render({ getValue }) {
return (
<Group spacing="xs" noWrap>
<ShowButton
hideText
recordItemId={getValue() as number}
/>
<EditButton
hideText
recordItemId={getValue() as number}
/>
</Group>
);
},
},
],
[],
);


const { ... } = useTable<IPost>({ columns });

return (
...
);
};

Refer to the <EditButton> documentation for detailed usage information. β†’

You can try using edit buttons which will trigger the edit forms for each record, allowing you to update the record data.

http://localhost:3000/posts

Creating a record​

Creating a record in refine follows a similar flow as editing records.

First, we'll create a <PostCreate> page:

src/pages/posts/create.tsx
import {
Create,
Select,
TextInput,
useForm,
useSelect,
} from "@pankod/refine-mantine";

export const PostCreate: React.FC = () => {
const { saveButtonProps, getInputProps, errors } = useForm({
initialValues: {
title: "",
status: "",
category: {
id: "",
},
},
validate: {
title: (value) => (value.length < 2 ? "Too short title" : null),
status: (value) =>
value.length <= 0 ? "Status is required" : null,
category: {
id: (value) =>
value.length <= 0 ? "Category is required" : null,
},
},
});

const { selectProps } = useSelect({
resource: "categories",
});

return (
<Create saveButtonProps={saveButtonProps}>
<form>
<TextInput
mt={8}
label="Title"
placeholder="Title"
{...getInputProps("title")}
/>
<Select
mt={8}
label="Status"
placeholder="Pick one"
{...getInputProps("status")}
data={[
{ label: "Published", value: "published" },
{ label: "Draft", value: "draft" },
{ label: "Rejected", value: "rejected" },
]}
/>
<Select
mt={8}
label="Category"
placeholder="Pick one"
{...getInputProps("category.id")}
{...selectProps}
/>
</form>
</Create>
);
};

We should notice some minor differences from the edit example:

✳️ <form> is wrapped with <Create> component.

✳️ Save button submits the form by executing the useCreate method provided by the dataProvider.

✳️ No defaultValue is passed to useSelect.


After creating the <PostCreate> component, add it to resource with create prop:


src/App.tsx
import { Refine } from "@pankod/refine-core";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import {
MantineProvider,
Global,
NotificationsProvider,
notificationProvider,
LightTheme,
Layout,
ReadyPage,
ErrorComponent,
} from "@pankod/refine-mantine";

import { PostList, PostShow, PostEdit, PostCreate } from "./pages/posts";

const App = () => {
return (
<MantineProvider theme={LightTheme} withNormalizeCSS withGlobalStyles>
<Global styles={{ body: { WebkitFontSmoothing: "auto" } }} />
<NotificationsProvider position="top-right">
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
notificationProvider={notificationProvider}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
Layout={Layout}
resources={[
{
name: "posts",
list: PostList,
show: PostShow,
edit: PostEdit,
create: PostCreate,
},
]}
/>
</NotificationsProvider>
</MantineProvider>
);
};

And that's it! Try it on the browser and see if you can create new posts from scratch.

http://localhost:3000/posts/create

Deleting a record​

Deleting a record can be done in two ways.

The first way is adding a delete button on each row since refine doesn't automatically add one, so we have to update our <PostList> component to add a <DeleteButton> for each record:

src/pages/posts/list.tsx
import {
...
ShowButton,
EditButton,
DeleteButton
} from "@pankod/refine-mantine";

const PostList: React.FC = () => {
const columns = React.useMemo<ColumnDef<IPost>[]>(
() => [
...
{
id: "actions",
header: "Actions",
accessorKey: "id",
enableColumnFilter: false,
enableSorting: false,
cell: function render({ getValue }) {
return (
<Group spacing="xs" noWrap>
<ShowButton
hideText
recordItemId={getValue() as number}
/>
<EditButton
hideText
recordItemId={getValue() as number}
/>
<DeleteButton
hideText
recordItemId={getValue() as number}
/>
</Group>
);
},
},
],
[],
);


const { ... } = useTable<IPost>({ columns });

return (
...
);
};

Refer to the <DeleteButton> documentation for detailed usage information. β†’

Now you can try deleting records yourself. Just click on the delete button of the record you want to delete and confirm.

The second way is showing delete button in <PostEdit> component. To show delete button in edit page, canDelete prop needs to be passed to resource object.

src/App.tsx
import { Refine } from "@pankod/refine-core";
import routerProvider from "@pankod/refine-react-router-v6";
import dataProvider from "@pankod/refine-simple-rest";
import {
MantineProvider,
Global,
NotificationsProvider,
notificationProvider,
LightTheme,
Layout,
ReadyPage,
ErrorComponent,
} from "@pankod/refine-mantine";

import { PostList, PostShow, PostEdit, PostCreate } from "./pages/posts";

const App = () => {
return (
<MantineProvider theme={LightTheme} withNormalizeCSS withGlobalStyles>
<Global styles={{ body: { WebkitFontSmoothing: "auto" } }} />
<NotificationsProvider position="top-right">
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(
"https://api.fake-rest.refine.dev",
)}
notificationProvider={notificationProvider}
ReadyPage={ReadyPage}
catchAll={<ErrorComponent />}
Layout={Layout}
resources={[
{
name: "posts",
list: PostList,
show: PostShow,
edit: PostEdit,
create: PostCreate,
canDelete: true,
},
]}
/>
</NotificationsProvider>
</MantineProvider>
);
};

After adding canDelete prop, <DeleteButton> will appear in edit form.

http://localhost:3000/posts

Live StackBlitz Example​

Our tutorial is complete. Below you'll find a Live StackBlitz Example displaying what we have done so far:

Next Steps​