mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-18 02:05:36 +00:00
84 lines
3.2 KiB
TypeScript
84 lines
3.2 KiB
TypeScript
|
|
import React from "react";
|
||
|
|
import {Paper, Table, TableBody, TableCell, TableContainer, TableHead, TablePagination, TableRow} from "@mui/material";
|
||
|
|
|
||
|
|
interface Column {
|
||
|
|
id: string;
|
||
|
|
label: string;
|
||
|
|
minWidth?: number;
|
||
|
|
align?: 'right';
|
||
|
|
format?: (value: number) => string;
|
||
|
|
}
|
||
|
|
|
||
|
|
interface Data {
|
||
|
|
name: string;
|
||
|
|
code: string;
|
||
|
|
population: number;
|
||
|
|
size: number;
|
||
|
|
density: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export default function StickyHeadTable({columns, rows}: { rows: Data[], columns: Column[] }) {
|
||
|
|
const [page, setPage] = React.useState(0);
|
||
|
|
const [rowsPerPage, setRowsPerPage] = React.useState(10);
|
||
|
|
|
||
|
|
const handleChangePage = (event: unknown, newPage: number) => {
|
||
|
|
setPage(newPage);
|
||
|
|
};
|
||
|
|
|
||
|
|
const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||
|
|
setRowsPerPage(+event.target.value);
|
||
|
|
setPage(0);
|
||
|
|
};
|
||
|
|
|
||
|
|
return (
|
||
|
|
<Paper sx={{width: '100%', overflow: 'hidden'}}>
|
||
|
|
<TableContainer sx={{maxHeight: 440}}>
|
||
|
|
<Table stickyHeader aria-label="sticky table">
|
||
|
|
<TableHead>
|
||
|
|
<TableRow>
|
||
|
|
{columns.map((column) => (
|
||
|
|
<TableCell
|
||
|
|
key={column.id}
|
||
|
|
align={column.align}
|
||
|
|
style={{minWidth: column.minWidth}}
|
||
|
|
>
|
||
|
|
{column.label}
|
||
|
|
</TableCell>
|
||
|
|
))}
|
||
|
|
</TableRow>
|
||
|
|
</TableHead>
|
||
|
|
<TableBody>
|
||
|
|
{rows
|
||
|
|
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
|
||
|
|
.map((row: Data) => {
|
||
|
|
return (
|
||
|
|
<TableRow hover role="checkbox" tabIndex={-1} key={row.code}>
|
||
|
|
{columns.map((column) => {
|
||
|
|
const value = row[column.id as keyof typeof row]
|
||
|
|
return (
|
||
|
|
<TableCell key={column.id} align={column.align}>
|
||
|
|
{column.format && typeof value === 'number'
|
||
|
|
? column.format(value)
|
||
|
|
: value}
|
||
|
|
</TableCell>
|
||
|
|
);
|
||
|
|
})}
|
||
|
|
</TableRow>
|
||
|
|
);
|
||
|
|
})}
|
||
|
|
</TableBody>
|
||
|
|
</Table>
|
||
|
|
</TableContainer>
|
||
|
|
<TablePagination
|
||
|
|
rowsPerPageOptions={[10, 25, 100]}
|
||
|
|
component="div"
|
||
|
|
count={rows.length}
|
||
|
|
rowsPerPage={rowsPerPage}
|
||
|
|
page={page}
|
||
|
|
onPageChange={handleChangePage}
|
||
|
|
onRowsPerPageChange={handleChangeRowsPerPage}
|
||
|
|
/>
|
||
|
|
</Paper>
|
||
|
|
);
|
||
|
|
}
|