Add checkbox column to tables

This commit is contained in:
Ken Hibino 2020-12-18 06:48:00 -08:00
parent fa1c9ee7ef
commit 95bb6051d0
3 changed files with 93 additions and 8 deletions

View File

@ -241,7 +241,9 @@ function DeadTasksTable(props: Props & ReduxProps) {
indeterminate={numSelected > 0 && numSelected < rowCount} indeterminate={numSelected > 0 && numSelected < rowCount}
checked={rowCount > 0 && numSelected === rowCount} checked={rowCount > 0 && numSelected === rowCount}
onChange={handleSelectAllClick} onChange={handleSelectAllClick}
inputProps={{ "aria-label": "select all dead tasks" }} inputProps={{
"aria-label": "select all tasks shown in the table",
}}
/> />
</TableCell> </TableCell>
{columns.map((col) => ( {columns.map((col) => (
@ -311,8 +313,6 @@ function Row(props: RowProps) {
const { task } = props; const { task } = props;
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const classes = useRowStyles(); const classes = useRowStyles();
const labelId = `dead-tasks-table-checkbox-${task.id}`;
return ( return (
<React.Fragment> <React.Fragment>
<TableRow key={task.id} className={classes.root}> <TableRow key={task.id} className={classes.root}>
@ -322,7 +322,6 @@ function Row(props: RowProps) {
props.onSelectChange(event.target.checked) props.onSelectChange(event.target.checked)
} }
checked={props.isSelected} checked={props.isSelected}
inputProps={{ "aria-labelledby": labelId }}
/> />
</TableCell> </TableCell>
<TableCell> <TableCell>

View File

@ -12,6 +12,7 @@ import TableFooter from "@material-ui/core/TableFooter";
import TablePagination from "@material-ui/core/TablePagination"; import TablePagination from "@material-ui/core/TablePagination";
import Paper from "@material-ui/core/Paper"; import Paper from "@material-ui/core/Paper";
import Box from "@material-ui/core/Box"; import Box from "@material-ui/core/Box";
import Checkbox from "@material-ui/core/Checkbox";
import Collapse from "@material-ui/core/Collapse"; import Collapse from "@material-ui/core/Collapse";
import IconButton from "@material-ui/core/IconButton"; import IconButton from "@material-ui/core/IconButton";
import KeyboardArrowUpIcon from "@material-ui/icons/KeyboardArrowUp"; import KeyboardArrowUpIcon from "@material-ui/icons/KeyboardArrowUp";
@ -64,6 +65,7 @@ function RetryTasksTable(props: Props & ReduxProps) {
const classes = useStyles(); const classes = useStyles();
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [pageSize, setPageSize] = useState(defaultPageSize); const [pageSize, setPageSize] = useState(defaultPageSize);
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
const handleChangePage = ( const handleChangePage = (
event: React.MouseEvent<HTMLButtonElement> | null, event: React.MouseEvent<HTMLButtonElement> | null,
@ -79,6 +81,15 @@ function RetryTasksTable(props: Props & ReduxProps) {
setPage(0); setPage(0);
}; };
const handleSelectAllClick = (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.checked) {
const newSelected = props.tasks.map((t) => t.key);
setSelectedKeys(newSelected);
} else {
setSelectedKeys([]);
}
};
const fetchData = useCallback(() => { const fetchData = useCallback(() => {
const pageOpts = { page: page + 1, size: pageSize }; const pageOpts = { page: page + 1, size: pageSize };
listRetryTasksAsync(queue, pageOpts); listRetryTasksAsync(queue, pageOpts);
@ -106,6 +117,8 @@ function RetryTasksTable(props: Props & ReduxProps) {
{ label: "Actions" }, { label: "Actions" },
]; ];
const rowCount = props.tasks.length;
const numSelected = selectedKeys.length;
return ( return (
<TableContainer component={Paper}> <TableContainer component={Paper}>
<Table <Table
@ -116,6 +129,16 @@ function RetryTasksTable(props: Props & ReduxProps) {
> >
<TableHead> <TableHead>
<TableRow> <TableRow>
<TableCell padding="checkbox">
<Checkbox
indeterminate={numSelected > 0 && numSelected < rowCount}
checked={rowCount > 0 && numSelected === rowCount}
onChange={handleSelectAllClick}
inputProps={{
"aria-label": "select all tasks shown in the table",
}}
/>
</TableCell>
{columns.map((col) => ( {columns.map((col) => (
<TableCell key={col.label}>{col.label}</TableCell> <TableCell key={col.label}>{col.label}</TableCell>
))} ))}
@ -126,6 +149,16 @@ function RetryTasksTable(props: Props & ReduxProps) {
<Row <Row
key={task.id} key={task.id}
task={task} task={task}
isSelected={selectedKeys.includes(task.key)}
onSelectChange={(checked: boolean) => {
if (checked) {
setSelectedKeys(selectedKeys.concat(task.key));
} else {
setSelectedKeys(
selectedKeys.filter((key) => key !== task.key)
);
}
}}
onDeleteClick={() => { onDeleteClick={() => {
props.deleteRetryTaskAsync(task.queue, task.key); props.deleteRetryTaskAsync(task.queue, task.key);
}} }}
@ -136,7 +169,7 @@ function RetryTasksTable(props: Props & ReduxProps) {
<TableRow> <TableRow>
<TablePagination <TablePagination
rowsPerPageOptions={rowsPerPageOptions} rowsPerPageOptions={rowsPerPageOptions}
colSpan={columns.length} colSpan={columns.length + 1}
count={props.totalTaskCount} count={props.totalTaskCount}
rowsPerPage={pageSize} rowsPerPage={pageSize}
page={page} page={page}
@ -165,6 +198,8 @@ const useRowStyles = makeStyles({
interface RowProps { interface RowProps {
task: RetryTaskExtended; task: RetryTaskExtended;
isSelected: boolean;
onSelectChange: (checked: boolean) => void;
onDeleteClick: () => void; onDeleteClick: () => void;
} }
@ -175,6 +210,14 @@ function Row(props: RowProps) {
return ( return (
<React.Fragment> <React.Fragment>
<TableRow key={task.id} className={classes.root}> <TableRow key={task.id} className={classes.root}>
<TableCell padding="checkbox">
<Checkbox
onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
props.onSelectChange(event.target.checked)
}
checked={props.isSelected}
/>
</TableCell>
<TableCell> <TableCell>
<IconButton <IconButton
aria-label="expand row" aria-label="expand row"
@ -199,7 +242,7 @@ function Row(props: RowProps) {
</TableCell> </TableCell>
</TableRow> </TableRow>
<TableRow> <TableRow>
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={9}> <TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={10}>
<Collapse in={open} timeout="auto" unmountOnExit> <Collapse in={open} timeout="auto" unmountOnExit>
<Box margin={1}> <Box margin={1}>
<Typography variant="h6" gutterBottom component="div"> <Typography variant="h6" gutterBottom component="div">

View File

@ -12,6 +12,7 @@ import TableFooter from "@material-ui/core/TableFooter";
import TablePagination from "@material-ui/core/TablePagination"; import TablePagination from "@material-ui/core/TablePagination";
import Paper from "@material-ui/core/Paper"; import Paper from "@material-ui/core/Paper";
import Box from "@material-ui/core/Box"; import Box from "@material-ui/core/Box";
import Checkbox from "@material-ui/core/Checkbox";
import Collapse from "@material-ui/core/Collapse"; import Collapse from "@material-ui/core/Collapse";
import IconButton from "@material-ui/core/IconButton"; import IconButton from "@material-ui/core/IconButton";
import KeyboardArrowUpIcon from "@material-ui/icons/KeyboardArrowUp"; import KeyboardArrowUpIcon from "@material-ui/icons/KeyboardArrowUp";
@ -67,6 +68,7 @@ function ScheduledTasksTable(props: Props & ReduxProps) {
const classes = useStyles(); const classes = useStyles();
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [pageSize, setPageSize] = useState(defaultPageSize); const [pageSize, setPageSize] = useState(defaultPageSize);
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
const handleChangePage = ( const handleChangePage = (
event: React.MouseEvent<HTMLButtonElement> | null, event: React.MouseEvent<HTMLButtonElement> | null,
@ -82,6 +84,15 @@ function ScheduledTasksTable(props: Props & ReduxProps) {
setPage(0); setPage(0);
}; };
const handleSelectAllClick = (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.checked) {
const newSelected = props.tasks.map((t) => t.key);
setSelectedKeys(newSelected);
} else {
setSelectedKeys([]);
}
};
const fetchData = useCallback(() => { const fetchData = useCallback(() => {
const pageOpts = { page: page + 1, size: pageSize }; const pageOpts = { page: page + 1, size: pageSize };
listScheduledTasksAsync(queue, pageOpts); listScheduledTasksAsync(queue, pageOpts);
@ -106,6 +117,8 @@ function ScheduledTasksTable(props: Props & ReduxProps) {
{ label: "Actions" }, { label: "Actions" },
]; ];
const rowCount = props.tasks.length;
const numSelected = selectedKeys.length;
return ( return (
<TableContainer component={Paper}> <TableContainer component={Paper}>
<Table <Table
@ -116,6 +129,16 @@ function ScheduledTasksTable(props: Props & ReduxProps) {
> >
<TableHead> <TableHead>
<TableRow> <TableRow>
<TableCell padding="checkbox">
<Checkbox
indeterminate={numSelected > 0 && numSelected < rowCount}
checked={rowCount > 0 && numSelected === rowCount}
onChange={handleSelectAllClick}
inputProps={{
"aria-label": "select all tasks shown in the table",
}}
/>
</TableCell>
{columns.map((col) => ( {columns.map((col) => (
<TableCell key={col.label}>{col.label}</TableCell> <TableCell key={col.label}>{col.label}</TableCell>
))} ))}
@ -126,6 +149,16 @@ function ScheduledTasksTable(props: Props & ReduxProps) {
<Row <Row
key={task.id} key={task.id}
task={task} task={task}
isSelected={selectedKeys.includes(task.key)}
onSelectChange={(checked: boolean) => {
if (checked) {
setSelectedKeys(selectedKeys.concat(task.key));
} else {
setSelectedKeys(
selectedKeys.filter((key) => key !== task.key)
);
}
}}
onDeleteClick={() => { onDeleteClick={() => {
props.deleteScheduledTaskAsync(queue, task.key); props.deleteScheduledTaskAsync(queue, task.key);
}} }}
@ -136,7 +169,7 @@ function ScheduledTasksTable(props: Props & ReduxProps) {
<TableRow> <TableRow>
<TablePagination <TablePagination
rowsPerPageOptions={rowsPerPageOptions} rowsPerPageOptions={rowsPerPageOptions}
colSpan={columns.length} colSpan={columns.length + 1}
count={props.totalTaskCount} count={props.totalTaskCount}
rowsPerPage={pageSize} rowsPerPage={pageSize}
page={page} page={page}
@ -165,6 +198,8 @@ const useRowStyles = makeStyles({
interface RowProps { interface RowProps {
task: ScheduledTaskExtended; task: ScheduledTaskExtended;
isSelected: boolean;
onSelectChange: (checked: boolean) => void;
onDeleteClick: () => void; onDeleteClick: () => void;
} }
@ -175,6 +210,14 @@ function Row(props: RowProps) {
return ( return (
<React.Fragment> <React.Fragment>
<TableRow key={task.id} className={classes.root}> <TableRow key={task.id} className={classes.root}>
<TableCell padding="checkbox">
<Checkbox
onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
props.onSelectChange(event.target.checked)
}
checked={props.isSelected}
/>
</TableCell>
<TableCell> <TableCell>
<IconButton <IconButton
aria-label="expand row" aria-label="expand row"
@ -196,7 +239,7 @@ function Row(props: RowProps) {
</TableCell> </TableCell>
</TableRow> </TableRow>
<TableRow> <TableRow>
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={5}> <TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
<Collapse in={open} timeout="auto" unmountOnExit> <Collapse in={open} timeout="auto" unmountOnExit>
<Box margin={1}> <Box margin={1}>
<Typography variant="h6" gutterBottom component="div"> <Typography variant="h6" gutterBottom component="div">