@open-elements/ui 0.4.0 → 0.6.0
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/dist/components/action-icon-button.d.ts +11 -0
- package/dist/components/action-icon-button.d.ts.map +1 -0
- package/dist/components/action-icon-button.js +14 -0
- package/dist/components/action-icon-button.js.map +1 -0
- package/dist/components/copy-to-clipboard-button.d.ts +7 -0
- package/dist/components/copy-to-clipboard-button.d.ts.map +1 -0
- package/dist/components/copy-to-clipboard-button.js +16 -0
- package/dist/components/copy-to-clipboard-button.js.map +1 -0
- package/dist/components/external-link-button.d.ts +7 -0
- package/dist/components/external-link-button.d.ts.map +1 -0
- package/dist/components/external-link-button.js +10 -0
- package/dist/components/external-link-button.js.map +1 -0
- package/dist/components/mailto-button.d.ts +7 -0
- package/dist/components/mailto-button.d.ts.map +1 -0
- package/dist/components/mailto-button.js +10 -0
- package/dist/components/mailto-button.js.map +1 -0
- package/dist/components/markdown-editor.d.ts.map +1 -1
- package/dist/components/markdown-editor.js +0 -2
- package/dist/components/markdown-editor.js.map +1 -1
- package/dist/components/table-pagination.d.ts +21 -0
- package/dist/components/table-pagination.d.ts.map +1 -0
- package/dist/components/table-pagination.js +15 -0
- package/dist/components/table-pagination.js.map +1 -0
- package/dist/components/tooltip-icon-button.d.ts +24 -0
- package/dist/components/tooltip-icon-button.d.ts.map +1 -0
- package/dist/components/tooltip-icon-button.js +28 -0
- package/dist/components/tooltip-icon-button.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +11 -20
- package/src/components/__tests__/action-icon-button.test.tsx +62 -0
- package/src/components/__tests__/copy-to-clipboard-button.test.tsx +48 -0
- package/src/components/__tests__/external-link-button.test.tsx +28 -0
- package/src/components/__tests__/mailto-button.test.tsx +30 -0
- package/src/components/__tests__/table-pagination.test.tsx +115 -0
- package/src/components/__tests__/tooltip-icon-button.test.tsx +94 -0
- package/src/components/action-icon-button.tsx +40 -0
- package/src/components/copy-to-clipboard-button.tsx +28 -0
- package/src/components/external-link-button.tsx +22 -0
- package/src/components/mailto-button.tsx +22 -0
- package/src/components/markdown-editor.tsx +2 -4
- package/src/components/table-pagination.tsx +88 -0
- package/src/components/tooltip-icon-button.tsx +83 -0
- package/src/index.ts +12 -0
- package/src/types/tiptap-markdown.d.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-elements/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Shared UI component library for Open Elements applications",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -25,17 +25,12 @@
|
|
|
25
25
|
"sideEffects": [
|
|
26
26
|
"**/*.css"
|
|
27
27
|
],
|
|
28
|
-
"
|
|
29
|
-
"react": "^19.0.0",
|
|
30
|
-
"react-dom": "^19.0.0",
|
|
31
|
-
"@base-ui/react": "^1.3.0",
|
|
32
|
-
"radix-ui": "^1.4.0",
|
|
33
|
-
"lucide-react": "^0.500.0",
|
|
28
|
+
"dependencies": {
|
|
34
29
|
"class-variance-authority": "^0.7.0",
|
|
35
30
|
"clsx": "^2.1.0",
|
|
36
31
|
"tailwind-merge": "^3.0.0",
|
|
37
|
-
"react-day-picker": "^9.14.0",
|
|
38
32
|
"date-fns": "^4.1.0",
|
|
33
|
+
"react-day-picker": "^9.14.0",
|
|
39
34
|
"@tiptap/core": "^3.22.0",
|
|
40
35
|
"@tiptap/react": "^3.22.0",
|
|
41
36
|
"@tiptap/starter-kit": "^3.22.0",
|
|
@@ -43,6 +38,13 @@
|
|
|
43
38
|
"@tiptap/extension-placeholder": "^3.22.0",
|
|
44
39
|
"tiptap-markdown": "^0.9.0"
|
|
45
40
|
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"react": "^19.0.0",
|
|
43
|
+
"react-dom": "^19.0.0",
|
|
44
|
+
"@base-ui/react": "^1.3.0",
|
|
45
|
+
"radix-ui": "^1.4.0",
|
|
46
|
+
"lucide-react": "^0.500.0"
|
|
47
|
+
},
|
|
46
48
|
"devDependencies": {
|
|
47
49
|
"@types/react": "^19.0.0",
|
|
48
50
|
"@types/react-dom": "^19.0.0",
|
|
@@ -60,18 +62,7 @@
|
|
|
60
62
|
"react-dom": "^19.0.0",
|
|
61
63
|
"@base-ui/react": "^1.3.0",
|
|
62
64
|
"radix-ui": "^1.4.0",
|
|
63
|
-
"lucide-react": "^0.500.0"
|
|
64
|
-
"class-variance-authority": "^0.7.0",
|
|
65
|
-
"clsx": "^2.1.0",
|
|
66
|
-
"tailwind-merge": "^3.0.0",
|
|
67
|
-
"react-day-picker": "^9.14.0",
|
|
68
|
-
"date-fns": "^4.1.0",
|
|
69
|
-
"@tiptap/core": "^3.22.0",
|
|
70
|
-
"@tiptap/react": "^3.22.0",
|
|
71
|
-
"@tiptap/starter-kit": "^3.22.0",
|
|
72
|
-
"@tiptap/extension-link": "^3.22.0",
|
|
73
|
-
"@tiptap/extension-placeholder": "^3.22.0",
|
|
74
|
-
"tiptap-markdown": "^0.9.0"
|
|
65
|
+
"lucide-react": "^0.500.0"
|
|
75
66
|
},
|
|
76
67
|
"scripts": {
|
|
77
68
|
"build": "tsc -p tsconfig.build.json && pnpm run build:css",
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, vi } from "vitest";
|
|
2
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { ActionIconButton } from "../action-icon-button.tsx";
|
|
4
|
+
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
cleanup();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe("ActionIconButton", () => {
|
|
10
|
+
it("renders children inside the button", () => {
|
|
11
|
+
render(
|
|
12
|
+
<ActionIconButton onClick={() => {}}>
|
|
13
|
+
<svg data-testid="icon" />
|
|
14
|
+
</ActionIconButton>,
|
|
15
|
+
);
|
|
16
|
+
expect(screen.getByTestId("icon")).toBeInTheDocument();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("calls onClick when clicked", () => {
|
|
20
|
+
const handleClick = vi.fn();
|
|
21
|
+
render(
|
|
22
|
+
<ActionIconButton onClick={handleClick}>
|
|
23
|
+
<svg />
|
|
24
|
+
</ActionIconButton>,
|
|
25
|
+
);
|
|
26
|
+
fireEvent.click(screen.getByRole("button"));
|
|
27
|
+
expect(handleClick).toHaveBeenCalledTimes(1);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("stops propagation so parent click handlers do not fire", () => {
|
|
31
|
+
const parentHandler = vi.fn();
|
|
32
|
+
render(
|
|
33
|
+
<div onClick={parentHandler}>
|
|
34
|
+
<ActionIconButton onClick={() => {}}>
|
|
35
|
+
<svg />
|
|
36
|
+
</ActionIconButton>
|
|
37
|
+
</div>,
|
|
38
|
+
);
|
|
39
|
+
fireEvent.click(screen.getByRole("button"));
|
|
40
|
+
expect(parentHandler).not.toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("sets title and aria-label when title is provided", () => {
|
|
44
|
+
render(
|
|
45
|
+
<ActionIconButton onClick={() => {}} title="Edit">
|
|
46
|
+
<svg />
|
|
47
|
+
</ActionIconButton>,
|
|
48
|
+
);
|
|
49
|
+
const button = screen.getByRole("button");
|
|
50
|
+
expect(button).toHaveAttribute("title", "Edit");
|
|
51
|
+
expect(button).toHaveAttribute("aria-label", "Edit");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("applies success tone class when tone is 'success'", () => {
|
|
55
|
+
render(
|
|
56
|
+
<ActionIconButton onClick={() => {}} tone="success">
|
|
57
|
+
<svg />
|
|
58
|
+
</ActionIconButton>,
|
|
59
|
+
);
|
|
60
|
+
expect(screen.getByRole("button").className).toContain("text-primary");
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, beforeEach, vi } from "vitest";
|
|
2
|
+
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { CopyToClipboardButton } from "../copy-to-clipboard-button.tsx";
|
|
4
|
+
|
|
5
|
+
const writeText = vi.fn();
|
|
6
|
+
Object.assign(navigator, { clipboard: { writeText } });
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vi.useFakeTimers();
|
|
10
|
+
writeText.mockClear();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.useRealTimers();
|
|
15
|
+
cleanup();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("CopyToClipboardButton", () => {
|
|
19
|
+
it("renders the copy icon initially", () => {
|
|
20
|
+
const { container } = render(<CopyToClipboardButton value="hello" />);
|
|
21
|
+
expect(container.querySelector("svg.lucide-copy")).toBeInTheDocument();
|
|
22
|
+
expect(container.querySelector("svg.lucide-check")).toBeNull();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("writes the value to the clipboard when clicked", () => {
|
|
26
|
+
render(<CopyToClipboardButton value="hello@example.com" />);
|
|
27
|
+
fireEvent.click(screen.getByRole("button"));
|
|
28
|
+
expect(writeText).toHaveBeenCalledWith("hello@example.com");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("swaps to the check icon after clicking", () => {
|
|
32
|
+
const { container } = render(<CopyToClipboardButton value="x" />);
|
|
33
|
+
fireEvent.click(screen.getByRole("button"));
|
|
34
|
+
expect(container.querySelector("svg.lucide-check")).toBeInTheDocument();
|
|
35
|
+
expect(container.querySelector("svg.lucide-copy")).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("reverts to the copy icon after the timeout", () => {
|
|
39
|
+
const { container } = render(<CopyToClipboardButton value="x" />);
|
|
40
|
+
fireEvent.click(screen.getByRole("button"));
|
|
41
|
+
expect(container.querySelector("svg.lucide-check")).toBeInTheDocument();
|
|
42
|
+
act(() => {
|
|
43
|
+
vi.advanceTimersByTime(2000);
|
|
44
|
+
});
|
|
45
|
+
expect(container.querySelector("svg.lucide-copy")).toBeInTheDocument();
|
|
46
|
+
expect(container.querySelector("svg.lucide-check")).toBeNull();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, beforeEach, vi } from "vitest";
|
|
2
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { ExternalLinkButton } from "../external-link-button.tsx";
|
|
4
|
+
|
|
5
|
+
const openSpy = vi.fn();
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
openSpy.mockClear();
|
|
9
|
+
vi.spyOn(window, "open").mockImplementation(openSpy);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
vi.restoreAllMocks();
|
|
14
|
+
cleanup();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe("ExternalLinkButton", () => {
|
|
18
|
+
it("renders the external-link icon", () => {
|
|
19
|
+
const { container } = render(<ExternalLinkButton href="https://example.com" />);
|
|
20
|
+
expect(container.querySelector("svg.lucide-external-link")).toBeInTheDocument();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("opens the URL in a new tab with noopener,noreferrer on click", () => {
|
|
24
|
+
render(<ExternalLinkButton href="https://example.com" />);
|
|
25
|
+
fireEvent.click(screen.getByRole("button"));
|
|
26
|
+
expect(openSpy).toHaveBeenCalledWith("https://example.com", "_blank", "noopener,noreferrer");
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, beforeEach } from "vitest";
|
|
2
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { MailtoButton } from "../mailto-button.tsx";
|
|
4
|
+
|
|
5
|
+
const originalLocation = window.location;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
Object.defineProperty(window, "location", {
|
|
9
|
+
writable: true,
|
|
10
|
+
value: { ...originalLocation, href: "" },
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
Object.defineProperty(window, "location", { writable: true, value: originalLocation });
|
|
16
|
+
cleanup();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("MailtoButton", () => {
|
|
20
|
+
it("renders the mail icon", () => {
|
|
21
|
+
const { container } = render(<MailtoButton email="x@y.z" />);
|
|
22
|
+
expect(container.querySelector("svg.lucide-mail")).toBeInTheDocument();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("navigates to the mailto: URL on click", () => {
|
|
26
|
+
render(<MailtoButton email="user@example.com" />);
|
|
27
|
+
fireEvent.click(screen.getByRole("button"));
|
|
28
|
+
expect(window.location.href).toBe("mailto:user@example.com");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, beforeEach, vi } from "vitest";
|
|
2
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { TablePagination, type PaginationTranslations } from "../table-pagination.tsx";
|
|
4
|
+
|
|
5
|
+
const translations: PaginationTranslations = {
|
|
6
|
+
perPage: "per page",
|
|
7
|
+
previous: "Previous",
|
|
8
|
+
next: "Next",
|
|
9
|
+
totalOne: "{count} entry",
|
|
10
|
+
totalOther: "{count} entries",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const localStorageMock = (() => {
|
|
14
|
+
let store: Record<string, string> = {};
|
|
15
|
+
return {
|
|
16
|
+
getItem: vi.fn((key: string) => store[key] ?? null),
|
|
17
|
+
setItem: vi.fn((key: string, value: string) => {
|
|
18
|
+
store[key] = value;
|
|
19
|
+
}),
|
|
20
|
+
removeItem: vi.fn((key: string) => {
|
|
21
|
+
delete store[key];
|
|
22
|
+
}),
|
|
23
|
+
clear: vi.fn(() => {
|
|
24
|
+
store = {};
|
|
25
|
+
}),
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
Object.defineProperty(globalThis, "localStorage", { value: localStorageMock });
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
localStorageMock.clear();
|
|
32
|
+
localStorageMock.setItem.mockClear();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
cleanup();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const defaultProps = {
|
|
40
|
+
page: 0,
|
|
41
|
+
pageSize: 20,
|
|
42
|
+
pageSizeOptions: [10, 20, 50, 100, 200] as const,
|
|
43
|
+
storageKey: "pageSize.test",
|
|
44
|
+
translations,
|
|
45
|
+
onPageChange: vi.fn(),
|
|
46
|
+
onPageSizeChange: vi.fn(),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
describe("TablePagination", () => {
|
|
50
|
+
it("renders the singular total label when only one entry", () => {
|
|
51
|
+
render(<TablePagination {...defaultProps} totalElements={1} totalPages={1} />);
|
|
52
|
+
expect(screen.getByText(/· 1 entry/)).toBeInTheDocument();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("renders the plural total label when more than one entry", () => {
|
|
56
|
+
render(<TablePagination {...defaultProps} totalElements={35} totalPages={2} />);
|
|
57
|
+
expect(screen.getByText(/· 35 entries/)).toBeInTheDocument();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("hides previous/next buttons when totalPages <= 1", () => {
|
|
61
|
+
render(<TablePagination {...defaultProps} totalElements={5} totalPages={1} />);
|
|
62
|
+
expect(screen.queryByText("Previous")).toBeNull();
|
|
63
|
+
expect(screen.queryByText("Next")).toBeNull();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("keeps the page-size selector visible when totalPages <= 1", () => {
|
|
67
|
+
render(<TablePagination {...defaultProps} totalElements={5} totalPages={1} />);
|
|
68
|
+
expect(screen.getByText("per page")).toBeInTheDocument();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("disables previous on the first page and enables next", () => {
|
|
72
|
+
render(<TablePagination {...defaultProps} totalElements={35} totalPages={2} />);
|
|
73
|
+
expect(screen.getByText("Previous").closest("button")).toBeDisabled();
|
|
74
|
+
expect(screen.getByText("Next").closest("button")).not.toBeDisabled();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("disables next on the last page", () => {
|
|
78
|
+
render(<TablePagination {...defaultProps} page={1} totalElements={35} totalPages={2} />);
|
|
79
|
+
expect(screen.getByText("Next").closest("button")).toBeDisabled();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("calls onPageChange with page+1 when next is clicked", () => {
|
|
83
|
+
const onPageChange = vi.fn();
|
|
84
|
+
render(
|
|
85
|
+
<TablePagination
|
|
86
|
+
{...defaultProps}
|
|
87
|
+
onPageChange={onPageChange}
|
|
88
|
+
totalElements={35}
|
|
89
|
+
totalPages={2}
|
|
90
|
+
/>,
|
|
91
|
+
);
|
|
92
|
+
fireEvent.click(screen.getByText("Next"));
|
|
93
|
+
expect(onPageChange).toHaveBeenCalledWith(1);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("calls onPageChange with page-1 when previous is clicked", () => {
|
|
97
|
+
const onPageChange = vi.fn();
|
|
98
|
+
render(
|
|
99
|
+
<TablePagination
|
|
100
|
+
{...defaultProps}
|
|
101
|
+
page={1}
|
|
102
|
+
onPageChange={onPageChange}
|
|
103
|
+
totalElements={35}
|
|
104
|
+
totalPages={2}
|
|
105
|
+
/>,
|
|
106
|
+
);
|
|
107
|
+
fireEvent.click(screen.getByText("Previous"));
|
|
108
|
+
expect(onPageChange).toHaveBeenCalledWith(0);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("sets aria-label on the size selector", () => {
|
|
112
|
+
render(<TablePagination {...defaultProps} totalElements={5} totalPages={1} />);
|
|
113
|
+
expect(screen.getByLabelText("per page")).toBeInTheDocument();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, vi } from "vitest";
|
|
2
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
3
|
+
import { Pencil, Trash2 } from "lucide-react";
|
|
4
|
+
import { TooltipIconButton } from "../tooltip-icon-button.tsx";
|
|
5
|
+
import { TooltipProvider } from "../tooltip.tsx";
|
|
6
|
+
|
|
7
|
+
function renderWithTooltip(ui: React.ReactElement) {
|
|
8
|
+
return render(<TooltipProvider>{ui}</TooltipProvider>);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
cleanup();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("TooltipIconButton", () => {
|
|
16
|
+
it("renders the icon", () => {
|
|
17
|
+
const { container } = renderWithTooltip(
|
|
18
|
+
<TooltipIconButton icon={<Pencil />} tooltip="Edit" onClick={() => {}} />,
|
|
19
|
+
);
|
|
20
|
+
expect(container.querySelector("svg.lucide-pencil")).toBeInTheDocument();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("uses the tooltip string as the button's accessible name", () => {
|
|
24
|
+
renderWithTooltip(
|
|
25
|
+
<TooltipIconButton icon={<Pencil />} tooltip="Edit user" onClick={() => {}} />,
|
|
26
|
+
);
|
|
27
|
+
expect(screen.getByRole("button", { name: "Edit user" })).toBeInTheDocument();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("calls onClick when clicked", () => {
|
|
31
|
+
const handleClick = vi.fn();
|
|
32
|
+
renderWithTooltip(<TooltipIconButton icon={<Pencil />} tooltip="Edit" onClick={handleClick} />);
|
|
33
|
+
fireEvent.click(screen.getByRole("button"));
|
|
34
|
+
expect(handleClick).toHaveBeenCalledTimes(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("stops click propagation so parent handlers do not fire", () => {
|
|
38
|
+
const parentHandler = vi.fn();
|
|
39
|
+
renderWithTooltip(
|
|
40
|
+
<div onClick={parentHandler}>
|
|
41
|
+
<TooltipIconButton icon={<Pencil />} tooltip="Edit" onClick={() => {}} />
|
|
42
|
+
</div>,
|
|
43
|
+
);
|
|
44
|
+
fireEvent.click(screen.getByRole("button"));
|
|
45
|
+
expect(parentHandler).not.toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("applies the destructive tone class", () => {
|
|
49
|
+
renderWithTooltip(
|
|
50
|
+
<TooltipIconButton
|
|
51
|
+
icon={<Trash2 />}
|
|
52
|
+
tooltip="Delete"
|
|
53
|
+
tone="destructive"
|
|
54
|
+
onClick={() => {}}
|
|
55
|
+
/>,
|
|
56
|
+
);
|
|
57
|
+
expect(screen.getByRole("button").className).toContain("text-destructive");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("applies the default (primary) tone class when tone is omitted", () => {
|
|
61
|
+
renderWithTooltip(<TooltipIconButton icon={<Pencil />} tooltip="Edit" onClick={() => {}} />);
|
|
62
|
+
expect(screen.getByRole("button").className).toContain("text-primary");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("disables the button when disabled is true and does not fire onClick", () => {
|
|
66
|
+
const handleClick = vi.fn();
|
|
67
|
+
renderWithTooltip(
|
|
68
|
+
<TooltipIconButton
|
|
69
|
+
icon={<Trash2 />}
|
|
70
|
+
tooltip="Cannot delete"
|
|
71
|
+
disabled
|
|
72
|
+
onClick={handleClick}
|
|
73
|
+
/>,
|
|
74
|
+
);
|
|
75
|
+
const button = screen.getByRole("button");
|
|
76
|
+
expect(button).toBeDisabled();
|
|
77
|
+
fireEvent.click(button);
|
|
78
|
+
expect(handleClick).not.toHaveBeenCalled();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("renders the asChild element instead of a button when asChild is set", () => {
|
|
82
|
+
const { container } = renderWithTooltip(
|
|
83
|
+
<TooltipIconButton tooltip="Edit tag" asChild>
|
|
84
|
+
<a href="/tags/123/edit">
|
|
85
|
+
<Pencil />
|
|
86
|
+
</a>
|
|
87
|
+
</TooltipIconButton>,
|
|
88
|
+
);
|
|
89
|
+
const anchor = container.querySelector("a");
|
|
90
|
+
expect(anchor).toHaveAttribute("href", "/tags/123/edit");
|
|
91
|
+
expect(anchor).toHaveAttribute("aria-label", "Edit tag");
|
|
92
|
+
expect(anchor?.querySelector("svg.lucide-pencil")).toBeInTheDocument();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { MouseEvent, ReactNode } from "react";
|
|
4
|
+
import { cn } from "../lib/utils.ts";
|
|
5
|
+
|
|
6
|
+
type Tone = "default" | "success";
|
|
7
|
+
|
|
8
|
+
type ActionIconButtonProps = {
|
|
9
|
+
readonly onClick: (event: MouseEvent<HTMLButtonElement>) => void;
|
|
10
|
+
readonly children: ReactNode;
|
|
11
|
+
readonly title?: string;
|
|
12
|
+
readonly tone?: Tone;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const TONE_CLASS: Record<Tone, string> = {
|
|
16
|
+
default: "text-muted-foreground hover:text-foreground [@media(pointer:coarse)]:text-foreground",
|
|
17
|
+
success: "text-primary",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function ActionIconButton({
|
|
21
|
+
onClick,
|
|
22
|
+
children,
|
|
23
|
+
title,
|
|
24
|
+
tone = "default",
|
|
25
|
+
}: ActionIconButtonProps) {
|
|
26
|
+
return (
|
|
27
|
+
<button
|
|
28
|
+
type="button"
|
|
29
|
+
title={title}
|
|
30
|
+
aria-label={title}
|
|
31
|
+
onClick={(event) => {
|
|
32
|
+
event.stopPropagation();
|
|
33
|
+
onClick(event);
|
|
34
|
+
}}
|
|
35
|
+
className={cn("transition-colors [&_svg]:h-3.5 [&_svg]:w-3.5", TONE_CLASS[tone])}
|
|
36
|
+
>
|
|
37
|
+
{children}
|
|
38
|
+
</button>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { Check, Copy } from "lucide-react";
|
|
5
|
+
import { ActionIconButton } from "./action-icon-button.tsx";
|
|
6
|
+
|
|
7
|
+
type CopyToClipboardButtonProps = {
|
|
8
|
+
readonly value: string;
|
|
9
|
+
readonly title?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const COPIED_RESET_MS = 2000;
|
|
13
|
+
|
|
14
|
+
export function CopyToClipboardButton({ value, title }: CopyToClipboardButtonProps) {
|
|
15
|
+
const [copied, setCopied] = useState(false);
|
|
16
|
+
|
|
17
|
+
const handleClick = () => {
|
|
18
|
+
navigator.clipboard.writeText(value);
|
|
19
|
+
setCopied(true);
|
|
20
|
+
setTimeout(() => setCopied(false), COPIED_RESET_MS);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<ActionIconButton onClick={handleClick} title={title} tone={copied ? "success" : "default"}>
|
|
25
|
+
{copied ? <Check /> : <Copy />}
|
|
26
|
+
</ActionIconButton>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ExternalLink } from "lucide-react";
|
|
4
|
+
import { ActionIconButton } from "./action-icon-button.tsx";
|
|
5
|
+
|
|
6
|
+
type ExternalLinkButtonProps = {
|
|
7
|
+
readonly href: string;
|
|
8
|
+
readonly title?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function ExternalLinkButton({ href, title }: ExternalLinkButtonProps) {
|
|
12
|
+
return (
|
|
13
|
+
<ActionIconButton
|
|
14
|
+
onClick={() => {
|
|
15
|
+
window.open(href, "_blank", "noopener,noreferrer");
|
|
16
|
+
}}
|
|
17
|
+
title={title}
|
|
18
|
+
>
|
|
19
|
+
<ExternalLink />
|
|
20
|
+
</ActionIconButton>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Mail } from "lucide-react";
|
|
4
|
+
import { ActionIconButton } from "./action-icon-button.tsx";
|
|
5
|
+
|
|
6
|
+
type MailtoButtonProps = {
|
|
7
|
+
readonly email: string;
|
|
8
|
+
readonly title?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function MailtoButton({ email, title }: MailtoButtonProps) {
|
|
12
|
+
return (
|
|
13
|
+
<ActionIconButton
|
|
14
|
+
onClick={() => {
|
|
15
|
+
window.location.href = `mailto:${email}`;
|
|
16
|
+
}}
|
|
17
|
+
title={title}
|
|
18
|
+
>
|
|
19
|
+
<Mail />
|
|
20
|
+
</ActionIconButton>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
@@ -115,8 +115,7 @@ export function MarkdownEditor({ value, onChange, placeholder }: MarkdownEditorP
|
|
|
115
115
|
content: value,
|
|
116
116
|
immediatelyRender: false,
|
|
117
117
|
onUpdate: ({ editor: ed }) => {
|
|
118
|
-
|
|
119
|
-
onChange((ed.storage as any).markdown.getMarkdown());
|
|
118
|
+
onChange(ed.storage.markdown.getMarkdown());
|
|
120
119
|
},
|
|
121
120
|
editorProps: {
|
|
122
121
|
attributes: {
|
|
@@ -127,8 +126,7 @@ export function MarkdownEditor({ value, onChange, placeholder }: MarkdownEditorP
|
|
|
127
126
|
|
|
128
127
|
useEffect(() => {
|
|
129
128
|
if (!editor) return;
|
|
130
|
-
|
|
131
|
-
const currentMarkdown = (editor.storage as any).markdown.getMarkdown();
|
|
129
|
+
const currentMarkdown = editor.storage.markdown.getMarkdown();
|
|
132
130
|
if (value !== currentMarkdown) {
|
|
133
131
|
editor.commands.setContent(value);
|
|
134
132
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Button } from "./button.tsx";
|
|
4
|
+
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select.tsx";
|
|
5
|
+
|
|
6
|
+
export type PaginationTranslations = {
|
|
7
|
+
readonly perPage: string;
|
|
8
|
+
readonly previous: string;
|
|
9
|
+
readonly next: string;
|
|
10
|
+
readonly totalOne: string;
|
|
11
|
+
readonly totalOther: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type TablePaginationProps = {
|
|
15
|
+
readonly page: number;
|
|
16
|
+
readonly pageSize: number;
|
|
17
|
+
readonly totalElements: number;
|
|
18
|
+
readonly totalPages: number;
|
|
19
|
+
readonly pageSizeOptions: readonly number[];
|
|
20
|
+
readonly storageKey: string;
|
|
21
|
+
readonly translations: PaginationTranslations;
|
|
22
|
+
readonly onPageChange: (page: number) => void;
|
|
23
|
+
readonly onPageSizeChange: (size: number) => void;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function TablePagination({
|
|
27
|
+
page,
|
|
28
|
+
pageSize,
|
|
29
|
+
totalElements,
|
|
30
|
+
totalPages,
|
|
31
|
+
pageSizeOptions,
|
|
32
|
+
storageKey,
|
|
33
|
+
translations,
|
|
34
|
+
onPageChange,
|
|
35
|
+
onPageSizeChange,
|
|
36
|
+
}: TablePaginationProps) {
|
|
37
|
+
const totalTemplate = totalElements === 1 ? translations.totalOne : translations.totalOther;
|
|
38
|
+
const totalLabel = totalTemplate.replace("{count}", String(totalElements));
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<div className="mt-4 flex items-center justify-between">
|
|
42
|
+
<div className="flex items-center gap-3">
|
|
43
|
+
<Select
|
|
44
|
+
value={String(pageSize)}
|
|
45
|
+
onValueChange={(v) => {
|
|
46
|
+
const n = Number(v);
|
|
47
|
+
onPageSizeChange(n);
|
|
48
|
+
localStorage.setItem(storageKey, v);
|
|
49
|
+
onPageChange(0);
|
|
50
|
+
}}
|
|
51
|
+
>
|
|
52
|
+
<SelectTrigger className="w-[80px] h-8 text-sm" aria-label={translations.perPage}>
|
|
53
|
+
<SelectValue />
|
|
54
|
+
</SelectTrigger>
|
|
55
|
+
<SelectContent>
|
|
56
|
+
{pageSizeOptions.map((s) => (
|
|
57
|
+
<SelectItem key={s} value={String(s)}>
|
|
58
|
+
{s}
|
|
59
|
+
</SelectItem>
|
|
60
|
+
))}
|
|
61
|
+
</SelectContent>
|
|
62
|
+
</Select>
|
|
63
|
+
<span className="text-sm text-muted-foreground">{translations.perPage}</span>
|
|
64
|
+
<span className="text-sm text-muted-foreground">· {totalLabel}</span>
|
|
65
|
+
</div>
|
|
66
|
+
{totalPages > 1 && (
|
|
67
|
+
<div className="flex gap-2">
|
|
68
|
+
<Button
|
|
69
|
+
variant="outline"
|
|
70
|
+
size="sm"
|
|
71
|
+
disabled={page === 0}
|
|
72
|
+
onClick={() => onPageChange(page - 1)}
|
|
73
|
+
>
|
|
74
|
+
{translations.previous}
|
|
75
|
+
</Button>
|
|
76
|
+
<Button
|
|
77
|
+
variant="outline"
|
|
78
|
+
size="sm"
|
|
79
|
+
disabled={page >= totalPages - 1}
|
|
80
|
+
onClick={() => onPageChange(page + 1)}
|
|
81
|
+
>
|
|
82
|
+
{translations.next}
|
|
83
|
+
</Button>
|
|
84
|
+
</div>
|
|
85
|
+
)}
|
|
86
|
+
</div>
|
|
87
|
+
);
|
|
88
|
+
}
|