@aleph-alpha/ui-library 1.19.0 → 1.21.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/system/index.d.ts +69 -0
- package/dist/system/lib.js +12528 -12369
- package/docs/public-docs/getting-started-designers.md +19 -6
- package/package.json +4 -3
- package/src/components/UiCodeBlock/UiCodeBlock.stories.ts +352 -0
- package/src/components/UiCodeBlock/UiCodeBlock.vue +168 -0
- package/src/components/UiCodeBlock/__tests__/UiCodeBlock.test.ts +210 -0
- package/src/components/UiCodeBlock/index.ts +2 -0
- package/src/components/UiCodeBlock/types.ts +60 -0
- package/src/components/index.ts +1 -0
- package/src/patterns/UiDataTable/UiDataTable.stories.ts +98 -1
- package/src/patterns/UiDataTable/UiDataTable.vue +15 -0
- package/src/patterns/UiDataTable/UiDataTableColumnHeader.vue +29 -55
- package/src/patterns/UiDataTable/__tests__/UiDataTable.test.ts +90 -8
- package/src/patterns/UiDataTable/__tests__/UiDataTableColumnHeader.test.ts +27 -81
- package/src/patterns/UiDataTable/types.ts +5 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { render, waitFor } from '@testing-library/vue';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
3
|
+
import { afterEach, describe, expect, test, vi, beforeEach } from 'vitest';
|
|
4
|
+
import UiCodeBlock from '../UiCodeBlock.vue';
|
|
5
|
+
|
|
6
|
+
vi.mock('shiki', () => ({
|
|
7
|
+
codeToHtml: vi
|
|
8
|
+
.fn()
|
|
9
|
+
.mockResolvedValue(
|
|
10
|
+
'<pre class="shiki shiki-themes github-light github-dark"><code><span class="line">const x = 1;</span></code></pre>',
|
|
11
|
+
),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
describe('UiCodeBlock', () => {
|
|
15
|
+
let originalClipboardDescriptor: PropertyDescriptor | undefined;
|
|
16
|
+
let originalExecCommand: typeof document.execCommand;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.clearAllMocks();
|
|
20
|
+
|
|
21
|
+
originalClipboardDescriptor = Object.getOwnPropertyDescriptor(navigator, 'clipboard');
|
|
22
|
+
originalExecCommand = document.execCommand;
|
|
23
|
+
|
|
24
|
+
Object.defineProperty(navigator, 'clipboard', {
|
|
25
|
+
value: {
|
|
26
|
+
writeText: vi.fn().mockResolvedValue(undefined),
|
|
27
|
+
readText: vi.fn().mockResolvedValue(''),
|
|
28
|
+
},
|
|
29
|
+
writable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
});
|
|
32
|
+
document.execCommand = vi.fn().mockReturnValue(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
if (originalClipboardDescriptor) {
|
|
37
|
+
Object.defineProperty(navigator, 'clipboard', originalClipboardDescriptor);
|
|
38
|
+
} else {
|
|
39
|
+
delete (navigator as never)['clipboard'];
|
|
40
|
+
}
|
|
41
|
+
document.execCommand = originalExecCommand;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('renders Shiki highlighted output after async load', async () => {
|
|
45
|
+
const { container } = render(UiCodeBlock, {
|
|
46
|
+
props: { code: 'const x = 1;', language: 'typescript' },
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
await waitFor(() => {
|
|
50
|
+
expect(container.querySelector('.shiki')).toBeInTheDocument();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('shows language label in header', async () => {
|
|
55
|
+
const { getByText } = render(UiCodeBlock, {
|
|
56
|
+
props: { code: 'print("hi")', language: 'python' },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await waitFor(() => {
|
|
60
|
+
expect(getByText('python')).toBeInTheDocument();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('shows filename in header and takes precedence over language label', async () => {
|
|
65
|
+
const { getByText, queryByText } = render(UiCodeBlock, {
|
|
66
|
+
props: { code: '{}', language: 'json', filename: 'config.json' },
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
await waitFor(() => {
|
|
70
|
+
expect(getByText('config.json')).toBeInTheDocument();
|
|
71
|
+
expect(queryByText('json')).not.toBeInTheDocument();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('shows copy button by default', async () => {
|
|
76
|
+
const { getByRole } = render(UiCodeBlock, {
|
|
77
|
+
props: { code: 'code', language: 'typescript' },
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
await waitFor(() => {
|
|
81
|
+
expect(getByRole('button', { name: 'Copy code' })).toBeInTheDocument();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('hides copy button when showCopyButton is false', async () => {
|
|
86
|
+
const { queryByRole } = render(UiCodeBlock, {
|
|
87
|
+
props: { code: 'code', language: 'typescript', showCopyButton: false },
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
await waitFor(() => {
|
|
91
|
+
expect(queryByRole('button', { name: 'Copy code' })).not.toBeInTheDocument();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('changes button label to "Copied" after clicking copy', async () => {
|
|
96
|
+
const user = userEvent.setup();
|
|
97
|
+
|
|
98
|
+
const { getByRole } = render(UiCodeBlock, {
|
|
99
|
+
props: { code: 'const x = 1;', language: 'typescript' },
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
await waitFor(() => {
|
|
103
|
+
expect(getByRole('button', { name: 'Copy code' })).toBeInTheDocument();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
await user.click(getByRole('button', { name: 'Copy code' }));
|
|
107
|
+
|
|
108
|
+
await waitFor(() => {
|
|
109
|
+
expect(getByRole('button', { name: 'Copied' })).toBeInTheDocument();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('applies line-numbers class when showLineNumbers is true', async () => {
|
|
114
|
+
const { container } = render(UiCodeBlock, {
|
|
115
|
+
props: { code: 'code', language: 'typescript', showLineNumbers: true },
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
await waitFor(() => {
|
|
119
|
+
expect(container.querySelector('.ui-code-block__line-numbers')).toBeInTheDocument();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('applies maxHeight style when provided', async () => {
|
|
124
|
+
const { container } = render(UiCodeBlock, {
|
|
125
|
+
props: { code: 'code', language: 'typescript', maxHeight: '200px' },
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
await waitFor(() => {
|
|
129
|
+
const scrollContainer = container.querySelector('.overflow-auto');
|
|
130
|
+
expect(scrollContainer).toHaveStyle({ maxHeight: '200px' });
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('uses custom copy labels when provided', async () => {
|
|
135
|
+
const user = userEvent.setup();
|
|
136
|
+
|
|
137
|
+
const { getByRole } = render(UiCodeBlock, {
|
|
138
|
+
props: {
|
|
139
|
+
code: 'x = 1',
|
|
140
|
+
language: 'python',
|
|
141
|
+
copyLabel: 'Code kopieren',
|
|
142
|
+
copiedLabel: 'Kopiert',
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
await waitFor(() => {
|
|
147
|
+
expect(getByRole('button', { name: 'Code kopieren' })).toBeInTheDocument();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
await user.click(getByRole('button', { name: 'Code kopieren' }));
|
|
151
|
+
|
|
152
|
+
await waitFor(() => {
|
|
153
|
+
expect(getByRole('button', { name: 'Kopiert' })).toBeInTheDocument();
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('falls back to text highlighting when language is unsupported', async () => {
|
|
158
|
+
const { codeToHtml } = await import('shiki');
|
|
159
|
+
const mock = vi.mocked(codeToHtml);
|
|
160
|
+
mock
|
|
161
|
+
.mockRejectedValueOnce(new Error('Unknown language'))
|
|
162
|
+
.mockResolvedValueOnce(
|
|
163
|
+
'<pre class="shiki shiki-themes github-light github-dark"><code><span class="line">code</span></code></pre>',
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
const { container } = render(UiCodeBlock, {
|
|
167
|
+
props: { code: 'code', language: 'unknown-lang' },
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
await waitFor(() => {
|
|
171
|
+
expect(container.querySelector('.shiki')).toBeInTheDocument();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(mock).toHaveBeenCalledTimes(2);
|
|
175
|
+
expect(mock).toHaveBeenLastCalledWith('code', expect.objectContaining({ lang: 'text' }));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test('shows plain pre/code fallback when all highlighting fails', async () => {
|
|
179
|
+
const { codeToHtml } = await import('shiki');
|
|
180
|
+
vi.mocked(codeToHtml).mockRejectedValue(new Error('Shiki broken'));
|
|
181
|
+
|
|
182
|
+
const { container } = render(UiCodeBlock, {
|
|
183
|
+
props: { code: 'fallback content', language: 'typescript' },
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
await waitFor(() => {
|
|
187
|
+
const codeEl = container.querySelector('pre code');
|
|
188
|
+
expect(codeEl).toBeInTheDocument();
|
|
189
|
+
expect(codeEl?.textContent).toBe('fallback content');
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('calls codeToHtml with correct language and dual themes', async () => {
|
|
194
|
+
const { codeToHtml } = await import('shiki');
|
|
195
|
+
|
|
196
|
+
render(UiCodeBlock, {
|
|
197
|
+
props: { code: 'const x = 1;', language: 'typescript' },
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
await waitFor(() => {
|
|
201
|
+
expect(codeToHtml).toHaveBeenCalledWith('const x = 1;', {
|
|
202
|
+
lang: 'typescript',
|
|
203
|
+
themes: {
|
|
204
|
+
light: 'github-light',
|
|
205
|
+
dark: 'github-dark',
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A code block component for displaying source code with syntax highlighting,
|
|
3
|
+
* line numbers, and copy-to-clipboard functionality. Powered by Shiki.
|
|
4
|
+
* @category Content
|
|
5
|
+
* @useCases code display, code snippet, syntax highlighting, API response, terminal output
|
|
6
|
+
* @keywords code, codeblock, syntax, highlight, copy, pre, snippet
|
|
7
|
+
* @related UiKbd
|
|
8
|
+
*/
|
|
9
|
+
export interface UiCodeBlockProps {
|
|
10
|
+
/**
|
|
11
|
+
* The source code to display.
|
|
12
|
+
*/
|
|
13
|
+
code: string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The programming language for syntax highlighting.
|
|
17
|
+
* Supports all languages available in Shiki (e.g., 'typescript', 'python', 'json', 'bash').
|
|
18
|
+
* Use 'text' for plain text without highlighting.
|
|
19
|
+
* @default 'text'
|
|
20
|
+
*/
|
|
21
|
+
language?: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Optional filename to display in the header.
|
|
25
|
+
* When provided, takes precedence over the language label.
|
|
26
|
+
* @example 'app.config.ts'
|
|
27
|
+
*/
|
|
28
|
+
filename?: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Whether to show line numbers alongside the code.
|
|
32
|
+
* @default false
|
|
33
|
+
*/
|
|
34
|
+
showLineNumbers?: boolean;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether to show the copy-to-clipboard button in the header.
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
showCopyButton?: boolean;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Maximum height of the code area before scrolling.
|
|
44
|
+
* Accepts any valid CSS height value.
|
|
45
|
+
* @example '300px'
|
|
46
|
+
*/
|
|
47
|
+
maxHeight?: string;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Aria label for the copy button in its default state.
|
|
51
|
+
* @default 'Copy code'
|
|
52
|
+
*/
|
|
53
|
+
copyLabel?: string;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Aria label for the copy button after copying.
|
|
57
|
+
* @default 'Copied'
|
|
58
|
+
*/
|
|
59
|
+
copiedLabel?: string;
|
|
60
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -229,7 +229,7 @@ const data: Payment[] = [
|
|
|
229
229
|
{ id: '3', amount: 837, status: 'processing', email: 'monserrat44@gmail.com' },
|
|
230
230
|
];
|
|
231
231
|
|
|
232
|
-
// Use UiDataTableColumnHeader for sortable columns
|
|
232
|
+
// Use UiDataTableColumnHeader for sortable columns (click to cycle: unsorted → asc → desc)
|
|
233
233
|
const columns: ColumnDef<Payment>[] = [
|
|
234
234
|
{
|
|
235
235
|
accessorKey: 'status',
|
|
@@ -1575,3 +1575,100 @@ export const WithCellFormatting: Story = {
|
|
|
1575
1575
|
template: '<UiDataTable v-bind="args" />',
|
|
1576
1576
|
}),
|
|
1577
1577
|
};
|
|
1578
|
+
|
|
1579
|
+
const withRowClickTemplateSource = `<script setup lang="ts">
|
|
1580
|
+
import { ref, h } from 'vue';
|
|
1581
|
+
import { UiDataTable, UiDataTableColumnHeader } from '@aleph-alpha/ui-library';
|
|
1582
|
+
import type { ColumnDef } from '@tanstack/vue-table';
|
|
1583
|
+
|
|
1584
|
+
interface Payment {
|
|
1585
|
+
id: string;
|
|
1586
|
+
amount: number;
|
|
1587
|
+
status: string;
|
|
1588
|
+
email: string;
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
const data: Payment[] = [
|
|
1592
|
+
{ id: '1', amount: 316, status: 'success', email: 'ken99@yahoo.com' },
|
|
1593
|
+
{ id: '2', amount: 242, status: 'success', email: 'abe45@gmail.com' },
|
|
1594
|
+
{ id: '3', amount: 837, status: 'processing', email: 'monserrat44@gmail.com' },
|
|
1595
|
+
];
|
|
1596
|
+
|
|
1597
|
+
const columns: ColumnDef<Payment>[] = [
|
|
1598
|
+
{
|
|
1599
|
+
accessorKey: 'status',
|
|
1600
|
+
header: ({ column }) => h(UiDataTableColumnHeader, { column, title: 'Status' }),
|
|
1601
|
+
size: 120,
|
|
1602
|
+
},
|
|
1603
|
+
{
|
|
1604
|
+
accessorKey: 'email',
|
|
1605
|
+
header: ({ column }) => h(UiDataTableColumnHeader, { column, title: 'Email' }),
|
|
1606
|
+
size: 250,
|
|
1607
|
+
},
|
|
1608
|
+
{
|
|
1609
|
+
accessorKey: 'amount',
|
|
1610
|
+
header: ({ column }) => h(UiDataTableColumnHeader, {
|
|
1611
|
+
column,
|
|
1612
|
+
title: 'Amount',
|
|
1613
|
+
class: 'justify-end'
|
|
1614
|
+
}),
|
|
1615
|
+
cell: ({ row }) => {
|
|
1616
|
+
const amount = Number.parseFloat(row.getValue('amount'));
|
|
1617
|
+
return new Intl.NumberFormat('de-DE', {
|
|
1618
|
+
style: 'currency',
|
|
1619
|
+
currency: 'EUR',
|
|
1620
|
+
}).format(amount);
|
|
1621
|
+
},
|
|
1622
|
+
size: 120,
|
|
1623
|
+
},
|
|
1624
|
+
];
|
|
1625
|
+
|
|
1626
|
+
const clickedRow = ref<Payment | null>(null);
|
|
1627
|
+
</script>
|
|
1628
|
+
|
|
1629
|
+
<template>
|
|
1630
|
+
<UiDataTable
|
|
1631
|
+
:data="data"
|
|
1632
|
+
:columns="columns"
|
|
1633
|
+
:on-row-click="(row) => (clickedRow = row)"
|
|
1634
|
+
/>
|
|
1635
|
+
<div v-if="clickedRow" class="mt-4 rounded border p-3 text-sm">
|
|
1636
|
+
Clicked: {{ clickedRow.email }} - {{ clickedRow.status }}
|
|
1637
|
+
</div>
|
|
1638
|
+
</template>`;
|
|
1639
|
+
|
|
1640
|
+
export const WithRowClick: Story = {
|
|
1641
|
+
args: {
|
|
1642
|
+
data: payments,
|
|
1643
|
+
columns: sortableColumns,
|
|
1644
|
+
},
|
|
1645
|
+
parameters: {
|
|
1646
|
+
docs: {
|
|
1647
|
+
source: {
|
|
1648
|
+
code: withRowClickTemplateSource,
|
|
1649
|
+
},
|
|
1650
|
+
},
|
|
1651
|
+
},
|
|
1652
|
+
render: (args) => ({
|
|
1653
|
+
components: { UiDataTable },
|
|
1654
|
+
setup() {
|
|
1655
|
+
const clickedRow = ref<Payment | null>(null);
|
|
1656
|
+
return {
|
|
1657
|
+
args,
|
|
1658
|
+
clickedRow,
|
|
1659
|
+
handleRowClick: (row: Payment) => {
|
|
1660
|
+
clickedRow.value = row;
|
|
1661
|
+
},
|
|
1662
|
+
};
|
|
1663
|
+
},
|
|
1664
|
+
template: `
|
|
1665
|
+
<div>
|
|
1666
|
+
<UiDataTable v-bind="args" :on-row-click="handleRowClick" />
|
|
1667
|
+
<div v-if="clickedRow" class="mt-4 rounded border p-3 text-sm">
|
|
1668
|
+
Clicked: <strong>{{ clickedRow.email }}</strong> - {{ clickedRow.status }} - {{ new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(clickedRow.amount) }}
|
|
1669
|
+
</div>
|
|
1670
|
+
<div v-else class="text-muted-foreground mt-4 text-sm">Click a row to see its data</div>
|
|
1671
|
+
</div>
|
|
1672
|
+
`,
|
|
1673
|
+
}),
|
|
1674
|
+
};
|
|
@@ -22,6 +22,9 @@
|
|
|
22
22
|
import type { UiDataTableProps } from './types';
|
|
23
23
|
import { DATA_TABLE_LABELS, DEFAULT_COLUMN_SIZE } from './constants';
|
|
24
24
|
|
|
25
|
+
const INTERACTIVE_SELECTOR =
|
|
26
|
+
'button, a, input, textarea, select, [role="button"], [role="link"], [role="menuitem"], [role="checkbox"], [role="switch"], [contenteditable="true"]';
|
|
27
|
+
|
|
25
28
|
defineOptions({
|
|
26
29
|
name: 'UiDataTable',
|
|
27
30
|
});
|
|
@@ -68,6 +71,14 @@
|
|
|
68
71
|
},
|
|
69
72
|
});
|
|
70
73
|
|
|
74
|
+
function handleRowClick(event: MouseEvent | KeyboardEvent, original: TData) {
|
|
75
|
+
if (!props.onRowClick) return;
|
|
76
|
+
const target = event.target as HTMLElement;
|
|
77
|
+
if (target.closest?.(INTERACTIVE_SELECTOR)) return;
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
props.onRowClick(original);
|
|
80
|
+
}
|
|
81
|
+
|
|
71
82
|
defineExpose({
|
|
72
83
|
table,
|
|
73
84
|
});
|
|
@@ -102,6 +113,7 @@
|
|
|
102
113
|
v-for="row in table.getRowModel().rows"
|
|
103
114
|
:key="row.id"
|
|
104
115
|
:data-state="row.getIsSelected() ? 'selected' : undefined"
|
|
116
|
+
:class="props.onRowClick ? 'cursor-pointer' : undefined"
|
|
105
117
|
>
|
|
106
118
|
<UiTableCell
|
|
107
119
|
v-for="cell in row.getVisibleCells()"
|
|
@@ -111,6 +123,9 @@
|
|
|
111
123
|
? { width: `${cell.column.getSize()}px` }
|
|
112
124
|
: undefined
|
|
113
125
|
"
|
|
126
|
+
@click="handleRowClick($event, row.original)"
|
|
127
|
+
@keydown.enter="handleRowClick($event, row.original)"
|
|
128
|
+
@keydown.space="handleRowClick($event, row.original)"
|
|
114
129
|
>
|
|
115
130
|
<FlexRender :render="cell.column.columnDef.cell" :props="cell.getContext()" />
|
|
116
131
|
</UiTableCell>
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
<script setup lang="ts" generic="TData, TValue">
|
|
2
2
|
import { UiButton } from '@/components/UiButton';
|
|
3
3
|
import { UiIcon } from '@/components/UiIcon';
|
|
4
|
-
import {
|
|
5
|
-
UiDropdownMenu,
|
|
6
|
-
UiDropdownMenuContent,
|
|
7
|
-
UiDropdownMenuItem,
|
|
8
|
-
UiDropdownMenuTrigger,
|
|
9
|
-
} from '@/components/UiDropdownMenu';
|
|
10
4
|
import { cn } from '@/lib/utils';
|
|
11
5
|
import { computed } from 'vue';
|
|
12
6
|
import { DATA_TABLE_LABELS } from './constants';
|
|
@@ -24,8 +18,6 @@
|
|
|
24
18
|
const props = defineProps<UiDataTableColumnHeaderProps<TData, TValue>>();
|
|
25
19
|
|
|
26
20
|
const labels = computed(() => ({
|
|
27
|
-
sortAscending: props.labels?.sortAscending ?? DATA_TABLE_LABELS.sortAscending,
|
|
28
|
-
sortDescending: props.labels?.sortDescending ?? DATA_TABLE_LABELS.sortDescending,
|
|
29
21
|
sortedAscending: props.labels?.sortedAscending ?? DATA_TABLE_LABELS.sortedAscending,
|
|
30
22
|
sortedDescending: props.labels?.sortedDescending ?? DATA_TABLE_LABELS.sortedDescending,
|
|
31
23
|
clickToSort: props.labels?.clickToSort ?? DATA_TABLE_LABELS.clickToSort,
|
|
@@ -37,57 +29,39 @@
|
|
|
37
29
|
if (sortState === 'desc') return `${props.title}, ${labels.value.sortedDescending}`;
|
|
38
30
|
return `${props.title}, ${labels.value.clickToSort}`;
|
|
39
31
|
});
|
|
32
|
+
|
|
33
|
+
function handleSort() {
|
|
34
|
+
const current = props.column.getIsSorted();
|
|
35
|
+
if (current === false) {
|
|
36
|
+
props.column.toggleSorting(false); // -> asc
|
|
37
|
+
} else if (current === 'asc') {
|
|
38
|
+
props.column.toggleSorting(true); // -> desc
|
|
39
|
+
} else {
|
|
40
|
+
props.column.clearSorting(); // -> unsorted
|
|
41
|
+
}
|
|
42
|
+
}
|
|
40
43
|
</script>
|
|
41
44
|
|
|
42
45
|
<template>
|
|
43
46
|
<div v-if="column.getCanSort()" :class="cn('flex items-center space-x-2', $attrs.class ?? '')">
|
|
44
|
-
<
|
|
45
|
-
<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
name="arrow-up"
|
|
63
|
-
:size="16"
|
|
64
|
-
class="ml-2"
|
|
65
|
-
aria-hidden="true"
|
|
66
|
-
/>
|
|
67
|
-
<UiIcon v-else name="chevrons-up-down" :size="16" class="ml-2" aria-hidden="true" />
|
|
68
|
-
</UiButton>
|
|
69
|
-
</UiDropdownMenuTrigger>
|
|
70
|
-
<UiDropdownMenuContent align="start">
|
|
71
|
-
<UiDropdownMenuItem @click="column.toggleSorting(false)">
|
|
72
|
-
<UiIcon
|
|
73
|
-
name="arrow-up"
|
|
74
|
-
:size="14"
|
|
75
|
-
class="mr-2 text-muted-foreground/70"
|
|
76
|
-
aria-hidden="true"
|
|
77
|
-
/>
|
|
78
|
-
{{ labels.sortAscending }}
|
|
79
|
-
</UiDropdownMenuItem>
|
|
80
|
-
<UiDropdownMenuItem @click="column.toggleSorting(true)">
|
|
81
|
-
<UiIcon
|
|
82
|
-
name="arrow-down"
|
|
83
|
-
:size="14"
|
|
84
|
-
class="mr-2 text-muted-foreground/70"
|
|
85
|
-
aria-hidden="true"
|
|
86
|
-
/>
|
|
87
|
-
{{ labels.sortDescending }}
|
|
88
|
-
</UiDropdownMenuItem>
|
|
89
|
-
</UiDropdownMenuContent>
|
|
90
|
-
</UiDropdownMenu>
|
|
47
|
+
<UiButton variant="ghost" size="sm" class="h-8" :aria-label="sortLabel" @click="handleSort">
|
|
48
|
+
<span>{{ title }}</span>
|
|
49
|
+
<UiIcon
|
|
50
|
+
v-if="column.getIsSorted() === 'desc'"
|
|
51
|
+
name="arrow-down"
|
|
52
|
+
:size="16"
|
|
53
|
+
class="ml-2"
|
|
54
|
+
aria-hidden="true"
|
|
55
|
+
/>
|
|
56
|
+
<UiIcon
|
|
57
|
+
v-else-if="column.getIsSorted() === 'asc'"
|
|
58
|
+
name="arrow-up"
|
|
59
|
+
:size="16"
|
|
60
|
+
class="ml-2"
|
|
61
|
+
aria-hidden="true"
|
|
62
|
+
/>
|
|
63
|
+
<UiIcon v-else name="chevrons-up-down" :size="16" class="ml-2" aria-hidden="true" />
|
|
64
|
+
</UiButton>
|
|
91
65
|
</div>
|
|
92
66
|
|
|
93
67
|
<div v-else :class="$attrs.class">
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { render, within } from '@testing-library/vue';
|
|
1
|
+
import { fireEvent, render, within } from '@testing-library/vue';
|
|
2
2
|
import userEvent from '@testing-library/user-event';
|
|
3
|
-
import { describe, expect, test } from 'vitest';
|
|
3
|
+
import { describe, expect, test, vi } from 'vitest';
|
|
4
4
|
import { h, ref, nextTick } from 'vue';
|
|
5
5
|
import type { ColumnDef } from '@tanstack/vue-table';
|
|
6
6
|
import UiDataTable from '../UiDataTable.vue';
|
|
@@ -137,35 +137,117 @@ describe('UiDataTable', () => {
|
|
|
137
137
|
});
|
|
138
138
|
|
|
139
139
|
describe('Sorting', () => {
|
|
140
|
-
test('sorts
|
|
140
|
+
test('sorts ascending on first click', async () => {
|
|
141
141
|
const user = userEvent.setup();
|
|
142
142
|
const { getByRole, getAllByRole } = render(UiDataTable, {
|
|
143
143
|
props: { columns: sortableColumns, data: testData },
|
|
144
144
|
});
|
|
145
145
|
|
|
146
|
-
// Click the Name column header button to
|
|
146
|
+
// Click the Name column header button to sort ascending
|
|
147
147
|
await user.click(getByRole('button', { name: /Name/i }));
|
|
148
|
-
// Click ascending option
|
|
149
|
-
await user.click(getByRole('menuitem', { name: /Asc/i }));
|
|
150
148
|
|
|
151
149
|
const cells = getAllByRole('cell');
|
|
152
150
|
// First data row should be Alice (alphabetically first)
|
|
153
151
|
expect(cells[0]).toHaveTextContent('Alice');
|
|
154
152
|
});
|
|
155
153
|
|
|
156
|
-
test('sorts descending
|
|
154
|
+
test('sorts descending on second click', async () => {
|
|
157
155
|
const user = userEvent.setup();
|
|
158
156
|
const { getByRole, getAllByRole } = render(UiDataTable, {
|
|
159
157
|
props: { columns: sortableColumns, data: testData },
|
|
160
158
|
});
|
|
161
159
|
|
|
160
|
+
// First click: ascending
|
|
161
|
+
await user.click(getByRole('button', { name: /Name/i }));
|
|
162
|
+
// Second click: descending
|
|
162
163
|
await user.click(getByRole('button', { name: /Name/i }));
|
|
163
|
-
await user.click(getByRole('menuitem', { name: /Desc/i }));
|
|
164
164
|
|
|
165
165
|
const cells = getAllByRole('cell');
|
|
166
166
|
// First data row should be Charlie (alphabetically last)
|
|
167
167
|
expect(cells[0]).toHaveTextContent('Charlie');
|
|
168
168
|
});
|
|
169
|
+
|
|
170
|
+
test('clears sorting on third click', async () => {
|
|
171
|
+
const user = userEvent.setup();
|
|
172
|
+
const { getByRole, getAllByRole } = render(UiDataTable, {
|
|
173
|
+
props: { columns: sortableColumns, data: testData },
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const initialCells = getAllByRole('cell');
|
|
177
|
+
const initialFirst = initialCells[0].textContent;
|
|
178
|
+
|
|
179
|
+
const headerButton = getByRole('button', { name: /Name/i });
|
|
180
|
+
// First click: ascending
|
|
181
|
+
await user.click(headerButton);
|
|
182
|
+
// Second click: descending
|
|
183
|
+
await user.click(headerButton);
|
|
184
|
+
// Third click: clear sorting (back to original order)
|
|
185
|
+
await user.click(headerButton);
|
|
186
|
+
|
|
187
|
+
const finalCells = getAllByRole('cell');
|
|
188
|
+
expect(finalCells[0]).toHaveTextContent(initialFirst ?? '');
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe('Row Click', () => {
|
|
193
|
+
test('calls onRowClick with row data when a row is clicked', async () => {
|
|
194
|
+
const onRowClick = vi.fn();
|
|
195
|
+
const { getByText } = render(UiDataTable, {
|
|
196
|
+
props: { columns: basicColumns, data: testData, onRowClick },
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// Click cell content - handler is on <td>, event bubbles from text
|
|
200
|
+
await fireEvent.click(getByText('Alice'));
|
|
201
|
+
|
|
202
|
+
expect(onRowClick).toHaveBeenCalledWith(testData[0]);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
test('adds cursor-pointer class when onRowClick is provided', () => {
|
|
206
|
+
const { container } = render(UiDataTable, {
|
|
207
|
+
props: { columns: basicColumns, data: testData, onRowClick: () => {} },
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const dataRows = container.querySelectorAll('tbody tr');
|
|
211
|
+
expect(dataRows[0]).toHaveClass('cursor-pointer');
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test('does not add cursor-pointer when onRowClick is not provided', () => {
|
|
215
|
+
const { container } = render(UiDataTable, {
|
|
216
|
+
props: { columns: basicColumns, data: testData },
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const dataRows = container.querySelectorAll('tbody tr');
|
|
220
|
+
expect(dataRows[0]).not.toHaveClass('cursor-pointer');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('does not fire onRowClick when clicking interactive elements inside a row', async () => {
|
|
224
|
+
const user = userEvent.setup();
|
|
225
|
+
const onRowClick = vi.fn();
|
|
226
|
+
const { getByLabelText } = render({
|
|
227
|
+
components: { UiDataTable },
|
|
228
|
+
template: '<UiDataTable :columns="columns" :data="data" :on-row-click="onRowClick" />',
|
|
229
|
+
setup() {
|
|
230
|
+
return { columns: selectableColumns, data: testData, onRowClick };
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
await nextTick();
|
|
235
|
+
|
|
236
|
+
// Click the checkbox (interactive element) - should NOT trigger onRowClick
|
|
237
|
+
await user.click(getByLabelText('Select row Alice'));
|
|
238
|
+
expect(onRowClick).not.toHaveBeenCalled();
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test('activates onRowClick via keyboard on cell', async () => {
|
|
242
|
+
const onRowClick = vi.fn();
|
|
243
|
+
const { getByText } = render(UiDataTable, {
|
|
244
|
+
props: { columns: basicColumns, data: testData, onRowClick },
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
await fireEvent.keyDown(getByText('Alice').closest('td')!, { key: 'Enter' });
|
|
248
|
+
|
|
249
|
+
expect(onRowClick).toHaveBeenCalledWith(testData[0]);
|
|
250
|
+
});
|
|
169
251
|
});
|
|
170
252
|
|
|
171
253
|
describe('Filtering', () => {
|