From 982849c5b7b9aa926afac05d1ffd421ffdc31cc0 Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Wed, 19 May 2021 21:49:23 +0200 Subject: [PATCH] wip new filter component Signed-off-by: Emma Indal Signed-off-by: Eric Peterson --- .../components/FiltersNext/FiltersNext.tsx | 108 ++++++++++++++---- 1 file changed, 85 insertions(+), 23 deletions(-) diff --git a/plugins/search/src/components/FiltersNext/FiltersNext.tsx b/plugins/search/src/components/FiltersNext/FiltersNext.tsx index 770fe8078d..45de27b032 100644 --- a/plugins/search/src/components/FiltersNext/FiltersNext.tsx +++ b/plugins/search/src/components/FiltersNext/FiltersNext.tsx @@ -14,33 +14,41 @@ * limitations under the License. */ -import React from 'react'; import { - makeStyles, - Typography, Card, CardContent, - Select, Checkbox, + FormControl, + InputLabel, List, + MenuItem, ListItem, ListItemText, - MenuItem, + makeStyles, + Select, } from '@material-ui/core'; +import React from 'react'; +import { useSearch } from '../SearchContext'; -const useStyles = makeStyles(theme => ({ +const useFilterStyles = makeStyles({ filters: { background: 'transparent', boxShadow: '0px 0px 0px 0px', }, - // checkbox: { - // padding: theme.spacing(0, 1, 0, 1), - // }, - // dropdown: { - // width: '100%', - // }, +}); + +const useCheckBoxStyles = makeStyles(theme => ({ + checkbox: { + padding: theme.spacing(0, 1, 0, 1), + }, })); +const useSelectStyles = makeStyles({ + select: { + width: '100%', + }, +}); + export type FilterOptions = { kind: Array; lifecycle: Array; @@ -72,13 +80,45 @@ export type FilterDefinition = { }; const CheckBoxFilter = ({ fieldName, values }: ValuedFilterProps) => { + const { filters, setFilters } = useSearch(); + const classes = useCheckBoxStyles(); + + const setCheckboxFilter = (filter: string) => { + const newFilters = filters; + const currentValues = newFilters[fieldName] as string[]; + + if (!filter) return; + + if (!currentValues) { + setFilters({ ...filters, [fieldName]: [filter] }); + } else if (!currentValues?.includes(filter)) { + setFilters({ + ...filters, + [fieldName]: [...currentValues, filter], + }); + } else { + const filterToDelete = currentValues.find(value => value === filter); + if (filterToDelete) { + currentValues.splice(currentValues.indexOf(filterToDelete), 1); + + setFilters({ ...filters, [fieldName]: currentValues }); + } + } + }; return ( - {fieldName} - + {fieldName} + {values.map((value: string) => ( - {}}> + setCheckboxFilter(value)} + > { }; const SelectFilter = ({ fieldName, values }: ValuedFilterProps) => { + const { filters, setFilters } = useSearch(); + const classes = useSelectStyles(); + + const setSelectFilter = (filter: string) => { + const newFilters = filters; + if (newFilters[fieldName] && filter === '') { + delete newFilters[fieldName]; + setFilters({ newFilters }); + } else { + setFilters({ ...filters, [fieldName]: filter as string }); + } + }; + return ( - {fieldName} - ) => + setSelectFilter(e?.target?.value) + } + > + + All - ))} - + {values.map((value: string) => ( + {value} + ))} + + ); }; export const FiltersNext = ({ definitions }: FiltersProps) => { - const classes = useStyles(); + const classes = useFilterStyles(); return (