1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
23 } from "@material-ui/core";
24 import * as classnames from "classnames";
25 import { DefaultTransformOrigin } from "../popover/helpers";
27 export type CssRules = "root" | "icon" | "active" | "checkbox";
29 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
32 display: "inline-flex",
33 justifyContent: "flex-start",
34 flexDirection: "inherit",
37 color: theme.palette.text.primary,
40 color: theme.palette.text.primary,
44 color: theme.palette.text.primary,
62 export interface DataTableFilterItem {
67 export interface DataTableFilterProps {
69 filters: DataTableFilterItem[];
70 onChange?: (filters: DataTableFilterItem[]) => void;
73 interface DataTableFilterState {
74 anchorEl?: HTMLElement;
75 filters: DataTableFilterItem[];
76 prevFilters: DataTableFilterItem[];
79 export const DataTableFilters = withStyles(styles)(
80 class extends React.Component<DataTableFilterProps & WithStyles<CssRules>, DataTableFilterState> {
81 state: DataTableFilterState = {
86 icon = React.createRef<HTMLElement>();
89 const { name, classes, children } = this.props;
90 const isActive = this.state.filters.some(f => f.selected);
92 <Tooltip title='Filters'>
94 className={classnames([classes.root, { [classes.active]: isActive }])}
99 <i className={classnames(["fas fa-filter", classes.icon])}
100 data-fa-transform="shrink-3"
105 anchorEl={this.state.anchorEl}
106 open={!!this.state.anchorEl}
107 anchorOrigin={DefaultTransformOrigin}
108 transformOrigin={DefaultTransformOrigin}
109 onClose={this.cancel}>
112 <Typography variant="caption">
117 {this.state.filters.map((filter, index) =>
121 onClick={this.toggleFilter(filter)}>
125 checked={filter.selected}
126 className={classes.checkbox} />
138 onClick={this.submit}>
145 onClick={this.cancel}>
154 static getDerivedStateFromProps(props: DataTableFilterProps, state: DataTableFilterState): DataTableFilterState {
155 return props.filters !== state.prevFilters
156 ? { ...state, filters: props.filters, prevFilters: props.filters }
161 this.setState({ anchorEl: this.icon.current || undefined });
165 const { onChange } = this.props;
167 onChange(this.state.filters);
169 this.setState({ anchorEl: undefined });
173 this.setState(prev => ({
175 filters: prev.prevFilters,
180 toggleFilter = (toggledFilter: DataTableFilterItem) => () => {
181 this.setState(prev => ({
183 filters: prev.filters.map(filter =>
184 filter === toggledFilter
185 ? { ...filter, selected: !filter.selected }