Skip to content

Commit

Permalink
Fix filter for multiple values
Browse files Browse the repository at this point in the history
  • Loading branch information
keisuke-umezawa committed Apr 18, 2024
1 parent 34459a8 commit 80a8d50
Showing 1 changed file with 60 additions and 53 deletions.
113 changes: 60 additions & 53 deletions optuna_dashboard/ts/components/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
getFilteredRowModel,
getFacetedUniqueValues,
getFacetedRowModel,
Header,
SortingState,
PaginationState,
ColumnFiltersState,
Expand All @@ -58,6 +59,61 @@ const HiddenSpan = styled("span")({
width: 1,
})

function FilterMenu<T>(props: {
header: Header<T, unknown>
filterChoices: string[]
}): React.ReactElement {
const { header, filterChoices } = props
const [filterMenuAnchorEl, setFilterMenuAnchorEl] =
React.useState<null | HTMLElement>(null)
return (
<>
<IconButton
size="small"
onClick={(e) => {
setFilterMenuAnchorEl(e.currentTarget)
}}
>
<FilterListIcon fontSize="small" />
</IconButton>
<Menu
anchorEl={filterMenuAnchorEl}
open={filterMenuAnchorEl !== null}
onClose={() => {
setFilterMenuAnchorEl(null)
}}
>
{filterChoices.map((choice) => (
<MenuItem
key={choice}
onClick={() => {
const skippedValues = header.column.getFilterValue() as string[]
const isSkipped = skippedValues.includes(choice)
const newSkippedValues = isSkipped
? skippedValues.filter((v) => v !== choice)
: skippedValues.concat(choice)
header.column.setFilterValue(newSkippedValues)
}}
>
<ListItemIcon>
{header.column.getFilterValue() !== undefined ? (
(header.column.getFilterValue() as string[]).includes(
choice
) ? (
<CheckBoxOutlineBlankIcon color="primary" />
) : (
<CheckBoxIcon color="primary" />
)
) : null}
</ListItemIcon>
{choice ?? "(missing value)"}
</MenuItem>
))}
</Menu>
</>
)
}

function DataGrid<T>(props: {
data: T[]
columns: ColumnDef<T>[]
Expand All @@ -67,8 +123,6 @@ function DataGrid<T>(props: {
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
[]
)
const [filterMenuAnchorEl, setFilterMenuAnchorEl] =
React.useState<null | HTMLElement>(null)
const [pagination, setPagination] = React.useState<PaginationState>({
pageIndex: 0,
pageSize: 50,
Expand Down Expand Up @@ -143,57 +197,10 @@ function DataGrid<T>(props: {
)
)}
{filterChoices !== null ? (
<>
<IconButton
size="small"
onClick={(e) => {
setFilterMenuAnchorEl(e.currentTarget)
}}
>
<FilterListIcon fontSize="small" />
</IconButton>
<Menu
anchorEl={filterMenuAnchorEl}
open={filterMenuAnchorEl !== null}
onClose={() => {
setFilterMenuAnchorEl(null)
}}
>
{filterChoices.map((choice) => (
<MenuItem
key={choice}
onClick={() => {
const skippedValues =
header.column.getFilterValue() as string[]
const isSkipped =
skippedValues.includes(choice)
const newSkippedValues = isSkipped
? skippedValues.filter(
(v) => v !== choice
)
: skippedValues.concat(choice)
header.column.setFilterValue(
newSkippedValues
)
}}
>
<ListItemIcon>
{header.column.getFilterValue() !==
undefined ? (
(
header.column.getFilterValue() as string[]
).includes(choice) ? (
<CheckBoxOutlineBlankIcon color="primary" />
) : (
<CheckBoxIcon color="primary" />
)
) : null}
</ListItemIcon>
{choice ?? "(missing value)"}
</MenuItem>
))}
</Menu>
</>
<FilterMenu
header={header}
filterChoices={filterChoices}
/>
) : null}
</TableHeaderCellSpan>
)}
Expand Down

0 comments on commit 80a8d50

Please sign in to comment.