import '@mantine/core/styles.css';
import '@mantine/dates/styles.css';
import 'mantine-react-table/styles.css';
import { useMemo, useState } from 'react';
import {
MantineReactTable,
type MRT_ColumnDef,
useMantineReactTable,
} from 'mantine-react-table';
import { data, type Person } from './makeData';
import { Group, Radio, Stack, Text } from '@mantine/core';
type ColumnMode = 'false' | 'remove' | 'reorder';
const ExampleGrouping = () => {
const columns = useMemo<MRT_ColumnDef<Person>[]>(
() => [
{
header: 'First Name',
accessorKey: 'firstName',
},
{
header: 'Last Name',
accessorKey: 'lastName',
},
{
header: 'Age',
accessorKey: 'age',
},
{
header: 'Gender',
accessorKey: 'gender',
},
{
header: 'State',
accessorKey: 'state',
},
{
header: 'Salary',
accessorKey: 'salary',
},
],
[],
);
const [groupedColumnMode, setGroupedColumnMode] =
useState<ColumnMode>('reorder');
const table = useMantineReactTable({
columns,
data,
enableGrouping: true,
groupedColumnMode:
groupedColumnMode === 'false' ? false : groupedColumnMode,
initialState: {
expanded: true,
grouping: ['state', 'gender'],
pagination: { pageIndex: 0, pageSize: 20 },
},
});
return (
<Stack gap="1rem">
<DemoRadioGroup
groupedColumnMode={groupedColumnMode}
setGroupedColumnMode={setGroupedColumnMode}
/>
<MantineReactTable table={table} />
</Stack>
);
};
export default ExampleGrouping;
const DemoRadioGroup = ({
groupedColumnMode,
setGroupedColumnMode,
}: {
groupedColumnMode: 'false' | 'remove' | 'reorder';
setGroupedColumnMode: (
groupedColumnMode: 'false' | 'remove' | 'reorder',
) => void;
}) => {
return (
<Stack m={'auto'} align={'center'}>
<Text>Grouped Column Mode</Text>
<Radio.Group
value={groupedColumnMode}
onChange={(event) => setGroupedColumnMode(event as ColumnMode)}
>
<Group>
<Radio value={'reorder'} label={'Reorder (default)'} />
<Radio value={'remove'} label={'Remove'} />
<Radio value={'false'} label={'False'} />
</Group>
</Radio.Group>
</Stack>
);
};