import '@mantine/core/styles.css';
import '@mantine/dates/styles.css';
import 'mantine-react-table/styles.css';
import { useEffect, useMemo, useRef, useState } from 'react';
import {
MantineReactTable,
useMantineReactTable,
type MRT_ColumnDef,
type MRT_SortingState,
type MRT_RowVirtualizer,
} from 'mantine-react-table';
import { makeData, type Person } from './makeData';
const Example = () => {
const columns = useMemo<MRT_ColumnDef<Person>[]>(
() => [
{
accessorKey: 'firstName',
header: 'First Name',
size: 150,
},
{
accessorKey: 'middleName',
header: 'Middle Name',
size: 150,
},
{
accessorKey: 'lastName',
header: 'Last Name',
size: 150,
},
{
accessorKey: 'email',
header: 'Email Address',
size: 300,
},
{
accessorKey: 'phoneNumber',
header: 'Phone Number',
size: 250,
},
{
accessorKey: 'address',
header: 'Address',
size: 300,
},
{
accessorKey: 'zipCode',
header: 'Zip Code',
},
{
accessorKey: 'city',
header: 'City',
size: 220,
},
{
accessorKey: 'state',
header: 'State',
},
{
accessorKey: 'country',
header: 'Country',
size: 350,
},
{
accessorKey: 'petName',
header: 'Pet Name',
},
{
accessorKey: 'age',
header: 'Age',
},
{
accessorKey: 'salary',
header: 'Salary',
},
{
accessorKey: 'dateOfBirth',
header: 'Date of Birth',
},
{
accessorKey: 'dateOfJoining',
header: 'Date of Joining',
},
{
accessorKey: 'isActive',
header: 'Is Active',
},
],
[],
);
const rowVirtualizerInstanceRef = useRef<MRT_RowVirtualizer>(null);
const [data, setData] = useState<Person[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [sorting, setSorting] = useState<MRT_SortingState>([]);
useEffect(() => {
if (typeof window !== 'undefined') {
setData(makeData(10_000));
setIsLoading(false);
}
}, []);
useEffect(() => {
try {
rowVirtualizerInstanceRef.current?.scrollToIndex(0);
} catch (e) {
console.log(e);
}
}, [sorting]);
const table = useMantineReactTable({
columns,
data,
enableBottomToolbar: false,
enableColumnResizing: true,
enableColumnVirtualization: true,
enableGlobalFilterModes: true,
enablePagination: false,
enableColumnPinning: true,
enableRowNumbers: true,
enableRowVirtualization: true,
mantineTableContainerProps: { style: { maxHeight: '600px' } },
onSortingChange: setSorting,
state: { isLoading, sorting },
rowVirtualizerInstanceRef,
rowVirtualizerOptions: { overscan: 5 },
columnVirtualizerOptions: { overscan: 2 },
});
return <MantineReactTable table={table} />;
};
export default Example;