like-gold
like-gold5mo ago

useSelect Mui

example of useSelect with material ui
2 Replies
kapa.ai
kapa.ai5mo ago
Hi @d4rk51d3. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To use the useSelect hook with Material UI in a refine application, you can follow the example provided in the documentation. Below is a detailed example of how to integrate useSelect with Material UI's DataGrid component to handle relational data. Example: Using useSelect with Material UI This example demonstrates how to fetch relational data using the useSelect hook and display it in a Material UI DataGrid.
import React from "react";
import { Option, useSelect } from "@refinedev/core";
import { useDataGrid, List } from "@refinedev/mui";
import {
DataGrid,
GridColDef,
GridValueFormatterParams,
} from "@mui/x-data-grid";

import { ICategory, IPost } from "interfaces";

const PostsList: React.FC = () => {
const { dataGridProps } = useDataGrid<IPost>({
pagination: {
current: 2,
pageSize: 10,
},
sorters: {
initial: [
{
field: "title",
order: "asc",
},
],
},
filters: {
initial: [
{
field: "status",
operator: "eq",
value: "draft",
},
],
},
syncWithLocation: true,
});

const {
options,
queryResult: { isLoading },
} = useSelect<ICategory>({
resource: "categories",
hasPagination: false,
});

const columns = React.useMemo<GridColDef<IPost>[]>(
() => [
{
field: "id",
headerName: "ID",
type: "number",
width: 50,
},
{ field: "title", headerName: "Title", minWidth: 400, flex: 1 },
{
field: "category.id",
headerName: "Category",
type: "singleSelect",
headerAlign: "left",
align: "left",
minWidth: 250,
flex: 0.5,
valueOptions: options,
valueFormatter: (params: GridValueFormatterParams<Option>) => {
return params.value;
},
renderCell: function render({ row }) {
if (isLoading) {
return "Loading...";
}

const category = options.find(option => option.value === row.category.id);
return category ? category.label : "Unknown";
},
},
],
[options, isLoading]
);

return (
<List>
<DataGrid {...dataGridProps} columns={columns} autoHeight />
</List>
);
};
export default PostsList;
import React from "react";
import { Option, useSelect } from "@refinedev/core";
import { useDataGrid, List } from "@refinedev/mui";
import {
DataGrid,
GridColDef,
GridValueFormatterParams,
} from "@mui/x-data-grid";

import { ICategory, IPost } from "interfaces";

const PostsList: React.FC = () => {
const { dataGridProps } = useDataGrid<IPost>({
pagination: {
current: 2,
pageSize: 10,
},
sorters: {
initial: [
{
field: "title",
order: "asc",
},
],
},
filters: {
initial: [
{
field: "status",
operator: "eq",
value: "draft",
},
],
},
syncWithLocation: true,
});

const {
options,
queryResult: { isLoading },
} = useSelect<ICategory>({
resource: "categories",
hasPagination: false,
});

const columns = React.useMemo<GridColDef<IPost>[]>(
() => [
{
field: "id",
headerName: "ID",
type: "number",
width: 50,
},
{ field: "title", headerName: "Title", minWidth: 400, flex: 1 },
{
field: "category.id",
headerName: "Category",
type: "singleSelect",
headerAlign: "left",
align: "left",
minWidth: 250,
flex: 0.5,
valueOptions: options,
valueFormatter: (params: GridValueFormatterParams<Option>) => {
return params.value;
},
renderCell: function render({ row }) {
if (isLoading) {
return "Loading...";
}

const category = options.find(option => option.value === row.category.id);
return category ? category.label : "Unknown";
},
},
],
[options, isLoading]
);

return (
<List>
<DataGrid {...dataGridProps} columns={columns} autoHeight />
</List>
);
};
export default PostsList;
Explanation 1. Fetching Data: The useSelect hook is used to fetch data from the categories resource. 2. DataGrid Configuration: The useDataGrid hook is used to configure the DataGrid component with pagination, sorting, and filtering. 3. Columns Definition: The columns for the DataGrid are defined, including a column for the category which uses the options fetched by useSelect. 4. Rendering: The DataGrid is rendered within a List component, displaying the ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@475681545793961986> kapa.ai is still learning and improving, please let us know how it did by reacting below

Did you find this page helpful?