import '@mantine/core/styles.css';
import '@mantine/dates/styles.css';
import 'mantine-react-table/styles.css';
import {
MantineReactTable,
useMantineReactTable,
type MRT_ColumnDef,
type MRT_Row,
} from 'mantine-react-table';
import { Box, Button } from '@mantine/core';
import { IconDownload } from '@tabler/icons-react';
import { mkConfig, generateCsv, download } from 'export-to-csv';
import { data, type Person } from './makeData';
const columns: MRT_ColumnDef<Person>[] = [
{
accessorKey: 'id',
header: 'ID',
size: 40,
},
{
accessorKey: 'firstName',
header: 'First Name',
size: 120,
},
{
accessorKey: 'lastName',
header: 'Last Name',
size: 120,
},
{
accessorKey: 'company',
header: 'Company',
size: 300,
},
{
accessorKey: 'city',
header: 'City',
},
{
accessorKey: 'country',
header: 'Country',
size: 220,
},
];
const csvConfig = mkConfig({
fieldSeparator: ',',
decimalSeparator: '.',
useKeysAsHeaders: true,
});
const Example = () => {
const handleExportRows = (rows: MRT_Row<Person>[]) => {
const rowData = rows.map((row) => row.original);
const csv = generateCsv(csvConfig)(rowData);
download(csvConfig)(csv);
};
const handleExportData = () => {
const csv = generateCsv(csvConfig)(data);
download(csvConfig)(csv);
};
const table = useMantineReactTable({
columns,
data,
enableRowSelection: true,
columnFilterDisplayMode: 'popover',
paginationDisplayMode: 'pages',
positionToolbarAlertBanner: 'bottom',
renderTopToolbarCustomActions: ({ table }) => (
<Box
style={{
display: 'flex',
gap: '16px',
padding: '8px',
flexWrap: 'wrap',
}}
>
<Button
color="lightblue"
//export all data that is currently in the table (ignore pagination, sorting, filtering, etc.)
onClick={handleExportData}
leftSection={<IconDownload />}
variant="filled"
>
Export All Data
</Button>
<Button
disabled={table.getPrePaginationRowModel().rows.length === 0}
//export all rows, including from the next page, (still respects filtering and sorting)
onClick={() =>
handleExportRows(table.getPrePaginationRowModel().rows)
}
leftSection={<IconDownload />}
variant="filled"
>
Export All Rows
</Button>
<Button
disabled={table.getRowModel().rows.length === 0}
//export all rows as seen on the screen (respects pagination, sorting, filtering, etc.)
onClick={() => handleExportRows(table.getRowModel().rows)}
leftSection={<IconDownload />}
variant="filled"
>
Export Page Rows
</Button>
<Button
disabled={
!table.getIsSomeRowsSelected() && !table.getIsAllRowsSelected()
}
//only export selected rows
onClick={() => handleExportRows(table.getSelectedRowModel().rows)}
leftSection={<IconDownload />}
variant="filled"
>
Export Selected Rows
</Button>
</Box>
),
});
return <MantineReactTable table={table} />;
};
export default Example;