Some checks failed
No response / noResponse (push) Has been cancelled
CI / Continuous releases (push) Has been cancelled
CI / test-dev (macos-latest) (push) Has been cancelled
CI / test-dev (ubuntu-latest) (push) Has been cancelled
CI / test-dev (windows-latest) (push) Has been cancelled
Maintenance / main (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CodeQL / Analyze (push) Has been cancelled
113 lines
2.6 KiB
JavaScript
113 lines
2.6 KiB
JavaScript
import * as React from 'react';
|
|
import Table from '@mui/material/Table';
|
|
import TableBody from '@mui/material/TableBody';
|
|
import TableCell from '@mui/material/TableCell';
|
|
import TableContainer from '@mui/material/TableContainer';
|
|
import TableHead from '@mui/material/TableHead';
|
|
import TableRow from '@mui/material/TableRow';
|
|
import Paper from '@mui/material/Paper';
|
|
import { TableVirtuoso } from 'react-virtuoso';
|
|
import Chance from 'chance';
|
|
|
|
const chance = new Chance(42);
|
|
|
|
function createData(id) {
|
|
return {
|
|
id,
|
|
firstName: chance.first(),
|
|
lastName: chance.last(),
|
|
age: chance.age(),
|
|
phone: chance.phone(),
|
|
state: chance.state({ full: true }),
|
|
};
|
|
}
|
|
|
|
const columns = [
|
|
{
|
|
width: 100,
|
|
label: 'First Name',
|
|
dataKey: 'firstName',
|
|
},
|
|
{
|
|
width: 100,
|
|
label: 'Last Name',
|
|
dataKey: 'lastName',
|
|
},
|
|
{
|
|
width: 50,
|
|
label: 'Age',
|
|
dataKey: 'age',
|
|
numeric: true,
|
|
},
|
|
{
|
|
width: 110,
|
|
label: 'State',
|
|
dataKey: 'state',
|
|
},
|
|
{
|
|
width: 130,
|
|
label: 'Phone Number',
|
|
dataKey: 'phone',
|
|
},
|
|
];
|
|
|
|
const rows = Array.from({ length: 200 }, (_, index) => createData(index));
|
|
|
|
const VirtuosoTableComponents = {
|
|
Scroller: React.forwardRef((props, ref) => (
|
|
<TableContainer component={Paper} {...props} ref={ref} />
|
|
)),
|
|
Table: (props) => (
|
|
<Table {...props} sx={{ borderCollapse: 'separate', tableLayout: 'fixed' }} />
|
|
),
|
|
TableHead: React.forwardRef((props, ref) => <TableHead {...props} ref={ref} />),
|
|
TableRow,
|
|
TableBody: React.forwardRef((props, ref) => <TableBody {...props} ref={ref} />),
|
|
};
|
|
|
|
function fixedHeaderContent() {
|
|
return (
|
|
<TableRow>
|
|
{columns.map((column) => (
|
|
<TableCell
|
|
key={column.dataKey}
|
|
variant="head"
|
|
align={column.numeric || false ? 'right' : 'left'}
|
|
style={{ width: column.width }}
|
|
sx={{ backgroundColor: 'background.paper' }}
|
|
>
|
|
{column.label}
|
|
</TableCell>
|
|
))}
|
|
</TableRow>
|
|
);
|
|
}
|
|
|
|
function rowContent(_index, row) {
|
|
return (
|
|
<React.Fragment>
|
|
{columns.map((column) => (
|
|
<TableCell
|
|
key={column.dataKey}
|
|
align={column.numeric || false ? 'right' : 'left'}
|
|
>
|
|
{row[column.dataKey]}
|
|
</TableCell>
|
|
))}
|
|
</React.Fragment>
|
|
);
|
|
}
|
|
|
|
export default function ReactVirtualizedTable() {
|
|
return (
|
|
<Paper style={{ height: 400, width: '100%' }}>
|
|
<TableVirtuoso
|
|
data={rows}
|
|
components={VirtuosoTableComponents}
|
|
fixedHeaderContent={fixedHeaderContent}
|
|
itemContent={rowContent}
|
|
/>
|
|
</Paper>
|
|
);
|
|
}
|