- Notifications
You must be signed in to change notification settings - Fork1k
chore(site): refactor filter component to be more extendable#13688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
16 commits Select commitHold shift + click to select a range
d222f72
Replace search by menu search
BrunoQuaresma050f2c9
Add select menu component
BrunoQuaresmad4c183b
Add select filter menu
BrunoQuaresma0706238
Replace filter menu by SelectFilter
BrunoQuaresma60aa016
Fix search icons
BrunoQuaresma08b0a82
Fix filter buttons size
BrunoQuaresmac7e15ec
Improve accessibility and searching
BrunoQuaresma2e5880f
Fix width of popover and move search to has its own component
BrunoQuaresma3fae8d4
Update site/src/components/Filter/UserFilter.tsx
BrunoQuaresma06ae934
Remove important style
BrunoQuaresma0d83bc8
Apply review comments
BrunoQuaresma59bd176
Merge branch 'bq/merge-search-and-search-field' of https://github.com…
BrunoQuaresma928209c
Add auto focus to search
BrunoQuaresmad344d9c
Fix types
BrunoQuaresmaebcbaa5
Apply PR suggestions
BrunoQuaresma50a6ce4
Fix closed story
BrunoQuaresmaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
This file contains hidden or 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,146 @@ | ||
import { action } from "@storybook/addon-actions"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { userEvent, within, expect } from "@storybook/test"; | ||
import { useState } from "react"; | ||
import { UserAvatar } from "components/UserAvatar/UserAvatar"; | ||
import { withDesktopViewport } from "testHelpers/storybook"; | ||
import { | ||
SelectFilter, | ||
SelectFilterSearch, | ||
type SelectFilterOption, | ||
} from "./SelectFilter"; | ||
const options: SelectFilterOption[] = Array.from({ length: 50 }, (_, i) => ({ | ||
startIcon: <UserAvatar username={`username ${i + 1}`} size="xs" />, | ||
label: `Option ${i + 1}`, | ||
value: `option-${i + 1}`, | ||
})); | ||
const meta: Meta<typeof SelectFilter> = { | ||
title: "components/SelectFilter", | ||
component: SelectFilter, | ||
args: { | ||
options, | ||
placeholder: "All options", | ||
}, | ||
decorators: [withDesktopViewport], | ||
render: function SelectFilterWithState(args) { | ||
const [selectedOption, setSelectedOption] = useState< | ||
SelectFilterOption | undefined | ||
>(args.selectedOption); | ||
return ( | ||
<SelectFilter | ||
{...args} | ||
selectedOption={selectedOption} | ||
onSelect={setSelectedOption} | ||
/> | ||
); | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const button = canvas.getByRole("button"); | ||
await userEvent.click(button); | ||
}, | ||
}; | ||
export default meta; | ||
type Story = StoryObj<typeof SelectFilter>; | ||
export const Closed: Story = { | ||
play: () => {}, | ||
}; | ||
export const Open: Story = {}; | ||
export const Selected: Story = { | ||
args: { | ||
selectedOption: options[25], | ||
}, | ||
}; | ||
export const WithSearch: Story = { | ||
args: { | ||
selectedOption: options[25], | ||
selectFilterSearch: ( | ||
<SelectFilterSearch | ||
value="" | ||
onChange={action("onSearch")} | ||
placeholder="Search options..." | ||
/> | ||
), | ||
}, | ||
}; | ||
export const LoadingOptions: Story = { | ||
args: { | ||
options: undefined, | ||
}, | ||
}; | ||
export const NoOptionsFound: Story = { | ||
args: { | ||
options: [], | ||
}, | ||
}; | ||
export const SelectingOption: Story = { | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const button = canvas.getByRole("button"); | ||
await userEvent.click(button); | ||
const option = canvas.getByText("Option 25"); | ||
await userEvent.click(option); | ||
await expect(button).toHaveTextContent("Option 25"); | ||
}, | ||
}; | ||
export const UnselectingOption: Story = { | ||
args: { | ||
selectedOption: options[25], | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const button = canvas.getByRole("button"); | ||
await userEvent.click(button); | ||
const menu = canvasElement.querySelector<HTMLElement>("[role=menu]")!; | ||
const option = within(menu).getByText("Option 26"); | ||
await userEvent.click(option); | ||
await expect(button).toHaveTextContent("All options"); | ||
}, | ||
}; | ||
export const SearchingOption: Story = { | ||
render: function SelectFilterWithSearch(args) { | ||
const [selectedOption, setSelectedOption] = useState< | ||
SelectFilterOption | undefined | ||
>(args.selectedOption); | ||
const [search, setSearch] = useState(""); | ||
const visibleOptions = options.filter((option) => | ||
option.value.includes(search), | ||
); | ||
return ( | ||
<SelectFilter | ||
{...args} | ||
selectedOption={selectedOption} | ||
onSelect={setSelectedOption} | ||
options={visibleOptions} | ||
selectFilterSearch={ | ||
<SelectFilterSearch | ||
value={search} | ||
onChange={setSearch} | ||
placeholder="Search options..." | ||
inputProps={{ "aria-label": "Search options" }} | ||
/> | ||
} | ||
/> | ||
); | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const button = canvas.getByRole("button"); | ||
await userEvent.click(button); | ||
const search = canvas.getByLabelText("Search options"); | ||
await userEvent.type(search, "option-2"); | ||
}, | ||
}; |
This file contains hidden or 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,116 @@ | ||
import { useState, type FC, type ReactNode } from "react"; | ||
import { Loader } from "components/Loader/Loader"; | ||
import { | ||
SelectMenu, | ||
SelectMenuTrigger, | ||
SelectMenuButton, | ||
SelectMenuContent, | ||
SelectMenuSearch, | ||
SelectMenuList, | ||
SelectMenuItem, | ||
SelectMenuIcon, | ||
} from "components/SelectMenu/SelectMenu"; | ||
const BASE_WIDTH = 200; | ||
const POPOVER_WIDTH = 320; | ||
export type SelectFilterOption = { | ||
startIcon?: ReactNode; | ||
label: string; | ||
value: string; | ||
}; | ||
export type SelectFilterProps = { | ||
options: SelectFilterOption[] | undefined; | ||
selectedOption?: SelectFilterOption; | ||
// Used to add a accessibility label to the select | ||
label: string; | ||
// Used when there is no option selected | ||
placeholder: string; | ||
// Used to customize the empty state message | ||
emptyText?: string; | ||
onSelect: (option: SelectFilterOption | undefined) => void; | ||
// SelectFilterSearch element | ||
selectFilterSearch?: ReactNode; | ||
}; | ||
export const SelectFilter: FC<SelectFilterProps> = ({ | ||
label, | ||
options, | ||
selectedOption, | ||
onSelect, | ||
placeholder, | ||
emptyText, | ||
selectFilterSearch, | ||
}) => { | ||
const [open, setOpen] = useState(false); | ||
return ( | ||
<SelectMenu open={open} onOpenChange={setOpen}> | ||
<SelectMenuTrigger> | ||
<SelectMenuButton | ||
startIcon={selectedOption?.startIcon} | ||
css={{ width: BASE_WIDTH }} | ||
aria-label={label} | ||
> | ||
{selectedOption?.label ?? placeholder} | ||
</SelectMenuButton> | ||
</SelectMenuTrigger> | ||
<SelectMenuContent | ||
horizontal="right" | ||
css={{ | ||
"& .MuiPaper-root": { | ||
// When including selectFilterSearch, we aim for the width to be as | ||
// wide as possible. | ||
width: selectFilterSearch ? "100%" : undefined, | ||
maxWidth: POPOVER_WIDTH, | ||
minWidth: BASE_WIDTH, | ||
}, | ||
}} | ||
> | ||
{selectFilterSearch} | ||
{options ? ( | ||
options.length > 0 ? ( | ||
<SelectMenuList> | ||
{options.map((o) => { | ||
const isSelected = o.value === selectedOption?.value; | ||
return ( | ||
<SelectMenuItem | ||
key={o.value} | ||
selected={isSelected} | ||
onClick={() => { | ||
setOpen(false); | ||
onSelect(isSelected ? undefined : o); | ||
}} | ||
> | ||
{o.startIcon && ( | ||
<SelectMenuIcon>{o.startIcon}</SelectMenuIcon> | ||
)} | ||
{o.label} | ||
</SelectMenuItem> | ||
); | ||
})} | ||
</SelectMenuList> | ||
) : ( | ||
<div | ||
css={(theme) => ({ | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
padding: 32, | ||
color: theme.palette.text.secondary, | ||
lineHeight: 1, | ||
})} | ||
> | ||
{emptyText || "No options found"} | ||
</div> | ||
) | ||
) : ( | ||
<Loader size={16} /> | ||
)} | ||
</SelectMenuContent> | ||
</SelectMenu> | ||
); | ||
}; | ||
export const SelectFilterSearch = SelectMenuSearch; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.