Skip to content

Commit

Permalink
RunList: Use custom filter
Browse files Browse the repository at this point in the history
- Use custom filter to show filter inputs
  outside the table
- Add a button to expand/hide filter options
- Display sort button only on hover over the column title
- Disable column action buttons (all of it's functions
  can be performed with sort, filter, and show/hide column
  buttons)

Signed-off-by: Vallari Agrawal <[email protected]>
  • Loading branch information
VallariAg committed Nov 13, 2024
1 parent e2bad8f commit 07b1729
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
66 changes: 56 additions & 10 deletions src/components/RunList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useMemo } from 'react';
import { useState } from "react";
import OpenInNewIcon from "@mui/icons-material/OpenInNew";
import Grid from '@mui/material/Grid';
import Button from '@mui/material/Button';
import type {
DecodedValueMap,
QueryParamConfigMap,
Expand All @@ -9,6 +12,7 @@ import { useDebounce } from "usehooks-ts";
import {
useMaterialReactTable,
MaterialReactTable,
MRT_TableHeadCellFilterContainer,
type MRT_ColumnDef,
type MRT_PaginationState,
type MRT_Updater,
Expand Down Expand Up @@ -38,14 +42,21 @@ const NON_FILTER_PARAMS = [
"page",
"pageSize",
];
const FILTER_COLUMNS = [
"status",
"scheduled",
"suite",
"branch",
"machine_type",
"sha1",
]

const _columns: MRT_ColumnDef<Run>[] = [
{
accessorKey: "name",
header: "link",
maxSize: 12,
enableColumnFilter: false,
enableColumnActions: false,
Cell: ({ row }) => {
return (
<IconLink to={`/runs/${row.original.name}`}>
Expand All @@ -62,15 +73,13 @@ const _columns: MRT_ColumnDef<Run>[] = [
return row.original.status.replace("finished ", "");
},
filterSelectOptions: Object.values(RunStatuses),
size: 40,
enableColumnActions: false,
maxSize: 25,
},
{
accessorKey: "user",
header: "user",
maxSize: 45,
enableColumnFilter: false,
enableColumnActions: false,
},
{
id: "scheduled",
Expand All @@ -82,23 +91,23 @@ const _columns: MRT_ColumnDef<Run>[] = [
const date_: string[] = row.original.scheduled.split(" ");
return <div> {date_[0]} <br /> {date_[1]} </div>
},
size: 50,
size: 35,
},
{
id: "started",
header: "started",
accessorFn: (row: Run) => formatDate(row.started),
enableColumnFilter: false,
sortingFn: "datetime",
size: 125,
size: 35,
},
{
id: "posted",
header: "updated",
accessorFn: (row: Run) => formatDate(row.posted),
enableColumnFilter: false,
sortingFn: "datetime",
size: 125,
maxSize: 35,
},
{
id: "runtime",
Expand All @@ -116,12 +125,12 @@ const _columns: MRT_ColumnDef<Run>[] = [
{
accessorKey: "suite",
header: "suite",
size: 70,
size: 50,
},
{
accessorKey: "branch",
header: "branch",
maxSize: 75,
maxSize: 70,
},
{
accessorKey: "machine_type",
Expand Down Expand Up @@ -200,6 +209,11 @@ type RunListProps = {
}

export default function RunList(props: RunListProps) {
const [openFilterMenu, setOpenFilterMenu] = useState(false);

const toggleFilterMenu = (isOpen: boolean) => () => {
setOpenFilterMenu(isOpen);
};
const { params, setter, tableOptions } = props;
const options = useDefaultTableOptions<Run>();
const debouncedParams = useDebounce(params, 500);
Expand Down Expand Up @@ -267,12 +281,17 @@ export default function RunList(props: RunListProps) {
data: data,
manualPagination: true,
manualFiltering: true,
enableColumnActions: false,
onPaginationChange,
rowCount: Infinity,
muiPaginationProps: {
showLastButton: false,
},
onColumnFiltersChange,
columnFilterDisplayMode: 'custom',
muiFilterTextFieldProps: ({ column }) => ({
label: `Filter by ${column.columnDef.header}`,
}),
initialState: {
...options.initialState,
columnVisibility: {
Expand Down Expand Up @@ -300,5 +319,32 @@ export default function RunList(props: RunListProps) {
...tableOptions,
});
if (query.isError) return null;
return <MaterialReactTable table={table} />
return (
<div>
{ openFilterMenu? (
<Grid container spacing={3} style={{padding: "10px"}}>
{table.getLeafHeaders().map((header) => {
console.log(header.id)
if (FILTER_COLUMNS.includes(header.id)) {
return (
<Grid item xs={2}>
<MRT_TableHeadCellFilterContainer
key={header.id}
header={header}
table={table}
style={{backgroundColor: "None"}}
in
/>
</Grid>
)
}
})}
</Grid>)
: ""}
<Button onClick={toggleFilterMenu(!openFilterMenu)} >
{openFilterMenu ? "Hide": "Show"} Filters
</Button>
<MaterialReactTable table={table} />
</div>
)
}
13 changes: 13 additions & 0 deletions src/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ export default function useDefaultTableOptions<TData extends MRT_RowData>(): Par
mrtTheme: {
baseBackgroundColor: theme.palette.background.default,
},
muiTableHeadCellProps: {
sx: {
'& .Mui-TableHeadCell-Content': {
fontSize: "0.8em",
},
'& .MuiTableSortLabel-root': {
display: "none",
},
'&:hover .MuiTableSortLabel-root': {
display: "block",
},
},
},
muiTableBodyCellProps: {
sx: {
color: "black",
Expand Down

0 comments on commit 07b1729

Please sign in to comment.