diff --git a/packages/core/src/components/Table/Filters.tsx b/packages/core/src/components/Table/Filters.tsx new file mode 100644 index 0000000000..db8a3a9af2 --- /dev/null +++ b/packages/core/src/components/Table/Filters.tsx @@ -0,0 +1,131 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React, { useEffect, useState } from 'react'; +import { BackstageTheme } from '@backstage/theme'; +import { Button, makeStyles } from '@material-ui/core'; +import { Select } from '../Select'; +import { CheckboxTree } from '../CheckboxTree'; +import { CheckboxTreeProps } from '../CheckboxTree/CheckboxTree'; +import { SelectProps } from '../Select/Select'; + +const useSubvalueCellStyles = makeStyles(theme => ({ + root: { + height: '100%', + width: '315px', + display: 'flex', + flexDirection: 'column', + marginRight: theme.spacing(3), + }, + value: {}, + header: { + display: 'flex', + alignItems: 'center', + height: '60px', + justifyContent: 'space-between', + borderBottom: `1px solid ${theme.palette.grey[500]}`, + }, + filters: { + display: 'flex', + flexDirection: 'column', + '& > *': { + marginTop: theme.spacing(2), + }, + }, + clear: { + textTransform: 'none', + }, +})); + +type Without = Pick>; + +export type Filter = { + type: 'select' | 'checkbox-tree' | 'multiple-select'; + element: + | Without + | Without; +}; + +type Props = { + filters: Filter[]; + onChangeFilters: (arg: any) => any; +}; + +export const Filters = (props: Props) => { + const classes = useSubvalueCellStyles(); + + const { onChangeFilters } = props; + + const [filters, setFilters] = useState(props.filters); + const [selectedFilters, setSelectedFilters] = useState({}); + + // Trigger re-rendering + const handleClick = () => { + setSelectedFilters({}); + setFilters([ + ...props.filters.map(el => ({ + type: el.type, + element: { ...el.element }, + })), + ]); + }; + + useEffect(() => { + onChangeFilters(selectedFilters); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [selectedFilters]); + + // As material table doesn't provide a way to add a column filter tab we will make our own filter logic + return ( +
+
+
Filters
+ +
+
+ {filters?.length && + filters.map(filter => + filter.type === 'checkbox-tree' ? ( + ({}) + // setSelectedFilters({ + // ...selectedFilters, + // [filter.element.label]: el, + // }) + } + /> + ) : ( +