1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
22 } from "@material-ui/core";
23 import * as classnames from "classnames";
24 import { DefaultTransformOrigin } from "../popover/helpers";
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 DataTableFilterItem {
66 export interface DataTableFilterProps {
68 filters: DataTableFilterItem[];
69 onChange?: (filters: DataTableFilterItem[]) => void;
72 interface DataTableFilterState {
73 anchorEl?: HTMLElement;
74 filters: DataTableFilterItem[];
75 prevFilters: DataTableFilterItem[];
78 export const DataTableFilters = withStyles(styles)(
79 class extends React.Component<DataTableFilterProps & WithStyles<CssRules>, DataTableFilterState> {
80 state: DataTableFilterState = {
85 icon = React.createRef<HTMLElement>();
88 const { name, classes, children } = this.props;
89 const isActive = this.state.filters.some(f => f.selected);
92 className={classnames([classes.root, { [classes.active]: isActive }])}
97 <i className={classnames(["fas fa-filter", classes.icon])}
98 data-fa-transform="shrink-3"
102 anchorEl={this.state.anchorEl}
103 open={!!this.state.anchorEl}
104 anchorOrigin={DefaultTransformOrigin}
105 transformOrigin={DefaultTransformOrigin}
106 onClose={this.cancel}>
109 <Typography variant="caption">
114 {this.state.filters.map((filter, index) =>
118 onClick={this.toggleFilter(filter)}>
122 checked={filter.selected}
123 className={classes.checkbox} />
135 onClick={this.submit}>
142 onClick={this.cancel}>
151 static getDerivedStateFromProps(props: DataTableFilterProps, state: DataTableFilterState): DataTableFilterState {
152 return props.filters !== state.prevFilters
153 ? { ...state, filters: props.filters, prevFilters: props.filters }
158 this.setState({ anchorEl: this.icon.current || undefined });
162 const { onChange } = this.props;
164 onChange(this.state.filters);
166 this.setState({ anchorEl: undefined });
170 this.setState(prev => ({
172 filters: prev.prevFilters,
177 toggleFilter = (toggledFilter: DataTableFilterItem) => () => {
178 this.setState(prev => ({
180 filters: prev.filters.map(filter =>
181 filter === toggledFilter
182 ? { ...filter, selected: !filter.selected }