chore(sidebar): move tag layout toggle into a menu
This commit is contained in:
parent
bf1f742321
commit
5ad4aea565
5 changed files with 103 additions and 52 deletions
|
|
@ -1,6 +1,15 @@
|
||||||
import { HashIcon, ListIcon, ListTreeIcon } from "lucide-react";
|
import { HashIcon, ListTreeIcon, MoreHorizontalIcon } from "lucide-react";
|
||||||
import { forwardRef, useMemo } from "react";
|
import { forwardRef, useMemo } from "react";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
import {
|
||||||
|
DropdownMenu,
|
||||||
|
DropdownMenuCheckboxItem,
|
||||||
|
DropdownMenuContent,
|
||||||
|
DropdownMenuGroup,
|
||||||
|
DropdownMenuLabel,
|
||||||
|
DropdownMenuTrigger,
|
||||||
|
} from "@/components/ui/dropdown-menu";
|
||||||
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||||
import { useMemoFilterContext } from "@/contexts/MemoFilterContext";
|
import { useMemoFilterContext } from "@/contexts/MemoFilterContext";
|
||||||
import { useLocalStorage, useOverflowTitle } from "@/hooks";
|
import { useLocalStorage, useOverflowTitle } from "@/hooks";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
|
@ -13,11 +22,7 @@ import {
|
||||||
sidebarRowStateAttributes,
|
sidebarRowStateAttributes,
|
||||||
sidebarRowStateClasses,
|
sidebarRowStateClasses,
|
||||||
} from "./SidebarRow";
|
} from "./SidebarRow";
|
||||||
import SidebarSection, {
|
import SidebarSection, { SIDEBAR_SECTION_ACTION_BUTTON_CLASSES, SIDEBAR_SECTION_ACTION_ICON_CLASSES } from "./SidebarSection";
|
||||||
SIDEBAR_SECTION_ACTION_ACTIVE_CLASSES,
|
|
||||||
SIDEBAR_SECTION_ACTION_BUTTON_CLASSES,
|
|
||||||
SIDEBAR_SECTION_ACTION_ICON_CLASSES,
|
|
||||||
} from "./SidebarSection";
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
tagCount: Record<string, number>;
|
tagCount: Record<string, number>;
|
||||||
|
|
@ -100,28 +105,42 @@ const TagsSection = ({ tagCount, onSelect, scope }: Props) => {
|
||||||
<SidebarSection
|
<SidebarSection
|
||||||
label={t("common.tags")}
|
label={t("common.tags")}
|
||||||
action={
|
action={
|
||||||
<div className="flex items-center gap-0.5" role="group" aria-label={t("common.tags")}>
|
<DropdownMenu>
|
||||||
<Button
|
<Tooltip>
|
||||||
variant="ghost"
|
<TooltipTrigger render={<span className="inline-flex" />}>
|
||||||
size="icon-sm"
|
<DropdownMenuTrigger
|
||||||
aria-label={`${t("common.tags")}: ${t("memo.layout-list")}`}
|
aria-label={`${t("common.tags")}: ${t("common.more")}`}
|
||||||
aria-pressed={!treeMode}
|
render={
|
||||||
className={cn(SIDEBAR_SECTION_ACTION_BUTTON_CLASSES, !treeMode && SIDEBAR_SECTION_ACTION_ACTIVE_CLASSES)}
|
<Button
|
||||||
onClick={() => setTreeMode(false)}
|
variant="ghost"
|
||||||
>
|
size="icon-sm"
|
||||||
<ListIcon className={SIDEBAR_SECTION_ACTION_ICON_CLASSES} strokeWidth={1.8} />
|
className={cn(
|
||||||
</Button>
|
SIDEBAR_SECTION_ACTION_BUTTON_CLASSES,
|
||||||
<Button
|
"data-popup-open:bg-sidebar-accent data-popup-open:text-foreground",
|
||||||
variant="ghost"
|
)}
|
||||||
size="icon-sm"
|
/>
|
||||||
aria-label={`${t("common.tags")}: ${t("common.tree-mode")}`}
|
}
|
||||||
aria-pressed={treeMode}
|
>
|
||||||
className={cn(SIDEBAR_SECTION_ACTION_BUTTON_CLASSES, treeMode && SIDEBAR_SECTION_ACTION_ACTIVE_CLASSES)}
|
<MoreHorizontalIcon className={SIDEBAR_SECTION_ACTION_ICON_CLASSES} strokeWidth={1.8} />
|
||||||
onClick={() => setTreeMode(true)}
|
</DropdownMenuTrigger>
|
||||||
>
|
</TooltipTrigger>
|
||||||
<ListTreeIcon className={SIDEBAR_SECTION_ACTION_ICON_CLASSES} strokeWidth={1.8} />
|
<TooltipContent side="top">{t("common.more")}</TooltipContent>
|
||||||
</Button>
|
</Tooltip>
|
||||||
</div>
|
<DropdownMenuContent align="end" sideOffset={4} size="sm" className="w-44">
|
||||||
|
<DropdownMenuGroup>
|
||||||
|
<DropdownMenuLabel className="text-muted-foreground">{t("common.tags")}</DropdownMenuLabel>
|
||||||
|
<DropdownMenuCheckboxItem
|
||||||
|
checked={treeMode}
|
||||||
|
onCheckedChange={setTreeMode}
|
||||||
|
closeOnClick
|
||||||
|
className="ps-2 pe-7 [&>span]:start-auto [&>span]:end-2"
|
||||||
|
>
|
||||||
|
<ListTreeIcon className="text-muted-foreground" strokeWidth={1.8} />
|
||||||
|
{t("common.tree-mode")}
|
||||||
|
</DropdownMenuCheckboxItem>
|
||||||
|
</DropdownMenuGroup>
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</DropdownMenu>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{treeMode ? (
|
{treeMode ? (
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@
|
||||||
"tags": "Tags",
|
"tags": "Tags",
|
||||||
"title": "Title",
|
"title": "Title",
|
||||||
"today": "Today",
|
"today": "Today",
|
||||||
"tree-mode": "Tree mode",
|
"tree-mode": "Show as tree",
|
||||||
"type": "Type",
|
"type": "Type",
|
||||||
"unpin": "Unpin",
|
"unpin": "Unpin",
|
||||||
"update": "Update",
|
"update": "Update",
|
||||||
|
|
|
||||||
|
|
@ -216,7 +216,7 @@
|
||||||
"tasks": "Tasks",
|
"tasks": "Tasks",
|
||||||
"title": "Title",
|
"title": "Title",
|
||||||
"today": "Today",
|
"today": "Today",
|
||||||
"tree-mode": "Tree mode",
|
"tree-mode": "Show as tree",
|
||||||
"type": "Type",
|
"type": "Type",
|
||||||
"unlink": "Unlink",
|
"unlink": "Unlink",
|
||||||
"unpin": "Unpin",
|
"unpin": "Unpin",
|
||||||
|
|
|
||||||
|
|
@ -147,7 +147,7 @@
|
||||||
"tasks": "任务",
|
"tasks": "任务",
|
||||||
"title": "标题",
|
"title": "标题",
|
||||||
"today": "今天",
|
"today": "今天",
|
||||||
"tree-mode": "树模式",
|
"tree-mode": "树形显示",
|
||||||
"type": "类型",
|
"type": "类型",
|
||||||
"unlink": "解绑",
|
"unlink": "解绑",
|
||||||
"unpin": "取消置顶",
|
"unpin": "取消置顶",
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,8 @@
|
||||||
import { fireEvent, render, screen } from "@testing-library/react";
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||||
import { MemoryRouter } from "react-router-dom";
|
import { MemoryRouter } from "react-router-dom";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { SIDEBAR_ROW_BOX_CLASSES, SIDEBAR_ROW_COUNT_RAIL_CLASSES, SIDEBAR_ROW_SLOT_CLASSES } from "@/components/AppSidebar/SidebarRow";
|
import { SIDEBAR_ROW_BOX_CLASSES, SIDEBAR_ROW_COUNT_RAIL_CLASSES, SIDEBAR_ROW_SLOT_CLASSES } from "@/components/AppSidebar/SidebarRow";
|
||||||
import {
|
import { SIDEBAR_SECTION_ACTION_BUTTON_CLASSES, SIDEBAR_SECTION_ACTION_ICON_CLASSES } from "@/components/AppSidebar/SidebarSection";
|
||||||
SIDEBAR_SECTION_ACTION_ACTIVE_CLASSES,
|
|
||||||
SIDEBAR_SECTION_ACTION_BUTTON_CLASSES,
|
|
||||||
SIDEBAR_SECTION_ACTION_ICON_CLASSES,
|
|
||||||
} from "@/components/AppSidebar/SidebarSection";
|
|
||||||
import TagsSection from "@/components/AppSidebar/TagsSection";
|
import TagsSection from "@/components/AppSidebar/TagsSection";
|
||||||
import { MemoFilterProvider } from "@/contexts/MemoFilterContext";
|
import { MemoFilterProvider } from "@/contexts/MemoFilterContext";
|
||||||
|
|
||||||
|
|
@ -29,22 +25,58 @@ describe("TagsSection", () => {
|
||||||
const heading = screen.getByRole("heading", { name: "common.tags", level: 2 });
|
const heading = screen.getByRole("heading", { name: "common.tags", level: 2 });
|
||||||
expect(heading.parentElement).toHaveTextContent(/^common.tags$/);
|
expect(heading.parentElement).toHaveTextContent(/^common.tags$/);
|
||||||
|
|
||||||
const listButton = screen.getByRole("button", { name: "common.tags: memo.layout-list" });
|
const trigger = screen.getByRole("button", { name: "common.tags: common.more" });
|
||||||
const treeButton = screen.getByRole("button", { name: "common.tags: common.tree-mode" });
|
expect(trigger).toHaveClass(...SIDEBAR_SECTION_ACTION_BUTTON_CLASSES.split(" "));
|
||||||
const stableActionClasses = SIDEBAR_SECTION_ACTION_BUTTON_CLASSES.split(" ").filter(
|
expect(trigger.querySelector("svg")).toHaveClass(SIDEBAR_SECTION_ACTION_ICON_CLASSES);
|
||||||
(className) => className !== "text-muted-foreground/65",
|
expect(screen.queryByRole("button", { name: "common.tags: memo.layout-list" })).not.toBeInTheDocument();
|
||||||
);
|
expect(screen.queryByRole("menuitemcheckbox")).not.toBeInTheDocument();
|
||||||
for (const button of [listButton, treeButton]) {
|
});
|
||||||
expect(button).toHaveClass(...stableActionClasses);
|
|
||||||
expect(button.querySelector("svg")).toHaveClass(SIDEBAR_SECTION_ACTION_ICON_CLASSES);
|
|
||||||
}
|
|
||||||
expect(listButton).toHaveClass(...SIDEBAR_SECTION_ACTION_ACTIVE_CLASSES.split(" "));
|
|
||||||
expect(treeButton).toHaveClass("text-muted-foreground/65");
|
|
||||||
|
|
||||||
fireEvent.click(treeButton);
|
it("switches layouts from the menu, closes it, and persists the preference", async () => {
|
||||||
expect(treeButton).toHaveClass(...SIDEBAR_SECTION_ACTION_ACTIVE_CLASSES.split(" "));
|
const onSelect = vi.fn();
|
||||||
expect(listButton).not.toHaveClass(...SIDEBAR_SECTION_ACTION_ACTIVE_CLASSES.split(" "));
|
render(
|
||||||
expect(listButton).toHaveClass("text-muted-foreground/65");
|
<MemoryRouter>
|
||||||
|
<MemoFilterProvider>
|
||||||
|
<TagsSection tagCount={{ a: 2, "a/b": 1 }} scope="home" onSelect={onSelect} />
|
||||||
|
</MemoFilterProvider>
|
||||||
|
</MemoryRouter>,
|
||||||
|
);
|
||||||
|
|
||||||
|
const trigger = screen.getByRole("button", { name: "common.tags: common.more" });
|
||||||
|
fireEvent.click(trigger);
|
||||||
|
const treeMode = await screen.findByRole("menuitemcheckbox", { name: "common.tree-mode" });
|
||||||
|
expect(treeMode).toHaveAttribute("aria-checked", "false");
|
||||||
|
fireEvent.click(treeMode);
|
||||||
|
|
||||||
|
expect(screen.getByRole("tree")).toBeInTheDocument();
|
||||||
|
expect(localStorage.getItem("tag-view-as-tree")).toBe("true");
|
||||||
|
await waitFor(() => expect(screen.queryByRole("menuitemcheckbox")).not.toBeInTheDocument());
|
||||||
|
await waitFor(() => expect(trigger).toHaveFocus());
|
||||||
|
|
||||||
|
fireEvent.click(trigger);
|
||||||
|
const checkedTreeMode = await screen.findByRole("menuitemcheckbox", { name: "common.tree-mode" });
|
||||||
|
expect(checkedTreeMode).toHaveAttribute("aria-checked", "true");
|
||||||
|
fireEvent.click(checkedTreeMode);
|
||||||
|
|
||||||
|
expect(screen.queryByRole("tree")).not.toBeInTheDocument();
|
||||||
|
expect(screen.getByRole("button", { name: "#a/b, setting.tags.used-count" })).toBeInTheDocument();
|
||||||
|
expect(localStorage.getItem("tag-view-as-tree")).toBe("false");
|
||||||
|
expect(onSelect).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reflects an existing tree preference when opening the menu", async () => {
|
||||||
|
localStorage.setItem("tag-view-as-tree", "true");
|
||||||
|
render(
|
||||||
|
<MemoryRouter>
|
||||||
|
<MemoFilterProvider>
|
||||||
|
<TagsSection tagCount={{ a: 2, "a/b": 1 }} scope="home" />
|
||||||
|
</MemoFilterProvider>
|
||||||
|
</MemoryRouter>,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(screen.getByRole("tree")).toBeInTheDocument();
|
||||||
|
fireEvent.click(screen.getByRole("button", { name: "common.tags: common.more" }));
|
||||||
|
expect(await screen.findByRole("menuitemcheckbox", { name: "common.tree-mode" })).toHaveAttribute("aria-checked", "true");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps flat rows on the shared row grammar with a trailing count rail", () => {
|
it("keeps flat rows on the shared row grammar with a trailing count rail", () => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue