-
Notifications
You must be signed in to change notification settings - Fork 899
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d222f72
Replace search by menu search
BrunoQuaresma 050f2c9
Add select menu component
BrunoQuaresma d4c183b
Add select filter menu
BrunoQuaresma 0706238
Replace filter menu by SelectFilter
BrunoQuaresma 60aa016
Fix search icons
BrunoQuaresma 08b0a82
Fix filter buttons size
BrunoQuaresma c7e15ec
Improve accessibility and searching
BrunoQuaresma 2e5880f
Fix width of popover and move search to has its own component
BrunoQuaresma 3fae8d4
Update site/src/components/Filter/UserFilter.tsx
BrunoQuaresma 06ae934
Remove important style
BrunoQuaresma 0d83bc8
Apply review comments
BrunoQuaresma 59bd176
Merge branch 'bq/merge-search-and-search-field' of https://github.com…
BrunoQuaresma 928209c
Add auto focus to search
BrunoQuaresma d344d9c
Fix types
BrunoQuaresma ebcbaa5
Apply PR suggestions
BrunoQuaresma 50a6ce4
Fix closed story
BrunoQuaresma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add select menu component
- Loading branch information
commit 050f2c9fb9d9d84aa3a04811d515672c6ed039ea
There are no files selected for viewing
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
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
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,66 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { userEvent, within } from "@storybook/test"; | ||
import { UserAvatar } from "components/UserAvatar/UserAvatar"; | ||
import { withDesktopViewport } from "testHelpers/storybook"; | ||
import { | ||
SelectMenu, | ||
SelectMenuButton, | ||
SelectMenuContent, | ||
SelectMenuIcon, | ||
SelectMenuItem, | ||
SelectMenuList, | ||
SelectMenuSearch, | ||
SelectMenuTrigger, | ||
} from "./SelectMenu"; | ||
|
||
const meta: Meta<typeof SelectMenu> = { | ||
title: "components/SelectMenu", | ||
component: SelectMenu, | ||
render: function SelectMenuRender() { | ||
const opts = options(50); | ||
const selectedOpt = opts[20]; | ||
|
||
return ( | ||
<SelectMenu> | ||
<SelectMenuTrigger> | ||
<SelectMenuButton | ||
startIcon={<UserAvatar size="xs" username={selectedOpt} />} | ||
> | ||
{selectedOpt} | ||
</SelectMenuButton> | ||
</SelectMenuTrigger> | ||
<SelectMenuContent> | ||
<SelectMenuSearch onChange={() => {}} /> | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<SelectMenuList> | ||
{opts.map((o) => ( | ||
<SelectMenuItem key={o} selected={o === selectedOpt}> | ||
<SelectMenuIcon> | ||
<UserAvatar size="xs" username={o} /> | ||
</SelectMenuIcon> | ||
{o} | ||
</SelectMenuItem> | ||
))} | ||
</SelectMenuList> | ||
</SelectMenuContent> | ||
</SelectMenu> | ||
); | ||
}, | ||
decorators: [withDesktopViewport], | ||
}; | ||
|
||
function options(n: number): string[] { | ||
return Array.from({ length: n }, (_, i) => `Item ${i + 1}`); | ||
} | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof SelectMenu>; | ||
|
||
export const Closed: Story = {}; | ||
|
||
export const Open: Story = { | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
const button = canvas.getByRole("button"); | ||
await userEvent.click(button); | ||
}, | ||
}; |
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,118 @@ | ||
import CheckOutlined from "@mui/icons-material/CheckOutlined"; | ||
import Button, { type ButtonProps } from "@mui/material/Button"; | ||
import MenuItem, { type MenuItemProps } from "@mui/material/MenuItem"; | ||
import MenuList, { type MenuListProps } from "@mui/material/MenuList"; | ||
import { | ||
type FC, | ||
forwardRef, | ||
Children, | ||
isValidElement, | ||
type HTMLProps, | ||
} from "react"; | ||
import { DropdownArrow } from "components/DropdownArrow/DropdownArrow"; | ||
import { | ||
Popover, | ||
PopoverContent, | ||
PopoverTrigger, | ||
} from "components/Popover/Popover"; | ||
import { | ||
SearchField, | ||
type SearchFieldProps, | ||
} from "components/SearchField/SearchField"; | ||
|
||
const SIDE_PADDING = 16; | ||
|
||
export const SelectMenu = Popover; | ||
|
||
export const SelectMenuTrigger = PopoverTrigger; | ||
|
||
export const SelectMenuContent = PopoverContent; | ||
|
||
export const SelectMenuButton = forwardRef<HTMLButtonElement, ButtonProps>( | ||
(props, ref) => { | ||
return ( | ||
<Button | ||
css={{ | ||
"& .MuiButton-startIcon": { | ||
marginLeft: 0, | ||
marginRight: SIDE_PADDING, | ||
}, | ||
}} | ||
endIcon={<DropdownArrow />} | ||
ref={ref} | ||
{...props} | ||
/> | ||
); | ||
}, | ||
); | ||
|
||
export const SelectMenuSearch: FC<SearchFieldProps> = (props) => { | ||
return ( | ||
<SearchField | ||
fullWidth | ||
size="medium" | ||
css={(theme) => ({ | ||
"& fieldset": { | ||
border: 0, | ||
borderRadius: 0, | ||
borderBottom: `1px solid ${theme.palette.divider} !important`, | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"& .MuiInputBase-root": { | ||
padding: `12px ${SIDE_PADDING}px`, | ||
}, | ||
"& .MuiInputAdornment-positionStart": { | ||
marginRight: SIDE_PADDING, | ||
}, | ||
})} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export const SelectMenuList: FC<MenuListProps> = (props) => { | ||
const items = Children.toArray(props.children); | ||
type ItemType = (typeof items)[number]; | ||
const selectedAsFirst = (a: ItemType, b: ItemType) => { | ||
if ( | ||
!isValidElement<MenuItemProps>(a) || | ||
!isValidElement<MenuItemProps>(b) | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
throw new Error( | ||
"SelectMenuList children must be SelectMenuItem components", | ||
); | ||
} | ||
return a.props.selected ? -1 : 0; | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
items.sort(selectedAsFirst); | ||
return ( | ||
<MenuList css={{ maxHeight: 480 }} {...props}> | ||
{items} | ||
</MenuList> | ||
); | ||
}; | ||
|
||
export const SelectMenuIcon: FC<HTMLProps<HTMLDivElement>> = (props) => { | ||
return <div css={{ marginRight: 16 }} {...props} />; | ||
}; | ||
|
||
export const SelectMenuItem: FC<MenuItemProps> = (props) => { | ||
return ( | ||
<MenuItem | ||
css={{ | ||
fontSize: 14, | ||
gap: 0, | ||
lineHeight: 1, | ||
padding: `12px ${SIDE_PADDING}px`, | ||
}} | ||
{...props} | ||
> | ||
{props.children} | ||
{props.selected && ( | ||
<CheckOutlined | ||
// TODO: Don't set the menu icon font size on default theme | ||
css={{ marginLeft: "auto", fontSize: "inherit !important" }} | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/> | ||
)} | ||
</MenuItem> | ||
); | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.