Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
BrunoQuaresma merged 16 commits intomainfrombq/merge-search-and-search-field
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
d222f72
Replace search by menu search
BrunoQuaresmaJun 25, 2024
050f2c9
Add select menu component
BrunoQuaresmaJun 26, 2024
d4c183b
Add select filter menu
BrunoQuaresmaJun 26, 2024
0706238
Replace filter menu by SelectFilter
BrunoQuaresmaJun 26, 2024
60aa016
Fix search icons
BrunoQuaresmaJun 26, 2024
08b0a82
Fix filter buttons size
BrunoQuaresmaJun 27, 2024
c7e15ec
Improve accessibility and searching
BrunoQuaresmaJun 27, 2024
2e5880f
Fix width of popover and move search to has its own component
BrunoQuaresmaJun 27, 2024
3fae8d4
Update site/src/components/Filter/UserFilter.tsx
BrunoQuaresmaJun 28, 2024
06ae934
Remove important style
BrunoQuaresmaJun 28, 2024
0d83bc8
Apply review comments
BrunoQuaresmaJun 28, 2024
59bd176
Merge branch 'bq/merge-search-and-search-field' of https://github.com…
BrunoQuaresmaJun 28, 2024
928209c
Add auto focus to search
BrunoQuaresmaJun 28, 2024
d344d9c
Fix types
BrunoQuaresmaJul 1, 2024
ebcbaa5
Apply PR suggestions
BrunoQuaresmaJul 2, 2024
50a6ce4
Fix closed story
BrunoQuaresmaJul 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletionssite/src/components/Filter/OptionItem.stories.tsx
View file
Open in desktop

This file was deleted.

146 changes: 146 additions & 0 deletionssite/src/components/Filter/SelectFilter.stories.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff 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");
},
};
116 changes: 116 additions & 0 deletionssite/src/components/Filter/SelectFilter.tsx
View file
Open in desktop
Original file line numberDiff line numberDiff 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;
Loading

[8]ページ先頭

©2009-2025 Movatter.jp