-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Filtering Structure for Data Table (#1436)
* added outside parts, now working on inside parts * filter bar v1 * updated tests * hitting pause on this awaiting design changes * separated out chips into chip group, addressed pr comments, changing styling to new design * styles and tests * readded icons
- Loading branch information
1 parent
986f4ef
commit 91b7f44
Showing
11 changed files
with
331 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Chip } from "@material-ui/core"; | ||
import _ from "lodash"; | ||
import * as React from "react"; | ||
import styled from "styled-components"; | ||
import Flex from "./Flex"; | ||
import Spacer from "./Spacer"; | ||
|
||
export interface Props { | ||
className?: string; | ||
/** currently checked filter options. Part of a `useState` with `setActiveChips` */ | ||
activeChips: string[]; | ||
/** the setState function for `activeChips` */ | ||
setActiveChips: (newChips: string[]) => void; | ||
} | ||
|
||
function ChipGroup({ className, activeChips, setActiveChips }: Props) { | ||
return ( | ||
<Flex className={className} align start> | ||
{_.map(activeChips, (filter, index) => { | ||
return ( | ||
<Flex key={index}> | ||
<Spacer padding="xxs" /> | ||
<Chip | ||
label={filter} | ||
onDelete={() => | ||
setActiveChips( | ||
activeChips.filter((filterCheck) => filterCheck !== filter) | ||
) | ||
} | ||
/> | ||
<Spacer padding="xxs" /> | ||
</Flex> | ||
); | ||
})} | ||
{activeChips.length > 0 && ( | ||
<Chip label="Clear All" onDelete={() => setActiveChips([])} /> | ||
)} | ||
</Flex> | ||
); | ||
} | ||
|
||
export default styled(ChipGroup).attrs({ className: ChipGroup.name })``; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { | ||
Checkbox, | ||
List, | ||
ListItem, | ||
ListItemIcon, | ||
Popover, | ||
} from "@material-ui/core"; | ||
import _ from "lodash"; | ||
import * as React from "react"; | ||
import styled from "styled-components"; | ||
import Button from "./Button"; | ||
import Flex from "./Flex"; | ||
import Icon, { IconType } from "./Icon"; | ||
import Spacer from "./Spacer"; | ||
import Text from "./Text"; | ||
|
||
const StyledPopover = styled(Popover)` | ||
.MuiPopover-paper { | ||
min-width: 450px; | ||
border-left: 2px solid ${(props) => props.theme.colors.neutral30}; | ||
padding-left: ${(props) => props.theme.spacing.medium}; | ||
} | ||
.MuiListItem-gutters { | ||
padding-left: 0px; | ||
} | ||
.MuiCheckbox-root { | ||
padding: 0px; | ||
} | ||
.MuiCheckbox-colorSecondary { | ||
&.Mui-checked { | ||
color: ${(props) => props.theme.colors.primary}; | ||
} | ||
} | ||
`; | ||
|
||
/** Filter Bar Properties */ | ||
export interface Props { | ||
className?: string; | ||
/** currently checked filter options. Part of a `useState` with `setActiveFilters` */ | ||
activeFilters: string[]; | ||
/** the setState function for `activeFilters` */ | ||
setActiveFilters: (newFilters: string[]) => void; | ||
/** Object containing column headers + corresponding filter options */ | ||
filterList: { [header: string]: string[] }; | ||
} | ||
|
||
/** Form Filter Bar */ | ||
function UnstyledFilterBar({ | ||
className, | ||
activeFilters, | ||
setActiveFilters, | ||
filterList, | ||
}: Props) { | ||
/** why isn't this a ref? It doesn't work. In The MUI Popover docs they do it with setState too... :( https://mui.com/components/popover/ */ | ||
const [anchorEl, setAnchorEl] = React.useState(null); | ||
const [showFilters, setShowFilters] = React.useState(false); | ||
|
||
const onCheck = (e: React.ChangeEvent<HTMLInputElement>, option: string) => { | ||
e.target.checked | ||
? setActiveFilters([...activeFilters, option]) | ||
: setActiveFilters( | ||
activeFilters.filter((filterCheck) => filterCheck !== option) | ||
); | ||
}; | ||
|
||
const onClose = () => setShowFilters(false); | ||
|
||
return ( | ||
<Flex className={className + " filter-bar"} align start> | ||
<Button | ||
variant="text" | ||
color="inherit" | ||
onClick={(e) => { | ||
if (!anchorEl) setAnchorEl(e.currentTarget); | ||
setShowFilters(!showFilters); | ||
}} | ||
> | ||
<Icon type={IconType.FilterIcon} size="medium" color="neutral30" /> | ||
</Button> | ||
<StyledPopover | ||
PaperProps={{ square: true }} | ||
elevation={0} | ||
open={showFilters} | ||
anchorEl={anchorEl} | ||
anchorOrigin={{ vertical: "bottom", horizontal: "left" }} | ||
onClose={onClose} | ||
> | ||
<Spacer padding="medium"> | ||
<Flex wide align between> | ||
<Text size="extraLarge" color="neutral30"> | ||
Filters | ||
</Text> | ||
<Button variant="text" color="inherit" onClick={onClose}> | ||
<Icon type={IconType.ClearIcon} size="large" color="neutral30" /> | ||
</Button> | ||
</Flex> | ||
<List> | ||
{_.map(filterList, (options: string[], header: string) => { | ||
return ( | ||
<ListItem key={header}> | ||
<Flex column> | ||
<Text size="large" color="neutral30"> | ||
{header} | ||
</Text> | ||
<List> | ||
{options.map((option: string, index: number) => { | ||
return ( | ||
<ListItem key={index}> | ||
<ListItemIcon> | ||
<Checkbox onChange={(e) => onCheck(e, option)} /> | ||
</ListItemIcon> | ||
<Text color="neutral30">{option}</Text> | ||
</ListItem> | ||
); | ||
})} | ||
</List> | ||
</Flex> | ||
</ListItem> | ||
); | ||
})} | ||
</List> | ||
</Spacer> | ||
</StyledPopover> | ||
</Flex> | ||
); | ||
} | ||
|
||
export const FilterBar = styled(UnstyledFilterBar)``; | ||
|
||
export default FilterBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import "jest-styled-components"; | ||
import React from "react"; | ||
import { withTheme } from "../../lib/test-utils"; | ||
import ChipGroup from "../ChipGroup"; | ||
|
||
describe("ChipGroup", () => { | ||
const setActiveChips = jest.fn(); | ||
const chipList = ["app", "app2", "app3"]; | ||
|
||
it("should render chips", () => { | ||
render( | ||
withTheme( | ||
<ChipGroup activeChips={chipList} setActiveChips={setActiveChips} /> | ||
) | ||
); | ||
expect(screen.queryByText("app")).toBeTruthy(); | ||
expect(screen.queryByText("app3")).toBeTruthy(); | ||
expect(screen.queryByText("Clear All")).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { fireEvent, render, screen } from "@testing-library/react"; | ||
import "jest-styled-components"; | ||
import React from "react"; | ||
import { withTheme } from "../../lib/test-utils"; | ||
import FilterBar from "../FilterBar"; | ||
|
||
describe("FilterBar", () => { | ||
const setActiveFilters = jest.fn(); | ||
const filterList = { | ||
Name: ["app", "app2", "app3"], | ||
Status: ["Ready", "Failed"], | ||
Type: ["Application", "Helm Release"], | ||
}; | ||
it("should initially render button with filter list closed", () => { | ||
render( | ||
withTheme( | ||
<FilterBar | ||
filterList={filterList} | ||
activeFilters={[]} | ||
setActiveFilters={setActiveFilters} | ||
/> | ||
) | ||
); | ||
expect(screen.getByRole("button")).toBeTruthy(); | ||
expect(screen.queryByText("Name")).toBeNull(); | ||
}); | ||
it("should reveal filter list on icon click", () => { | ||
render( | ||
withTheme( | ||
<FilterBar | ||
filterList={filterList} | ||
activeFilters={[]} | ||
setActiveFilters={setActiveFilters} | ||
/> | ||
) | ||
); | ||
const icon = screen.getAllByRole("button")[0]; | ||
fireEvent.click(icon); | ||
expect(screen.queryByText("Name")).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Meta, Story } from "@storybook/react"; | ||
import React from "react"; | ||
import ChipGroup, { Props } from "../components/ChipGroup"; | ||
|
||
export default { | ||
title: "ChipGroup", | ||
component: ChipGroup, | ||
parameters: { | ||
docs: { | ||
description: { | ||
component: | ||
"Series of deletable MUI Chip components: https://mui.com/components/chips/", | ||
}, | ||
}, | ||
}, | ||
} as Meta; | ||
|
||
const Template: Story<Props> = (args) => { | ||
const [storyChips, setStoryChips] = React.useState([ | ||
"chip", | ||
"chippy", | ||
"another one", | ||
]); | ||
|
||
return ( | ||
<ChipGroup | ||
{...args} | ||
activeChips={storyChips} | ||
setActiveChips={setStoryChips} | ||
/> | ||
); | ||
}; | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = {}; |
Oops, something went wrong.