1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
19 } from "@material-ui/core";
20 import * as classnames from "classnames";
21 import { DefaultTransformOrigin } from "~/components/popover/helpers";
22 import { createTree } from '~/models/tree';
23 import { DataTableFilters, DataTableFiltersTree } from "./data-table-filters-tree";
24 import { getNodeDescendants } from '~/models/tree';
26 export type CssRules = "root" | "icon" | "active" | "checkbox";
28 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
31 display: "inline-flex",
32 justifyContent: "flex-start",
33 flexDirection: "inherit",
36 color: theme.palette.text.primary,
39 color: theme.palette.text.primary,
43 color: theme.palette.text.primary,
61 export interface DataTableFilterProps {
63 filters: DataTableFilters;
64 onChange?: (filters: DataTableFilters) => void;
67 interface DataTableFilterState {
68 anchorEl?: HTMLElement;
69 filters: DataTableFilters;
70 prevFilters: DataTableFilters;
73 export const DataTableFiltersPopover = withStyles(styles)(
74 class extends React.Component<DataTableFilterProps & WithStyles<CssRules>, DataTableFilterState> {
75 state: DataTableFilterState = {
77 filters: createTree(),
78 prevFilters: createTree(),
80 icon = React.createRef<HTMLElement>();
83 const { name, classes, children } = this.props;
84 const isActive = getNodeDescendants('')(this.state.filters).some(f => f.selected);
86 <Tooltip title='Filters'>
88 className={classnames([classes.root, { [classes.active]: isActive }])}
93 <i className={classnames(["fas fa-filter", classes.icon])}
94 data-fa-transform="shrink-3"
99 anchorEl={this.state.anchorEl}
100 open={!!this.state.anchorEl}
101 anchorOrigin={DefaultTransformOrigin}
102 transformOrigin={DefaultTransformOrigin}
103 onClose={this.cancel}>
106 <Typography variant="caption">
110 <DataTableFiltersTree
111 filters={this.state.filters}
112 onChange={filters => this.setState({ filters })} />
118 onClick={this.submit}>
125 onClick={this.cancel}>
134 static getDerivedStateFromProps(props: DataTableFilterProps, state: DataTableFilterState): DataTableFilterState {
135 return props.filters !== state.prevFilters
136 ? { ...state, filters: props.filters, prevFilters: props.filters }
141 this.setState({ anchorEl: this.icon.current || undefined });
145 const { onChange } = this.props;
147 onChange(this.state.filters);
149 this.setState({ anchorEl: undefined });
153 this.setState(prev => ({
155 filters: prev.prevFilters,
160 setFilters = (filters: DataTableFilters) => {
161 this.setState({ filters });