@fldr/ui 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ai/component-registry.json +962 -43
- package/ai/component-registry.md +1 -1
- package/dist/.build-complete +1 -1
- package/dist/ai/schemas.d.ts +18 -18
- package/dist/blocks-source/activity-feed/activity-feed.test.tsx +75 -0
- package/dist/blocks-source/activity-feed/activity-feed.tsx +256 -0
- package/dist/blocks-source/app-shell/app-shell.test.tsx +74 -0
- package/dist/blocks-source/app-shell/app-shell.tsx +123 -0
- package/dist/blocks-source/auth-card/auth-card.test.tsx +64 -0
- package/dist/blocks-source/auth-card/auth-card.tsx +234 -0
- package/dist/blocks-source/dashboard-page/dashboard-page.tsx +167 -0
- package/dist/blocks-source/error-page/error-page.test.tsx +60 -0
- package/dist/blocks-source/error-page/error-page.tsx +97 -0
- package/dist/blocks-source/file-table/file-item.tsx +164 -0
- package/dist/blocks-source/file-table/file-table.test.tsx +82 -0
- package/dist/blocks-source/file-table/file-table.tsx +292 -0
- package/dist/blocks-source/folder-grid/folder-grid.test.tsx +54 -0
- package/dist/blocks-source/folder-grid/folder-grid.tsx +170 -0
- package/dist/blocks-source/login-page/login-page.tsx +47 -0
- package/dist/blocks-source/onboarding-checklist/onboarding-checklist.test.tsx +50 -0
- package/dist/blocks-source/onboarding-checklist/onboarding-checklist.tsx +180 -0
- package/dist/blocks-source/settings-section/settings-section.test.tsx +43 -0
- package/dist/blocks-source/settings-section/settings-section.tsx +165 -0
- package/dist/blocks-source/share-dialog/share-dialog.test.tsx +69 -0
- package/dist/blocks-source/share-dialog/share-dialog.tsx +307 -0
- package/dist/blocks-source/stat-grid/stat-grid.test.tsx +52 -0
- package/dist/blocks-source/stat-grid/stat-grid.tsx +153 -0
- package/dist/blocks-source/upload-dropzone/upload-dropzone.test.tsx +82 -0
- package/dist/blocks-source/upload-dropzone/upload-dropzone.tsx +264 -0
- package/dist/blocks.d.ts +780 -0
- package/dist/blocks.js +1664 -0
- package/dist/chunks/delete-resource-cmlptb61ih8s84r3.js +151 -0
- package/dist/delete-resource-DY-GFy2s.d.ts +48 -0
- package/dist/index.d.ts +4 -50
- package/dist/index.js +1 -146
- package/dist/primitives.d.ts +1 -1
- package/dist/styles/kumo-standalone.css +1 -1
- package/package.json +5 -1
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { LinkButton } from "../../components/button";
|
|
3
|
+
import { cn } from "../../utils/cn";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_COPY: Record<number, { title: string; description: string }> = {
|
|
6
|
+
403: {
|
|
7
|
+
title: "You don't have access",
|
|
8
|
+
description: "Ask the owner to share it with you, or switch accounts.",
|
|
9
|
+
},
|
|
10
|
+
404: {
|
|
11
|
+
title: "Page not found",
|
|
12
|
+
description: "This page doesn't exist or was moved.",
|
|
13
|
+
},
|
|
14
|
+
500: {
|
|
15
|
+
title: "Something went wrong",
|
|
16
|
+
description: "We couldn't load this page. Try again in a moment.",
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export interface ErrorPageProps {
|
|
21
|
+
/** HTTP-style status. 403, 404 and 500 come with default copy. @default 404 */
|
|
22
|
+
status?: number;
|
|
23
|
+
title?: string;
|
|
24
|
+
description?: ReactNode;
|
|
25
|
+
/** Where the primary button goes. Pass `null` to hide it. @default "/" */
|
|
26
|
+
homeHref?: string | null;
|
|
27
|
+
/** @default "Go home" */
|
|
28
|
+
homeLabel?: string;
|
|
29
|
+
/** Extra actions after the home button, e.g. "Contact support". */
|
|
30
|
+
actions?: ReactNode;
|
|
31
|
+
/** Shown above the status, e.g. the product mark. */
|
|
32
|
+
logo?: ReactNode;
|
|
33
|
+
/** Small print under the actions, e.g. a request ID. */
|
|
34
|
+
details?: ReactNode;
|
|
35
|
+
/** Fill the viewport. Set false to render inside a layout. @default true */
|
|
36
|
+
fullScreen?: boolean;
|
|
37
|
+
className?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* ErrorPage: a quiet 404 / 403 / 500 page with a way back.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* <ErrorPage status={404} homeHref="/files" homeLabel="Back to files" />
|
|
46
|
+
* <ErrorPage status={500} details="Request ID 7f3a…" actions={<Button onClick={retry}>Retry</Button>} />
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function ErrorPage({
|
|
50
|
+
status = 404,
|
|
51
|
+
title,
|
|
52
|
+
description,
|
|
53
|
+
homeHref = "/",
|
|
54
|
+
homeLabel = "Go home",
|
|
55
|
+
actions,
|
|
56
|
+
logo,
|
|
57
|
+
details,
|
|
58
|
+
fullScreen = true,
|
|
59
|
+
className,
|
|
60
|
+
}: ErrorPageProps) {
|
|
61
|
+
const copy = DEFAULT_COPY[status] ?? DEFAULT_COPY[500];
|
|
62
|
+
return (
|
|
63
|
+
<main
|
|
64
|
+
className={cn(
|
|
65
|
+
"flex w-full items-center justify-center bg-kumo-canvas px-4 py-12 text-kumo-default",
|
|
66
|
+
fullScreen && "min-h-dvh",
|
|
67
|
+
className,
|
|
68
|
+
)}
|
|
69
|
+
>
|
|
70
|
+
<div className="flex w-full max-w-sm flex-col items-start gap-3">
|
|
71
|
+
{logo}
|
|
72
|
+
<span className="font-mono text-sm text-kumo-subtle tabular-nums">
|
|
73
|
+
{status}
|
|
74
|
+
</span>
|
|
75
|
+
<h1 className="m-0 text-2xl font-semibold text-kumo-strong">
|
|
76
|
+
{title ?? copy.title}
|
|
77
|
+
</h1>
|
|
78
|
+
<p className="m-0 text-base text-kumo-subtle">
|
|
79
|
+
{description ?? copy.description}
|
|
80
|
+
</p>
|
|
81
|
+
{(homeHref !== null || actions) && (
|
|
82
|
+
<div className="mt-2 flex flex-wrap items-center gap-2">
|
|
83
|
+
{homeHref !== null && (
|
|
84
|
+
<LinkButton href={homeHref} variant="primary">
|
|
85
|
+
{homeLabel}
|
|
86
|
+
</LinkButton>
|
|
87
|
+
)}
|
|
88
|
+
{actions}
|
|
89
|
+
</div>
|
|
90
|
+
)}
|
|
91
|
+
{details && (
|
|
92
|
+
<p className="m-0 mt-2 text-xs text-kumo-subtle">{details}</p>
|
|
93
|
+
)}
|
|
94
|
+
</div>
|
|
95
|
+
</main>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { useCallback, useState, type ReactNode } from "react";
|
|
2
|
+
import {
|
|
3
|
+
DotsThreeIcon,
|
|
4
|
+
FileIcon as GenericFileIcon,
|
|
5
|
+
FilePdfIcon,
|
|
6
|
+
FolderSimpleIcon,
|
|
7
|
+
FileImageIcon,
|
|
8
|
+
FileXlsIcon,
|
|
9
|
+
type Icon,
|
|
10
|
+
} from "@phosphor-icons/react";
|
|
11
|
+
import { Button } from "../../components/button";
|
|
12
|
+
import { DropdownMenu } from "../../components/dropdown";
|
|
13
|
+
import { cn } from "../../utils/cn";
|
|
14
|
+
|
|
15
|
+
/** Kind of item, used to pick the type icon. */
|
|
16
|
+
export type FileKind = "folder" | "pdf" | "image" | "sheet" | "file";
|
|
17
|
+
|
|
18
|
+
/** Minimal shape shared by `FileTable` and `FolderGrid`. Extend it with your own fields. */
|
|
19
|
+
export interface FileItem {
|
|
20
|
+
id: string;
|
|
21
|
+
name: string;
|
|
22
|
+
kind: FileKind;
|
|
23
|
+
/** Size in bytes. */
|
|
24
|
+
size?: number;
|
|
25
|
+
modifiedAt?: Date | string | number;
|
|
26
|
+
owner?: string;
|
|
27
|
+
/** Number of children, for folders. */
|
|
28
|
+
itemCount?: number;
|
|
29
|
+
/** Image shown instead of the type icon in `FolderGrid`. */
|
|
30
|
+
thumbnailUrl?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** One entry in an item's actions menu. */
|
|
34
|
+
export interface FileAction<T extends FileItem = FileItem> {
|
|
35
|
+
label: ReactNode;
|
|
36
|
+
icon?: Icon | ReactNode;
|
|
37
|
+
variant?: "default" | "danger";
|
|
38
|
+
onSelect: (item: T) => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Actions menu content: an items array, or a render prop returning `DropdownMenu.Item`s. */
|
|
42
|
+
export type FileActions<T extends FileItem = FileItem> =
|
|
43
|
+
| FileAction<T>[]
|
|
44
|
+
| ((item: T) => ReactNode);
|
|
45
|
+
|
|
46
|
+
const KIND_ICONS: Record<FileKind, Icon> = {
|
|
47
|
+
folder: FolderSimpleIcon,
|
|
48
|
+
pdf: FilePdfIcon,
|
|
49
|
+
image: FileImageIcon,
|
|
50
|
+
sheet: FileXlsIcon,
|
|
51
|
+
file: GenericFileIcon,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Phosphor icon for a file kind. */
|
|
55
|
+
export function FileKindIcon({
|
|
56
|
+
kind,
|
|
57
|
+
size = 18,
|
|
58
|
+
className,
|
|
59
|
+
}: {
|
|
60
|
+
kind: FileKind;
|
|
61
|
+
size?: number;
|
|
62
|
+
className?: string;
|
|
63
|
+
}) {
|
|
64
|
+
const IconComponent = KIND_ICONS[kind] ?? GenericFileIcon;
|
|
65
|
+
return (
|
|
66
|
+
<IconComponent
|
|
67
|
+
size={size}
|
|
68
|
+
weight={kind === "folder" ? "fill" : "regular"}
|
|
69
|
+
className={cn("shrink-0 text-kumo-subtle", className)}
|
|
70
|
+
aria-hidden
|
|
71
|
+
/>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function formatFileSize(bytes: number, locale?: string) {
|
|
76
|
+
const units = ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte"];
|
|
77
|
+
const i = Math.min(
|
|
78
|
+
units.length - 1,
|
|
79
|
+
bytes > 0 ? Math.floor(Math.log(bytes) / Math.log(1024)) : 0,
|
|
80
|
+
);
|
|
81
|
+
return new Intl.NumberFormat(locale, {
|
|
82
|
+
style: "unit",
|
|
83
|
+
unit: units[i],
|
|
84
|
+
unitDisplay: "short",
|
|
85
|
+
maximumFractionDigits: i === 0 ? 0 : 1,
|
|
86
|
+
}).format(bytes / 1024 ** i);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function formatFileDate(value: Date | string | number, locale?: string) {
|
|
90
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: "medium" }).format(
|
|
91
|
+
new Date(value),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function toTime(value: FileItem["modifiedAt"]) {
|
|
96
|
+
return value === undefined ? 0 : new Date(value).getTime();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Controlled/uncontrolled state helper. */
|
|
100
|
+
export function useControllable<V>(
|
|
101
|
+
value: V | undefined,
|
|
102
|
+
defaultValue: V,
|
|
103
|
+
onChange?: (next: V) => void,
|
|
104
|
+
) {
|
|
105
|
+
const [inner, setInner] = useState(defaultValue);
|
|
106
|
+
const current = value ?? inner;
|
|
107
|
+
const set = useCallback(
|
|
108
|
+
(next: V) => {
|
|
109
|
+
if (value === undefined) setInner(next);
|
|
110
|
+
onChange?.(next);
|
|
111
|
+
},
|
|
112
|
+
[value, onChange],
|
|
113
|
+
);
|
|
114
|
+
return [current, set] as const;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Kebab button that opens an item's actions menu. Clicks don't bubble to the row/card. */
|
|
118
|
+
export function ItemActionsMenu<T extends FileItem>({
|
|
119
|
+
item,
|
|
120
|
+
actions,
|
|
121
|
+
label,
|
|
122
|
+
className,
|
|
123
|
+
}: {
|
|
124
|
+
item: T;
|
|
125
|
+
actions: FileActions<T>;
|
|
126
|
+
label: string;
|
|
127
|
+
className?: string;
|
|
128
|
+
}) {
|
|
129
|
+
return (
|
|
130
|
+
<span
|
|
131
|
+
className={cn("inline-flex", className)}
|
|
132
|
+
onClick={(e) => e.stopPropagation()}
|
|
133
|
+
onKeyDown={(e) => e.stopPropagation()}
|
|
134
|
+
>
|
|
135
|
+
<DropdownMenu>
|
|
136
|
+
<DropdownMenu.Trigger
|
|
137
|
+
render={
|
|
138
|
+
<Button
|
|
139
|
+
variant="ghost"
|
|
140
|
+
size="sm"
|
|
141
|
+
shape="square"
|
|
142
|
+
icon={<DotsThreeIcon weight="bold" />}
|
|
143
|
+
aria-label={label}
|
|
144
|
+
/>
|
|
145
|
+
}
|
|
146
|
+
/>
|
|
147
|
+
<DropdownMenu.Content align="end">
|
|
148
|
+
{typeof actions === "function"
|
|
149
|
+
? actions(item)
|
|
150
|
+
: actions.map((action, i) => (
|
|
151
|
+
<DropdownMenu.Item
|
|
152
|
+
key={i}
|
|
153
|
+
icon={action.icon}
|
|
154
|
+
variant={action.variant}
|
|
155
|
+
onClick={() => action.onSelect(item)}
|
|
156
|
+
>
|
|
157
|
+
{action.label}
|
|
158
|
+
</DropdownMenu.Item>
|
|
159
|
+
))}
|
|
160
|
+
</DropdownMenu.Content>
|
|
161
|
+
</DropdownMenu>
|
|
162
|
+
</span>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it, vi } from "vite-plus/test";
|
|
3
|
+
import { FileTable, type FileItem } from "./file-table";
|
|
4
|
+
|
|
5
|
+
const items: FileItem[] = [
|
|
6
|
+
{ id: "a", name: "Tax return 2025.pdf", kind: "pdf", size: 2_400_000 },
|
|
7
|
+
{ id: "b", name: "Receipts", kind: "folder", itemCount: 48 },
|
|
8
|
+
{ id: "c", name: "Budget.xlsx", kind: "sheet", size: 18_000 },
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const rowNames = () =>
|
|
12
|
+
screen
|
|
13
|
+
.getAllByRole("row")
|
|
14
|
+
.slice(1)
|
|
15
|
+
.map((row) => row.querySelector("td:nth-child(2)")?.textContent);
|
|
16
|
+
|
|
17
|
+
describe("FileTable", () => {
|
|
18
|
+
it("renders folders first, then files sorted by name, with formatted sizes", () => {
|
|
19
|
+
render(<FileTable items={items} locale="en-US" />);
|
|
20
|
+
expect(rowNames()).toEqual([
|
|
21
|
+
"Receipts",
|
|
22
|
+
"Budget.xlsx",
|
|
23
|
+
"Tax return 2025.pdf",
|
|
24
|
+
]);
|
|
25
|
+
expect(screen.getByText("2.3 MB")).toBeDefined();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("sorts by size when the header is clicked and toggles direction", () => {
|
|
29
|
+
const onSortChange = vi.fn();
|
|
30
|
+
render(<FileTable items={items} onSortChange={onSortChange} />);
|
|
31
|
+
fireEvent.click(screen.getByRole("button", { name: "Size" }));
|
|
32
|
+
expect(onSortChange).toHaveBeenLastCalledWith({
|
|
33
|
+
key: "size",
|
|
34
|
+
direction: "asc",
|
|
35
|
+
});
|
|
36
|
+
expect(rowNames()).toEqual([
|
|
37
|
+
"Receipts",
|
|
38
|
+
"Budget.xlsx",
|
|
39
|
+
"Tax return 2025.pdf",
|
|
40
|
+
]);
|
|
41
|
+
fireEvent.click(screen.getByRole("button", { name: "Size" }));
|
|
42
|
+
expect(rowNames()).toEqual([
|
|
43
|
+
"Receipts",
|
|
44
|
+
"Tax return 2025.pdf",
|
|
45
|
+
"Budget.xlsx",
|
|
46
|
+
]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("shows the bulk bar when rows are selected and clears it", () => {
|
|
50
|
+
const onSelectionChange = vi.fn();
|
|
51
|
+
render(
|
|
52
|
+
<FileTable
|
|
53
|
+
items={items}
|
|
54
|
+
onSelectionChange={onSelectionChange}
|
|
55
|
+
bulkActions={(ids) => <button type="button">Share {ids.length}</button>}
|
|
56
|
+
/>,
|
|
57
|
+
);
|
|
58
|
+
expect(screen.queryByRole("toolbar")).toBeNull();
|
|
59
|
+
fireEvent.click(screen.getByRole("checkbox", { name: "Select Receipts" }));
|
|
60
|
+
expect(screen.getByText("1 selected")).toBeDefined();
|
|
61
|
+
expect(screen.getByText("Share 1")).toBeDefined();
|
|
62
|
+
fireEvent.click(screen.getByRole("checkbox", { name: "Select all" }));
|
|
63
|
+
expect(onSelectionChange).toHaveBeenLastCalledWith(["a", "b", "c"]);
|
|
64
|
+
fireEvent.click(screen.getByRole("button", { name: "Clear selection" }));
|
|
65
|
+
expect(screen.queryByRole("toolbar")).toBeNull();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("opens rows on click but not when toggling the checkbox", () => {
|
|
69
|
+
const onOpen = vi.fn();
|
|
70
|
+
render(<FileTable items={items} onOpen={onOpen} />);
|
|
71
|
+
fireEvent.click(screen.getByRole("checkbox", { name: "Select Receipts" }));
|
|
72
|
+
expect(onOpen).not.toHaveBeenCalled();
|
|
73
|
+
fireEvent.click(screen.getByRole("button", { name: "Receipts" }));
|
|
74
|
+
expect(onOpen).toHaveBeenCalledWith(items[1]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("renders the empty slot when there are no items", () => {
|
|
78
|
+
render(<FileTable items={[]} empty={<p>No files yet</p>} />);
|
|
79
|
+
expect(screen.getByText("No files yet")).toBeDefined();
|
|
80
|
+
expect(screen.queryByRole("table")).toBeNull();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { useMemo, type ReactNode } from "react";
|
|
2
|
+
import { CaretDownIcon, CaretUpIcon, XIcon } from "@phosphor-icons/react";
|
|
3
|
+
import { Button } from "../../components/button";
|
|
4
|
+
import { Table } from "../../components/table";
|
|
5
|
+
import { cn } from "../../utils/cn";
|
|
6
|
+
import {
|
|
7
|
+
FileKindIcon,
|
|
8
|
+
ItemActionsMenu,
|
|
9
|
+
formatFileDate,
|
|
10
|
+
formatFileSize,
|
|
11
|
+
toTime,
|
|
12
|
+
useControllable,
|
|
13
|
+
type FileActions,
|
|
14
|
+
type FileItem,
|
|
15
|
+
} from "./file-item";
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
FileKindIcon,
|
|
19
|
+
formatFileDate,
|
|
20
|
+
formatFileSize,
|
|
21
|
+
type FileAction,
|
|
22
|
+
type FileActions,
|
|
23
|
+
type FileItem,
|
|
24
|
+
type FileKind,
|
|
25
|
+
} from "./file-item";
|
|
26
|
+
|
|
27
|
+
export type FileTableSortKey = "name" | "size" | "modifiedAt" | "owner";
|
|
28
|
+
|
|
29
|
+
export interface FileTableSort {
|
|
30
|
+
key: FileTableSortKey;
|
|
31
|
+
direction: "asc" | "desc";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface FileTableLabels {
|
|
35
|
+
name: string;
|
|
36
|
+
size: string;
|
|
37
|
+
modified: string;
|
|
38
|
+
owner: string;
|
|
39
|
+
selectAll: string;
|
|
40
|
+
selectRow: (name: string) => string;
|
|
41
|
+
selected: (count: number) => string;
|
|
42
|
+
clearSelection: string;
|
|
43
|
+
actions: (name: string) => string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const DEFAULT_LABELS: FileTableLabels = {
|
|
47
|
+
name: "Name",
|
|
48
|
+
size: "Size",
|
|
49
|
+
modified: "Modified",
|
|
50
|
+
owner: "Owner",
|
|
51
|
+
selectAll: "Select all",
|
|
52
|
+
selectRow: (name) => `Select ${name}`,
|
|
53
|
+
selected: (count) => `${count} selected`,
|
|
54
|
+
clearSelection: "Clear selection",
|
|
55
|
+
actions: (name) => `Actions for ${name}`,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export interface FileTableProps<T extends FileItem = FileItem> {
|
|
59
|
+
items: T[];
|
|
60
|
+
/** Controlled sort. Folders always stay above files. */
|
|
61
|
+
sort?: FileTableSort;
|
|
62
|
+
/** @default { key: "name", direction: "asc" } */
|
|
63
|
+
defaultSort?: FileTableSort;
|
|
64
|
+
onSortChange?: (sort: FileTableSort) => void;
|
|
65
|
+
/** Show row checkboxes and the bulk-action bar. @default true */
|
|
66
|
+
selectable?: boolean;
|
|
67
|
+
selectedIds?: string[];
|
|
68
|
+
defaultSelectedIds?: string[];
|
|
69
|
+
onSelectionChange?: (ids: string[]) => void;
|
|
70
|
+
/** Shown in the bulk-action bar while rows are selected. */
|
|
71
|
+
bulkActions?: ReactNode | ((selectedIds: string[]) => ReactNode);
|
|
72
|
+
/** Per-row actions menu: an items array or a render prop returning `DropdownMenu.Item`s. */
|
|
73
|
+
actions?: FileActions<T>;
|
|
74
|
+
/** Called when a row is clicked or its name activated. */
|
|
75
|
+
onOpen?: (item: T) => void;
|
|
76
|
+
/** Rendered instead of the table when `items` is empty. */
|
|
77
|
+
empty?: ReactNode;
|
|
78
|
+
labels?: Partial<FileTableLabels>;
|
|
79
|
+
/** Locale for size and date formatting. Defaults to the runtime locale. */
|
|
80
|
+
locale?: string;
|
|
81
|
+
className?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const DEFAULT_SORT: FileTableSort = { key: "name", direction: "asc" };
|
|
85
|
+
|
|
86
|
+
function compare(a: FileItem, b: FileItem, key: FileTableSortKey) {
|
|
87
|
+
if (key === "size") return (a.size ?? -1) - (b.size ?? -1);
|
|
88
|
+
if (key === "modifiedAt") return toTime(a.modifiedAt) - toTime(b.modifiedAt);
|
|
89
|
+
return (a[key] ?? "").localeCompare(b[key] ?? "", undefined, {
|
|
90
|
+
numeric: true,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* FileTable — sortable, selectable list of files and folders.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```tsx
|
|
99
|
+
* <FileTable
|
|
100
|
+
* items={[{ id: "1", name: "Receipts", kind: "folder", itemCount: 48 }]}
|
|
101
|
+
* onOpen={(item) => navigate(item.id)}
|
|
102
|
+
* actions={[{ label: "Delete", variant: "danger", onSelect: remove }]}
|
|
103
|
+
* bulkActions={(ids) => <Button size="sm" onClick={() => share(ids)}>Share</Button>}
|
|
104
|
+
* />
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export function FileTable<T extends FileItem = FileItem>({
|
|
108
|
+
items,
|
|
109
|
+
sort: sortProp,
|
|
110
|
+
defaultSort = DEFAULT_SORT,
|
|
111
|
+
onSortChange,
|
|
112
|
+
selectable = true,
|
|
113
|
+
selectedIds,
|
|
114
|
+
defaultSelectedIds = [],
|
|
115
|
+
onSelectionChange,
|
|
116
|
+
bulkActions,
|
|
117
|
+
actions,
|
|
118
|
+
onOpen,
|
|
119
|
+
empty,
|
|
120
|
+
labels: labelsProp,
|
|
121
|
+
locale,
|
|
122
|
+
className,
|
|
123
|
+
}: FileTableProps<T>) {
|
|
124
|
+
const labels = { ...DEFAULT_LABELS, ...labelsProp };
|
|
125
|
+
const [sort, setSort] = useControllable(sortProp, defaultSort, onSortChange);
|
|
126
|
+
const [selected, setSelected] = useControllable(
|
|
127
|
+
selectedIds,
|
|
128
|
+
defaultSelectedIds,
|
|
129
|
+
onSelectionChange,
|
|
130
|
+
);
|
|
131
|
+
const selectedSet = new Set(selected);
|
|
132
|
+
|
|
133
|
+
const sorted = useMemo(() => {
|
|
134
|
+
const dir = sort.direction === "asc" ? 1 : -1;
|
|
135
|
+
return [...items].sort(
|
|
136
|
+
(a, b) =>
|
|
137
|
+
Number(b.kind === "folder") - Number(a.kind === "folder") ||
|
|
138
|
+
dir * compare(a, b, sort.key),
|
|
139
|
+
);
|
|
140
|
+
}, [items, sort]);
|
|
141
|
+
|
|
142
|
+
if (items.length === 0 && empty) return <>{empty}</>;
|
|
143
|
+
|
|
144
|
+
const allSelected = items.length > 0 && selected.length === items.length;
|
|
145
|
+
const toggle = (id: string, checked: boolean) =>
|
|
146
|
+
setSelected(checked ? [...selected, id] : selected.filter((s) => s !== id));
|
|
147
|
+
|
|
148
|
+
const head = (key: FileTableSortKey, label: string, cls?: string) => {
|
|
149
|
+
const active = sort.key === key;
|
|
150
|
+
const Caret = sort.direction === "asc" ? CaretUpIcon : CaretDownIcon;
|
|
151
|
+
return (
|
|
152
|
+
<Table.Head
|
|
153
|
+
className={cn("font-medium", cls)}
|
|
154
|
+
aria-sort={
|
|
155
|
+
active
|
|
156
|
+
? sort.direction === "asc"
|
|
157
|
+
? "ascending"
|
|
158
|
+
: "descending"
|
|
159
|
+
: undefined
|
|
160
|
+
}
|
|
161
|
+
>
|
|
162
|
+
<button
|
|
163
|
+
type="button"
|
|
164
|
+
className="inline-flex cursor-pointer items-center gap-1 text-kumo-subtle hover:text-kumo-default"
|
|
165
|
+
onClick={() =>
|
|
166
|
+
setSort({
|
|
167
|
+
key,
|
|
168
|
+
direction: active && sort.direction === "asc" ? "desc" : "asc",
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
>
|
|
172
|
+
{label}
|
|
173
|
+
{active && <Caret size={12} weight="bold" aria-hidden />}
|
|
174
|
+
</button>
|
|
175
|
+
</Table.Head>
|
|
176
|
+
);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const bulk =
|
|
180
|
+
typeof bulkActions === "function" ? bulkActions(selected) : bulkActions;
|
|
181
|
+
|
|
182
|
+
return (
|
|
183
|
+
<div className={cn("w-full", className)}>
|
|
184
|
+
{selectable && selected.length > 0 && (
|
|
185
|
+
<div
|
|
186
|
+
role="toolbar"
|
|
187
|
+
aria-label={labels.selected(selected.length)}
|
|
188
|
+
className="flex items-center gap-2 bg-kumo-tint px-3 py-1.5"
|
|
189
|
+
>
|
|
190
|
+
<span className="text-sm font-medium text-kumo-default">
|
|
191
|
+
{labels.selected(selected.length)}
|
|
192
|
+
</span>
|
|
193
|
+
<div className="ml-auto flex items-center gap-1.5">
|
|
194
|
+
{bulk}
|
|
195
|
+
<Button
|
|
196
|
+
variant="ghost"
|
|
197
|
+
size="sm"
|
|
198
|
+
shape="square"
|
|
199
|
+
icon={<XIcon />}
|
|
200
|
+
aria-label={labels.clearSelection}
|
|
201
|
+
onClick={() => setSelected([])}
|
|
202
|
+
/>
|
|
203
|
+
</div>
|
|
204
|
+
</div>
|
|
205
|
+
)}
|
|
206
|
+
<Table>
|
|
207
|
+
<Table.Header variant="compact">
|
|
208
|
+
<Table.Row>
|
|
209
|
+
{selectable && (
|
|
210
|
+
<Table.CheckHead
|
|
211
|
+
label={labels.selectAll}
|
|
212
|
+
checked={allSelected}
|
|
213
|
+
indeterminate={selected.length > 0 && !allSelected}
|
|
214
|
+
onCheckedChange={(checked) =>
|
|
215
|
+
setSelected(checked ? items.map((i) => i.id) : [])
|
|
216
|
+
}
|
|
217
|
+
/>
|
|
218
|
+
)}
|
|
219
|
+
{head("name", labels.name, "w-full")}
|
|
220
|
+
{head("size", labels.size, "text-right")}
|
|
221
|
+
{head("modifiedAt", labels.modified, "hidden sm:table-cell")}
|
|
222
|
+
{head("owner", labels.owner, "hidden md:table-cell")}
|
|
223
|
+
{actions && <Table.Head className="w-10" />}
|
|
224
|
+
</Table.Row>
|
|
225
|
+
</Table.Header>
|
|
226
|
+
<Table.Body>
|
|
227
|
+
{sorted.map((item) => {
|
|
228
|
+
const isSelected = selectedSet.has(item.id);
|
|
229
|
+
return (
|
|
230
|
+
<Table.Row
|
|
231
|
+
key={item.id}
|
|
232
|
+
variant={isSelected ? "selected" : "default"}
|
|
233
|
+
className={cn(
|
|
234
|
+
!isSelected &&
|
|
235
|
+
"even:bg-kumo-base hover:bg-kumo-tint even:hover:bg-kumo-tint",
|
|
236
|
+
onOpen && "cursor-pointer",
|
|
237
|
+
)}
|
|
238
|
+
onClick={onOpen && (() => onOpen(item))}
|
|
239
|
+
>
|
|
240
|
+
{selectable && (
|
|
241
|
+
<Table.CheckCell
|
|
242
|
+
label={labels.selectRow(item.name)}
|
|
243
|
+
checked={isSelected}
|
|
244
|
+
onCheckedChange={(checked) => toggle(item.id, checked)}
|
|
245
|
+
onClick={(e) => e.stopPropagation()}
|
|
246
|
+
/>
|
|
247
|
+
)}
|
|
248
|
+
<Table.Cell className="max-w-0">
|
|
249
|
+
<span className="flex min-w-0 items-center gap-2">
|
|
250
|
+
<FileKindIcon kind={item.kind} />
|
|
251
|
+
{onOpen ? (
|
|
252
|
+
<button
|
|
253
|
+
type="button"
|
|
254
|
+
className="cursor-pointer truncate text-left"
|
|
255
|
+
>
|
|
256
|
+
{item.name}
|
|
257
|
+
</button>
|
|
258
|
+
) : (
|
|
259
|
+
<span className="truncate">{item.name}</span>
|
|
260
|
+
)}
|
|
261
|
+
</span>
|
|
262
|
+
</Table.Cell>
|
|
263
|
+
<Table.Cell className="text-right whitespace-nowrap text-kumo-subtle tabular-nums">
|
|
264
|
+
{item.size === undefined
|
|
265
|
+
? "—"
|
|
266
|
+
: formatFileSize(item.size, locale)}
|
|
267
|
+
</Table.Cell>
|
|
268
|
+
<Table.Cell className="hidden whitespace-nowrap text-kumo-subtle sm:table-cell">
|
|
269
|
+
{item.modifiedAt === undefined
|
|
270
|
+
? "—"
|
|
271
|
+
: formatFileDate(item.modifiedAt, locale)}
|
|
272
|
+
</Table.Cell>
|
|
273
|
+
<Table.Cell className="hidden whitespace-nowrap text-kumo-subtle md:table-cell">
|
|
274
|
+
{item.owner ?? "—"}
|
|
275
|
+
</Table.Cell>
|
|
276
|
+
{actions && (
|
|
277
|
+
<Table.Cell className="py-0 text-right">
|
|
278
|
+
<ItemActionsMenu
|
|
279
|
+
item={item}
|
|
280
|
+
actions={actions}
|
|
281
|
+
label={labels.actions(item.name)}
|
|
282
|
+
/>
|
|
283
|
+
</Table.Cell>
|
|
284
|
+
)}
|
|
285
|
+
</Table.Row>
|
|
286
|
+
);
|
|
287
|
+
})}
|
|
288
|
+
</Table.Body>
|
|
289
|
+
</Table>
|
|
290
|
+
</div>
|
|
291
|
+
);
|
|
292
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it, vi } from "vite-plus/test";
|
|
3
|
+
import type { FileItem } from "../file-table";
|
|
4
|
+
import { FolderGrid } from "./folder-grid";
|
|
5
|
+
|
|
6
|
+
const items: FileItem[] = [
|
|
7
|
+
{
|
|
8
|
+
id: "r",
|
|
9
|
+
name: "Receipts",
|
|
10
|
+
kind: "folder",
|
|
11
|
+
itemCount: 48,
|
|
12
|
+
size: 12 * 1024 * 1024,
|
|
13
|
+
},
|
|
14
|
+
{ id: "l", name: "Lease.pdf", kind: "pdf", size: 1024 },
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
describe("FolderGrid", () => {
|
|
18
|
+
it("renders cards with a meta line", () => {
|
|
19
|
+
render(<FolderGrid items={items} locale="en-US" />);
|
|
20
|
+
expect(screen.getByText("Receipts")).toBeDefined();
|
|
21
|
+
expect(screen.getByText("48 files · 12 MB")).toBeDefined();
|
|
22
|
+
expect(screen.getByText("1 kB")).toBeDefined();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("opens a card and selects without opening", () => {
|
|
26
|
+
const onOpen = vi.fn();
|
|
27
|
+
const onSelectionChange = vi.fn();
|
|
28
|
+
render(
|
|
29
|
+
<FolderGrid
|
|
30
|
+
items={items}
|
|
31
|
+
selectable
|
|
32
|
+
onOpen={onOpen}
|
|
33
|
+
onSelectionChange={onSelectionChange}
|
|
34
|
+
/>,
|
|
35
|
+
);
|
|
36
|
+
fireEvent.click(screen.getByRole("checkbox", { name: "Select Receipts" }));
|
|
37
|
+
expect(onSelectionChange).toHaveBeenCalledWith(["r"]);
|
|
38
|
+
expect(onOpen).not.toHaveBeenCalled();
|
|
39
|
+
fireEvent.click(screen.getByRole("button", { name: "Lease.pdf" }));
|
|
40
|
+
expect(onOpen).toHaveBeenCalledWith(items[1]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("renders an actions trigger per card", () => {
|
|
44
|
+
render(
|
|
45
|
+
<FolderGrid
|
|
46
|
+
items={items}
|
|
47
|
+
actions={[{ label: "Share", onSelect: () => {} }]}
|
|
48
|
+
/>,
|
|
49
|
+
);
|
|
50
|
+
expect(
|
|
51
|
+
screen.getByRole("button", { name: "Actions for Receipts" }),
|
|
52
|
+
).toBeDefined();
|
|
53
|
+
});
|
|
54
|
+
});
|