@aleph-alpha/ui-library 7.1.0 → 7.2.1
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/components/UiPagination/UiPagination.vue.d.ts +19 -0
- package/dist/system/components/UiPagination/constants.d.ts +6 -0
- package/dist/system/components/UiPagination/index.d.ts +3 -0
- package/dist/system/components/UiPagination/types.d.ts +81 -0
- package/dist/system/components/core/pagination/Pagination.vue.d.ts +29 -0
- package/dist/system/components/core/pagination/PaginationContent.vue.d.ts +29 -0
- package/dist/system/components/core/pagination/PaginationEllipsis.vue.d.ts +22 -0
- package/dist/system/components/core/pagination/PaginationFirst.vue.d.ts +22 -0
- package/dist/system/components/core/pagination/PaginationItem.vue.d.ts +26 -0
- package/dist/system/components/core/pagination/PaginationLast.vue.d.ts +22 -0
- package/dist/system/components/core/pagination/PaginationNext.vue.d.ts +22 -0
- package/dist/system/components/core/pagination/PaginationPrevious.vue.d.ts +22 -0
- package/dist/system/components/core/pagination/index.d.ts +8 -0
- package/dist/system/components/index.d.ts +1 -0
- package/dist/system/icons/story-sidebar-icons.json +4 -0
- package/dist/system/lib.js +11605 -10906
- package/dist/system/patterns/UiBulkAction/UiBulkAction.vue.d.ts +43 -0
- package/dist/system/patterns/UiBulkAction/index.d.ts +3 -0
- package/dist/system/patterns/UiBulkAction/types.d.ts +109 -0
- package/dist/system/patterns/UiBulkAction/useBulkAction.d.ts +7 -0
- package/dist/system/patterns/UiDataTable/constants.d.ts +10 -0
- package/dist/system/patterns/index.d.ts +1 -0
- package/dist/system/story-sidebar-icons.json +4 -0
- package/package.json +10 -5
- package/src/components/UiPagination/UiPagination.guidance.md +60 -0
- package/src/components/UiPagination/UiPagination.stories.ts +1070 -0
- package/src/components/UiPagination/UiPagination.vue +109 -0
- package/src/components/UiPagination/__tests__/UiPagination.test.ts +203 -0
- package/src/components/UiPagination/constants.ts +17 -0
- package/src/components/UiPagination/index.ts +3 -0
- package/src/components/UiPagination/types.ts +82 -0
- package/src/components/core/pagination/Pagination.vue +26 -0
- package/src/components/core/pagination/PaginationContent.vue +24 -0
- package/src/components/core/pagination/PaginationEllipsis.vue +30 -0
- package/src/components/core/pagination/PaginationFirst.vue +30 -0
- package/src/components/core/pagination/PaginationItem.vue +39 -0
- package/src/components/core/pagination/PaginationLast.vue +30 -0
- package/src/components/core/pagination/PaginationNext.vue +35 -0
- package/src/components/core/pagination/PaginationPrevious.vue +35 -0
- package/src/components/core/pagination/index.ts +8 -0
- package/src/components/index.ts +1 -0
- package/src/patterns/UiBulkAction/UiBulkAction.guidance.md +59 -0
- package/src/patterns/UiBulkAction/UiBulkAction.stories.ts +2238 -0
- package/src/patterns/UiBulkAction/UiBulkAction.vue +62 -0
- package/src/patterns/UiBulkAction/__tests__/useBulkAction.test.ts +78 -0
- package/src/patterns/UiBulkAction/index.ts +3 -0
- package/src/patterns/UiBulkAction/types.ts +126 -0
- package/src/patterns/UiBulkAction/useBulkAction.ts +50 -0
- package/src/patterns/UiDataTable/UiDataTable.stories.ts +375 -1
- package/src/patterns/UiDataTable/UiDataTablePagination.vue +43 -60
- package/src/patterns/UiDataTable/__tests__/UiDataTablePagination.test.ts +58 -29
- package/src/patterns/UiDataTable/constants.ts +10 -0
- package/src/patterns/index.ts +1 -0
|
@@ -0,0 +1,2238 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/vue3-vite';
|
|
2
|
+
import { expect, fn, userEvent, waitFor, within } from 'storybook/test';
|
|
3
|
+
import type { ColumnDef, Row } from '@aleph-alpha/ui-library';
|
|
4
|
+
import { computed, h, reactive, ref, watch } from 'vue';
|
|
5
|
+
import { UiBulkAction, useBulkAction } from './index';
|
|
6
|
+
import {
|
|
7
|
+
UiDataTable,
|
|
8
|
+
UiDataTableColumnFilter,
|
|
9
|
+
UiDataTableFilterMenu,
|
|
10
|
+
UiDataTableColumnHeader,
|
|
11
|
+
UiDataTablePagination,
|
|
12
|
+
UiDataTableToolbar,
|
|
13
|
+
} from '../UiDataTable';
|
|
14
|
+
import {
|
|
15
|
+
payments,
|
|
16
|
+
paymentsSmall,
|
|
17
|
+
type Payment,
|
|
18
|
+
} from '../UiDataTable/UiDataTable.mock';
|
|
19
|
+
import {
|
|
20
|
+
UiAlert,
|
|
21
|
+
UiAlertDescription,
|
|
22
|
+
UiAlertTitle,
|
|
23
|
+
} from '@/components/UiAlert';
|
|
24
|
+
import {
|
|
25
|
+
UiAlertDialog,
|
|
26
|
+
UiAlertDialogAction,
|
|
27
|
+
UiAlertDialogCancel,
|
|
28
|
+
UiAlertDialogContent,
|
|
29
|
+
UiAlertDialogDescription,
|
|
30
|
+
UiAlertDialogFooter,
|
|
31
|
+
UiAlertDialogHeader,
|
|
32
|
+
UiAlertDialogTitle,
|
|
33
|
+
} from '@/components/UiAlertDialog';
|
|
34
|
+
import { UiBadge, type UiBadgeVariants } from '@/components/UiBadge';
|
|
35
|
+
import { UiButton } from '@/components/UiButton';
|
|
36
|
+
import { UiCheckbox } from '@/components/UiCheckbox';
|
|
37
|
+
import { UiCodeBlock } from '@/components/UiCodeBlock';
|
|
38
|
+
import {
|
|
39
|
+
UiDropdownMenu,
|
|
40
|
+
UiDropdownMenuContent,
|
|
41
|
+
UiDropdownMenuItem,
|
|
42
|
+
UiDropdownMenuSeparator,
|
|
43
|
+
UiDropdownMenuTrigger,
|
|
44
|
+
} from '@/components/UiDropdownMenu';
|
|
45
|
+
import {
|
|
46
|
+
UiField,
|
|
47
|
+
UiFieldContent,
|
|
48
|
+
UiFieldGroup,
|
|
49
|
+
UiFieldLabel,
|
|
50
|
+
} from '@/components/UiField';
|
|
51
|
+
import { UiIcon } from '@/components/UiIcon';
|
|
52
|
+
import { UiInput } from '@/components/UiInput';
|
|
53
|
+
import { UiSpinner } from '@/components/UiSpinner';
|
|
54
|
+
import { UiFloatActionItems } from '../UiFloatActions';
|
|
55
|
+
import {
|
|
56
|
+
UiTabs,
|
|
57
|
+
UiTabsContent,
|
|
58
|
+
UiTabsList,
|
|
59
|
+
UiTabsTrigger,
|
|
60
|
+
} from '@/components/UiTabs';
|
|
61
|
+
import { UiTooltip } from '@/components/UiTooltip';
|
|
62
|
+
import {
|
|
63
|
+
ArchiveIcon,
|
|
64
|
+
BanIcon,
|
|
65
|
+
ChevronDownIcon,
|
|
66
|
+
DeleteIcon,
|
|
67
|
+
DownloadIcon,
|
|
68
|
+
MoreHorizontalIcon,
|
|
69
|
+
Trash2Icon,
|
|
70
|
+
UploadIcon,
|
|
71
|
+
XIcon,
|
|
72
|
+
} from '@/icons/lucide';
|
|
73
|
+
import {
|
|
74
|
+
accessibilityStory,
|
|
75
|
+
designNotesStory,
|
|
76
|
+
guidanceLinks,
|
|
77
|
+
parseGuidance,
|
|
78
|
+
} from '../../../.storybook/guidance';
|
|
79
|
+
import guidanceMarkdown from './UiBulkAction.guidance.md?raw';
|
|
80
|
+
|
|
81
|
+
const guidance = parseGuidance(guidanceMarkdown);
|
|
82
|
+
|
|
83
|
+
const meta = {
|
|
84
|
+
title: 'Patterns/UiBulkAction',
|
|
85
|
+
component: UiBulkAction,
|
|
86
|
+
tags: ['autodocs'],
|
|
87
|
+
parameters: {
|
|
88
|
+
docs: {
|
|
89
|
+
links: guidanceLinks(guidance),
|
|
90
|
+
description: {
|
|
91
|
+
component:
|
|
92
|
+
'A toolbar of actions applied to every selected item at once. The select-all checkbox always comes first and the reset button always comes last; custom actions go in the default slot between them. Pair `UiBulkAction` with the `useBulkAction` composable, which holds the selection and runs the actions.',
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
argTypes: {
|
|
97
|
+
checked: {
|
|
98
|
+
control: 'select',
|
|
99
|
+
options: [false, true, 'indeterminate'],
|
|
100
|
+
description:
|
|
101
|
+
'State of the select-all checkbox (`v-model:checked`). Use `indeterminate` when only some rows are selected.',
|
|
102
|
+
},
|
|
103
|
+
selectedCount: {
|
|
104
|
+
control: { type: 'number', min: 0 },
|
|
105
|
+
description:
|
|
106
|
+
'Number of selected items. Exposed back to the default slot as `selectedCount`.',
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
args: {
|
|
110
|
+
checked: false,
|
|
111
|
+
selectedCount: 0,
|
|
112
|
+
onReset: fn(),
|
|
113
|
+
},
|
|
114
|
+
} satisfies Meta<typeof UiBulkAction>;
|
|
115
|
+
|
|
116
|
+
export default meta;
|
|
117
|
+
type Story = StoryObj<typeof meta>;
|
|
118
|
+
|
|
119
|
+
const formatAmount = (amount: number) =>
|
|
120
|
+
new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(
|
|
121
|
+
amount,
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Usage, guidelines, and do's and don'ts for bulk actions.
|
|
126
|
+
*/
|
|
127
|
+
export const DesignNotes: Story = {
|
|
128
|
+
...designNotesStory(guidance),
|
|
129
|
+
tags: ['!autodocs'],
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const a11yPreviewData: Payment[] = [
|
|
133
|
+
{
|
|
134
|
+
id: '1',
|
|
135
|
+
amount: 630.44,
|
|
136
|
+
status: 'success',
|
|
137
|
+
email: 'michael.mitc@example.com',
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
id: '2',
|
|
141
|
+
amount: 767.5,
|
|
142
|
+
status: 'success',
|
|
143
|
+
email: 'felicia.reid@example.com',
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: '3',
|
|
147
|
+
amount: 396.84,
|
|
148
|
+
status: 'processing',
|
|
149
|
+
email: 'georgia.young@example.com',
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
const PREVIEW_SELECTED_ROWS = 2;
|
|
154
|
+
|
|
155
|
+
const a11yPreviewColumns: ColumnDef<Payment>[] = [
|
|
156
|
+
{
|
|
157
|
+
id: 'select',
|
|
158
|
+
header: () => h('span', { class: 'sr-only' }, 'Select'),
|
|
159
|
+
cell: ({ row }) =>
|
|
160
|
+
h(UiCheckbox, {
|
|
161
|
+
modelValue: row.index < PREVIEW_SELECTED_ROWS,
|
|
162
|
+
'aria-label': `Select ${row.original.email}`,
|
|
163
|
+
name: `a11y-select-${row.id}`,
|
|
164
|
+
}),
|
|
165
|
+
size: 40,
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
accessorKey: 'status',
|
|
169
|
+
header: 'Status',
|
|
170
|
+
cell: ({ row }) =>
|
|
171
|
+
h('div', { class: 'capitalize' }, row.getValue('status')),
|
|
172
|
+
},
|
|
173
|
+
{ accessorKey: 'email', header: 'Email' },
|
|
174
|
+
{
|
|
175
|
+
accessorKey: 'amount',
|
|
176
|
+
header: () => h('div', { class: 'text-right' }, 'Amount'),
|
|
177
|
+
cell: ({ row }) =>
|
|
178
|
+
h(
|
|
179
|
+
'div',
|
|
180
|
+
{ class: 'text-right font-medium' },
|
|
181
|
+
formatAmount(Number(row.getValue('amount'))),
|
|
182
|
+
),
|
|
183
|
+
},
|
|
184
|
+
];
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Accessibility requirements for bulk actions, with a preview of the
|
|
188
|
+
* Floating Action in its selected state above a Data Table.
|
|
189
|
+
*/
|
|
190
|
+
export const Accessibility: Story = {
|
|
191
|
+
...accessibilityStory({
|
|
192
|
+
requirements: guidance.accessibility,
|
|
193
|
+
implementation: [
|
|
194
|
+
'UiBulkAction renders a role="toolbar" named by ariaLabel (default "Bulk actions"); Arrow keys, Home and End move between its actions',
|
|
195
|
+
'The select-all checkbox (selectAllLabel) and the reset button (resetLabel) are built in and always first and last, so focus order matches the visual order',
|
|
196
|
+
'Wrap the selected count in an element with role="status" so changes are announced',
|
|
197
|
+
'Give icon-only items an aria-label and wrap them in UiTooltip',
|
|
198
|
+
'Bind the select-all checkbox to "indeterminate" when only some rows on the page are selected',
|
|
199
|
+
'The select-all checkbox sits in a 32px label, so the whole cell is a touch target (WCAG 2.5.8)',
|
|
200
|
+
'When an action does not apply to the selection, keep it focusable and put the reason in its accessible name, for example "Success, unavailable: 2 failed payments selected"',
|
|
201
|
+
'Announce the result of apply, for example in a toast or a role="status" message, not only visually',
|
|
202
|
+
],
|
|
203
|
+
example: {
|
|
204
|
+
components: { UiBulkAction, UiDataTable, UiFloatActionItems },
|
|
205
|
+
setup: () => ({
|
|
206
|
+
columns: a11yPreviewColumns,
|
|
207
|
+
data: a11yPreviewData,
|
|
208
|
+
selectedCount: PREVIEW_SELECTED_ROWS,
|
|
209
|
+
MoreHorizontalIcon,
|
|
210
|
+
Trash2Icon,
|
|
211
|
+
}),
|
|
212
|
+
template: `
|
|
213
|
+
<div class="flex w-full flex-col gap-3">
|
|
214
|
+
<UiBulkAction checked="indeterminate" :selected-count="selectedCount" v-slot="{ selectedCount }">
|
|
215
|
+
<span role="status">
|
|
216
|
+
<UiFloatActionItems type="text">{{ selectedCount }} selected</UiFloatActionItems>
|
|
217
|
+
</span>
|
|
218
|
+
<UiFloatActionItems type="separator" />
|
|
219
|
+
<UiFloatActionItems type="label">Export</UiFloatActionItems>
|
|
220
|
+
<UiFloatActionItems type="label">Assign</UiFloatActionItems>
|
|
221
|
+
<UiFloatActionItems type="icon" :icon="MoreHorizontalIcon" aria-label="More actions" />
|
|
222
|
+
<UiFloatActionItems type="separator" />
|
|
223
|
+
<UiFloatActionItems type="label" :prefix-icon="Trash2Icon">Delete</UiFloatActionItems>
|
|
224
|
+
</UiBulkAction>
|
|
225
|
+
<UiDataTable :columns="columns" :data="data" />
|
|
226
|
+
</div>
|
|
227
|
+
`,
|
|
228
|
+
},
|
|
229
|
+
}),
|
|
230
|
+
tags: ['!autodocs'],
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const defaultTemplateSource = `<script setup lang="ts">
|
|
234
|
+
import { ref } from 'vue';
|
|
235
|
+
import { UiBulkAction, UiFloatActionItems } from '@aleph-alpha/ui-library';
|
|
236
|
+
|
|
237
|
+
const checked = ref<boolean | 'indeterminate'>('indeterminate');
|
|
238
|
+
</script>
|
|
239
|
+
|
|
240
|
+
<template>
|
|
241
|
+
<!-- Checkbox first, reset last: custom actions go in between. -->
|
|
242
|
+
<UiBulkAction v-model:checked="checked" :selected-count="3" @reset="checked = false" v-slot="{ selectedCount }">
|
|
243
|
+
<UiFloatActionItems v-if="selectedCount" type="text">{{ selectedCount }} selected</UiFloatActionItems>
|
|
244
|
+
<UiFloatActionItems type="separator" />
|
|
245
|
+
<UiFloatActionItems type="label">Export</UiFloatActionItems>
|
|
246
|
+
<UiFloatActionItems type="label">Assign</UiFloatActionItems>
|
|
247
|
+
</UiBulkAction>
|
|
248
|
+
</template>`;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The bulk action bar on its own. The select-all checkbox is always first and
|
|
252
|
+
* reset is always last; custom actions are slotted between them. The
|
|
253
|
+
* `selectedCount` prop is passed back to the slot so actions can show it.
|
|
254
|
+
*/
|
|
255
|
+
export const Default: Story = {
|
|
256
|
+
parameters: {
|
|
257
|
+
docs: {
|
|
258
|
+
source: {
|
|
259
|
+
code: defaultTemplateSource,
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
render: (args) => ({
|
|
264
|
+
components: { UiBulkAction, UiFloatActionItems },
|
|
265
|
+
setup() {
|
|
266
|
+
const checked = ref(args.checked);
|
|
267
|
+
watch(
|
|
268
|
+
() => args.checked,
|
|
269
|
+
(value) => {
|
|
270
|
+
checked.value = value;
|
|
271
|
+
},
|
|
272
|
+
);
|
|
273
|
+
return { args, checked };
|
|
274
|
+
},
|
|
275
|
+
template: `
|
|
276
|
+
<UiBulkAction
|
|
277
|
+
v-model:checked="checked"
|
|
278
|
+
:selected-count="args.selectedCount"
|
|
279
|
+
@reset="args.onReset"
|
|
280
|
+
v-slot="{ selectedCount }"
|
|
281
|
+
>
|
|
282
|
+
<UiFloatActionItems v-if="selectedCount" type="text">{{ selectedCount }} selected</UiFloatActionItems>
|
|
283
|
+
<UiFloatActionItems type="separator" />
|
|
284
|
+
<UiFloatActionItems type="label">Export</UiFloatActionItems>
|
|
285
|
+
<UiFloatActionItems type="label">Assign</UiFloatActionItems>
|
|
286
|
+
</UiBulkAction>
|
|
287
|
+
`,
|
|
288
|
+
}),
|
|
289
|
+
play: async ({ args, canvasElement, step }) => {
|
|
290
|
+
// Scenario: the built-in controls wire up — the checkbox drives
|
|
291
|
+
// `v-model:checked` and the reset button emits `reset`.
|
|
292
|
+
const canvas = within(canvasElement);
|
|
293
|
+
const selectAll = canvas.getByRole('checkbox', {
|
|
294
|
+
name: 'Select all rows on this page',
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
await step(
|
|
298
|
+
'interaction: the select-all checkbox toggles the checked state',
|
|
299
|
+
async () => {
|
|
300
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'false');
|
|
301
|
+
await userEvent.click(selectAll);
|
|
302
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'true');
|
|
303
|
+
await userEvent.click(selectAll);
|
|
304
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'false');
|
|
305
|
+
},
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
await step('interaction: the reset button emits reset', async () => {
|
|
309
|
+
await userEvent.click(
|
|
310
|
+
canvas.getByRole('button', { name: 'Reset selection' }),
|
|
311
|
+
);
|
|
312
|
+
await expect(args.onReset).toHaveBeenCalledTimes(1);
|
|
313
|
+
});
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const STATUS_TABS: { value: 'all' | Payment['status']; label: string }[] = [
|
|
318
|
+
{ value: 'all', label: 'All' },
|
|
319
|
+
{ value: 'success', label: 'Success' },
|
|
320
|
+
{ value: 'processing', label: 'Processing' },
|
|
321
|
+
{ value: 'pending', label: 'Pending' },
|
|
322
|
+
{ value: 'failed', label: 'Failed' },
|
|
323
|
+
];
|
|
324
|
+
|
|
325
|
+
const onExport = fn().mockName('export');
|
|
326
|
+
const onArchive = fn().mockName('archive');
|
|
327
|
+
const onDelete = fn().mockName('delete');
|
|
328
|
+
|
|
329
|
+
const countByStatus = (
|
|
330
|
+
data: Payment[],
|
|
331
|
+
status: (typeof STATUS_TABS)[number]['value'],
|
|
332
|
+
) =>
|
|
333
|
+
status === 'all'
|
|
334
|
+
? data.length
|
|
335
|
+
: data.filter((item) => item.status === status).length;
|
|
336
|
+
|
|
337
|
+
const tableColumns: ColumnDef<Payment>[] = [
|
|
338
|
+
{
|
|
339
|
+
id: 'select',
|
|
340
|
+
header: () => h('span', { class: 'sr-only' }, 'Select'),
|
|
341
|
+
cell: ({ row }) =>
|
|
342
|
+
h(UiCheckbox, {
|
|
343
|
+
modelValue: row.getIsSelected(),
|
|
344
|
+
'onUpdate:modelValue': (value: boolean) => row.toggleSelected(!!value),
|
|
345
|
+
'aria-label': `Select ${row.original.email}`,
|
|
346
|
+
name: `select-${row.id}`,
|
|
347
|
+
}),
|
|
348
|
+
enableSorting: false,
|
|
349
|
+
enableHiding: false,
|
|
350
|
+
size: 40,
|
|
351
|
+
},
|
|
352
|
+
{
|
|
353
|
+
accessorKey: 'status',
|
|
354
|
+
meta: { label: 'Status' },
|
|
355
|
+
header: ({ column }) =>
|
|
356
|
+
h(UiDataTableColumnHeader, { column, title: 'Status' }),
|
|
357
|
+
cell: ({ row }) =>
|
|
358
|
+
h('div', { class: 'capitalize' }, row.getValue('status')),
|
|
359
|
+
size: 120,
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
accessorKey: 'email',
|
|
363
|
+
meta: { label: 'Email' },
|
|
364
|
+
header: ({ column }) =>
|
|
365
|
+
h(UiDataTableColumnHeader, { column, title: 'Email' }),
|
|
366
|
+
cell: ({ row }) => h('div', { class: 'lowercase' }, row.getValue('email')),
|
|
367
|
+
size: 250,
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
accessorKey: 'amount',
|
|
371
|
+
meta: { label: 'Amount' },
|
|
372
|
+
header: ({ column }) =>
|
|
373
|
+
h(UiDataTableColumnHeader, {
|
|
374
|
+
column,
|
|
375
|
+
title: 'Amount',
|
|
376
|
+
class: 'justify-end',
|
|
377
|
+
}),
|
|
378
|
+
cell: ({ row }) =>
|
|
379
|
+
h(
|
|
380
|
+
'div',
|
|
381
|
+
{ class: 'text-right font-medium' },
|
|
382
|
+
formatAmount(Number(row.getValue('amount'))),
|
|
383
|
+
),
|
|
384
|
+
filterFn: (row: Row<Payment>, columnId, value: number | undefined) =>
|
|
385
|
+
value === undefined || Number(row.getValue(columnId)) >= value,
|
|
386
|
+
size: 120,
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
id: 'actions',
|
|
390
|
+
header: () => h('span', { class: 'sr-only' }, 'Actions'),
|
|
391
|
+
cell: ({ row }) =>
|
|
392
|
+
h('div', { class: 'flex items-center justify-end gap-1' }, [
|
|
393
|
+
h(UiDropdownMenu, null, {
|
|
394
|
+
default: () => [
|
|
395
|
+
h(UiDropdownMenuTrigger, { asChild: true }, () =>
|
|
396
|
+
h(
|
|
397
|
+
UiButton,
|
|
398
|
+
{
|
|
399
|
+
variant: 'ghost',
|
|
400
|
+
size: 'icon-sm',
|
|
401
|
+
'aria-label': `Actions for ${row.original.email}`,
|
|
402
|
+
},
|
|
403
|
+
() => h(UiIcon, { name: MoreHorizontalIcon, size: 16 }),
|
|
404
|
+
),
|
|
405
|
+
),
|
|
406
|
+
h(UiDropdownMenuContent, { align: 'end' }, () => [
|
|
407
|
+
h(UiDropdownMenuItem, null, () => 'View details'),
|
|
408
|
+
h(UiDropdownMenuItem, null, () => 'View customer'),
|
|
409
|
+
]),
|
|
410
|
+
],
|
|
411
|
+
}),
|
|
412
|
+
h(
|
|
413
|
+
UiButton,
|
|
414
|
+
{
|
|
415
|
+
variant: 'ghost',
|
|
416
|
+
size: 'icon-sm',
|
|
417
|
+
'aria-label': row.getIsExpanded() ? 'Collapse row' : 'Expand row',
|
|
418
|
+
'aria-expanded': row.getIsExpanded(),
|
|
419
|
+
onClick: () => row.toggleExpanded(),
|
|
420
|
+
},
|
|
421
|
+
() =>
|
|
422
|
+
h(UiIcon, {
|
|
423
|
+
name: ChevronDownIcon,
|
|
424
|
+
size: 16,
|
|
425
|
+
class: row.getIsExpanded() ? 'rotate-180' : undefined,
|
|
426
|
+
}),
|
|
427
|
+
),
|
|
428
|
+
]),
|
|
429
|
+
enableSorting: false,
|
|
430
|
+
enableHiding: false,
|
|
431
|
+
size: 80,
|
|
432
|
+
},
|
|
433
|
+
];
|
|
434
|
+
|
|
435
|
+
const withDataTableTemplateSource = `<script setup lang="ts">
|
|
436
|
+
import { computed, h, reactive, ref, watch } from 'vue';
|
|
437
|
+
import {
|
|
438
|
+
UiBulkAction,
|
|
439
|
+
UiButton,
|
|
440
|
+
UiCheckbox,
|
|
441
|
+
UiDataTable,
|
|
442
|
+
UiDataTableColumnFilter,
|
|
443
|
+
UiDataTableColumnHeader,
|
|
444
|
+
UiDataTableFilterMenu,
|
|
445
|
+
UiDataTablePagination,
|
|
446
|
+
UiDataTableToolbar,
|
|
447
|
+
UiField,
|
|
448
|
+
UiFieldContent,
|
|
449
|
+
UiFieldGroup,
|
|
450
|
+
UiFieldLabel,
|
|
451
|
+
UiFloatActionItems,
|
|
452
|
+
UiInput,
|
|
453
|
+
UiTabs,
|
|
454
|
+
UiTabsList,
|
|
455
|
+
UiTabsTrigger,
|
|
456
|
+
useBulkAction,
|
|
457
|
+
} from '@aleph-alpha/ui-library';
|
|
458
|
+
import type { ColumnDef } from '@aleph-alpha/ui-library';
|
|
459
|
+
import {
|
|
460
|
+
ArchiveIcon,
|
|
461
|
+
DownloadIcon,
|
|
462
|
+
Trash2Icon,
|
|
463
|
+
XIcon,
|
|
464
|
+
} from '@aleph-alpha/ui-library/icons';
|
|
465
|
+
|
|
466
|
+
interface Payment {
|
|
467
|
+
id: string;
|
|
468
|
+
amount: number;
|
|
469
|
+
status: 'pending' | 'processing' | 'success' | 'failed';
|
|
470
|
+
email: string;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const props = defineProps<{ data: Payment[] }>();
|
|
474
|
+
|
|
475
|
+
// Selection column without a header checkbox: select-all lives in the bulk bar.
|
|
476
|
+
const columns: ColumnDef<Payment>[] = [
|
|
477
|
+
{
|
|
478
|
+
id: 'select',
|
|
479
|
+
header: () => h('span', { class: 'sr-only' }, 'Select'),
|
|
480
|
+
cell: ({ row }) => h(UiCheckbox, {
|
|
481
|
+
modelValue: row.getIsSelected(),
|
|
482
|
+
'onUpdate:modelValue': (value: boolean) => row.toggleSelected(!!value),
|
|
483
|
+
'aria-label': \`Select \${row.original.email}\`,
|
|
484
|
+
name: \`select-\${row.id}\`,
|
|
485
|
+
}),
|
|
486
|
+
enableSorting: false,
|
|
487
|
+
enableHiding: false,
|
|
488
|
+
},
|
|
489
|
+
{ accessorKey: 'status', meta: { label: 'Status' }, header: ({ column }) => h(UiDataTableColumnHeader, { column, title: 'Status' }) },
|
|
490
|
+
{ accessorKey: 'email', meta: { label: 'Email' }, header: ({ column }) => h(UiDataTableColumnHeader, { column, title: 'Email' }) },
|
|
491
|
+
{
|
|
492
|
+
accessorKey: 'amount',
|
|
493
|
+
meta: { label: 'Amount' },
|
|
494
|
+
header: ({ column }) => h(UiDataTableColumnHeader, { column, title: 'Amount', class: 'justify-end' }),
|
|
495
|
+
filterFn: (row, columnId, value?: number) => value === undefined || Number(row.getValue(columnId)) >= value,
|
|
496
|
+
},
|
|
497
|
+
];
|
|
498
|
+
|
|
499
|
+
const tableRef = ref<InstanceType<typeof UiDataTable>>();
|
|
500
|
+
const table = computed(() => tableRef.value?.table);
|
|
501
|
+
|
|
502
|
+
const { apply, reset, selectedItems, selectedCount, hasSelection } = useBulkAction<Payment>({
|
|
503
|
+
actions: [
|
|
504
|
+
{ key: 'export', action: (items) => console.log('Export', items) },
|
|
505
|
+
{ key: 'archive', action: (items) => console.log('Archive', items) },
|
|
506
|
+
{ key: 'delete', action: async (items) => { console.log('Delete', items); reset(); } },
|
|
507
|
+
],
|
|
508
|
+
onReset: () => table.value?.resetRowSelection(),
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
watch(
|
|
512
|
+
() => table.value?.getSelectedRowModel().rows,
|
|
513
|
+
(rows) => { selectedItems.value = rows?.map((row) => row.original) ?? []; },
|
|
514
|
+
);
|
|
515
|
+
|
|
516
|
+
// Tabs filter by status
|
|
517
|
+
const activeTab = ref('all');
|
|
518
|
+
const tabs = computed(() => [
|
|
519
|
+
{ value: 'all', label: 'All', count: props.data.length },
|
|
520
|
+
...['success', 'processing', 'pending', 'failed'].map((status) => ({
|
|
521
|
+
value: status,
|
|
522
|
+
label: status[0].toUpperCase() + status.slice(1),
|
|
523
|
+
count: props.data.filter((item) => item.status === status).length,
|
|
524
|
+
})),
|
|
525
|
+
]);
|
|
526
|
+
watch(activeTab, (value) => {
|
|
527
|
+
table.value?.getColumn('status')?.setFilterValue(value === 'all' ? undefined : value);
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
const search = computed({
|
|
531
|
+
get: () => (table.value?.getState().globalFilter as string) ?? '',
|
|
532
|
+
set: (value) => table.value?.setGlobalFilter(value),
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
const filterOpen = ref(false);
|
|
536
|
+
const draft = reactive({ email: '', minAmount: '' });
|
|
537
|
+
const activeFilterCount = computed(
|
|
538
|
+
() => table.value?.getState().columnFilters.filter((filter) => filter.id !== 'status').length ?? 0,
|
|
539
|
+
);
|
|
540
|
+
|
|
541
|
+
function applyFilters() {
|
|
542
|
+
table.value?.getColumn('email')?.setFilterValue(draft.email || undefined);
|
|
543
|
+
table.value?.getColumn('amount')?.setFilterValue(draft.minAmount ? Number(draft.minAmount) : undefined);
|
|
544
|
+
filterOpen.value = false;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function removeFilters() {
|
|
548
|
+
draft.email = '';
|
|
549
|
+
draft.minAmount = '';
|
|
550
|
+
applyFilters();
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const selectAllState = computed({
|
|
554
|
+
get: () => {
|
|
555
|
+
if (table.value?.getIsAllPageRowsSelected()) return true;
|
|
556
|
+
return table.value?.getIsSomePageRowsSelected() ? 'indeterminate' : false;
|
|
557
|
+
},
|
|
558
|
+
set: (value) => table.value?.toggleAllPageRowsSelected(!!value),
|
|
559
|
+
});
|
|
560
|
+
</script>
|
|
561
|
+
|
|
562
|
+
<template>
|
|
563
|
+
<div class="flex flex-col gap-4">
|
|
564
|
+
<UiDataTableToolbar>
|
|
565
|
+
<UiTabs v-model="activeTab">
|
|
566
|
+
<UiTabsList>
|
|
567
|
+
<UiTabsTrigger v-for="tab in tabs" :key="tab.value" :value="tab.value">
|
|
568
|
+
{{ tab.label }} ({{ tab.count }})
|
|
569
|
+
</UiTabsTrigger>
|
|
570
|
+
</UiTabsList>
|
|
571
|
+
</UiTabs>
|
|
572
|
+
<template #actions>
|
|
573
|
+
<UiInput v-model="search" name="bulk-search" placeholder="Search content..." aria-label="Search content" class="h-8 w-full sm:w-[250px]" />
|
|
574
|
+
<UiDataTableFilterMenu v-model:open="filterOpen" :active-count="activeFilterCount">
|
|
575
|
+
<form class="flex flex-col gap-5" @submit.prevent="applyFilters">
|
|
576
|
+
<UiFieldGroup>
|
|
577
|
+
<UiField>
|
|
578
|
+
<UiFieldLabel for="bulk-filter-email">Email</UiFieldLabel>
|
|
579
|
+
<UiFieldContent>
|
|
580
|
+
<UiInput id="bulk-filter-email" v-model="draft.email" name="email" placeholder="Contains..." />
|
|
581
|
+
</UiFieldContent>
|
|
582
|
+
</UiField>
|
|
583
|
+
<UiField>
|
|
584
|
+
<UiFieldLabel for="bulk-filter-amount">Minimum amount</UiFieldLabel>
|
|
585
|
+
<UiFieldContent>
|
|
586
|
+
<UiInput id="bulk-filter-amount" v-model="draft.minAmount" name="min-amount" type="number" placeholder="0" />
|
|
587
|
+
</UiFieldContent>
|
|
588
|
+
</UiField>
|
|
589
|
+
<div class="flex items-center gap-2">
|
|
590
|
+
<UiButton v-if="activeFilterCount" variant="outline" :end-icon="XIcon" type="button" @click="removeFilters">
|
|
591
|
+
Remove filters
|
|
592
|
+
</UiButton>
|
|
593
|
+
<UiButton type="submit" class="ml-auto">Apply</UiButton>
|
|
594
|
+
</div>
|
|
595
|
+
</UiFieldGroup>
|
|
596
|
+
</form>
|
|
597
|
+
</UiDataTableFilterMenu>
|
|
598
|
+
<UiDataTableColumnFilter v-if="table" :table="table" />
|
|
599
|
+
</template>
|
|
600
|
+
</UiDataTableToolbar>
|
|
601
|
+
|
|
602
|
+
<UiBulkAction v-model:checked="selectAllState" :selected-count="selectedCount" @reset="reset">
|
|
603
|
+
<template v-if="hasSelection">
|
|
604
|
+
<UiFloatActionItems type="separator" />
|
|
605
|
+
<UiFloatActionItems type="icon" :icon="DownloadIcon" aria-label="Export selected" @click="apply('export')" />
|
|
606
|
+
<UiFloatActionItems type="icon" :icon="ArchiveIcon" aria-label="Export and archive selected" @click="apply(['export', 'archive'])" />
|
|
607
|
+
<UiFloatActionItems type="icon" :icon="Trash2Icon" aria-label="Delete selected" @click="apply('delete')" />
|
|
608
|
+
</template>
|
|
609
|
+
</UiBulkAction>
|
|
610
|
+
|
|
611
|
+
<UiDataTable ref="tableRef" :data="props.data" :columns="columns" :get-row-id="(row) => row.id" />
|
|
612
|
+
<UiDataTablePagination v-if="table" :table="table" :show-page-size="false" />
|
|
613
|
+
</div>
|
|
614
|
+
</template>`;
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* Bulk action bar above a `UiDataTable` with status tabs, search, a filter
|
|
618
|
+
* menu and column visibility. The bar's checkbox selects every row on the
|
|
619
|
+
* current page, and `useBulkAction` mirrors the table's selection. Each button
|
|
620
|
+
* calls `apply` with one action key or an array of keys run in order, and
|
|
621
|
+
* Refresh calls `reset`, which clears the selection and runs `onReset`.
|
|
622
|
+
*/
|
|
623
|
+
export const WithDataTable: Story = {
|
|
624
|
+
parameters: {
|
|
625
|
+
docs: {
|
|
626
|
+
source: {
|
|
627
|
+
code: withDataTableTemplateSource,
|
|
628
|
+
},
|
|
629
|
+
},
|
|
630
|
+
},
|
|
631
|
+
render: () => ({
|
|
632
|
+
components: {
|
|
633
|
+
UiBulkAction,
|
|
634
|
+
UiButton,
|
|
635
|
+
UiCheckbox,
|
|
636
|
+
UiDataTable,
|
|
637
|
+
UiDataTableColumnFilter,
|
|
638
|
+
UiDataTableFilterMenu,
|
|
639
|
+
UiDataTablePagination,
|
|
640
|
+
UiDataTableToolbar,
|
|
641
|
+
UiField,
|
|
642
|
+
UiFieldContent,
|
|
643
|
+
UiFieldGroup,
|
|
644
|
+
UiFieldLabel,
|
|
645
|
+
UiFloatActionItems,
|
|
646
|
+
UiInput,
|
|
647
|
+
UiTabs,
|
|
648
|
+
UiTabsList,
|
|
649
|
+
UiTabsTrigger,
|
|
650
|
+
},
|
|
651
|
+
setup() {
|
|
652
|
+
const data = payments;
|
|
653
|
+
const tableRef = ref<InstanceType<typeof UiDataTable>>();
|
|
654
|
+
const table = computed(() => tableRef.value?.table);
|
|
655
|
+
|
|
656
|
+
const { apply, reset, selectedItems, selectedCount, hasSelection } =
|
|
657
|
+
useBulkAction<Payment>({
|
|
658
|
+
actions: [
|
|
659
|
+
{ key: 'export', action: onExport },
|
|
660
|
+
{ key: 'archive', action: onArchive },
|
|
661
|
+
{
|
|
662
|
+
key: 'delete',
|
|
663
|
+
action: (items) => {
|
|
664
|
+
onDelete(items);
|
|
665
|
+
reset();
|
|
666
|
+
},
|
|
667
|
+
},
|
|
668
|
+
],
|
|
669
|
+
onReset: () => table.value?.resetRowSelection(),
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
watch(
|
|
673
|
+
() => table.value?.getSelectedRowModel().rows,
|
|
674
|
+
(rows) => {
|
|
675
|
+
selectedItems.value = rows?.map((row) => row.original) ?? [];
|
|
676
|
+
},
|
|
677
|
+
);
|
|
678
|
+
|
|
679
|
+
const activeTab = ref<string>('all');
|
|
680
|
+
const tabs = STATUS_TABS.map((tab) => ({
|
|
681
|
+
...tab,
|
|
682
|
+
count: countByStatus(data, tab.value),
|
|
683
|
+
}));
|
|
684
|
+
watch(activeTab, (value) => {
|
|
685
|
+
table.value
|
|
686
|
+
?.getColumn('status')
|
|
687
|
+
?.setFilterValue(value === 'all' ? undefined : value);
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
const search = computed({
|
|
691
|
+
get: () => (table.value?.getState().globalFilter as string) ?? '',
|
|
692
|
+
set: (value) => table.value?.setGlobalFilter(value),
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
const filterOpen = ref(false);
|
|
696
|
+
const draft = reactive({ email: '', minAmount: '' });
|
|
697
|
+
const activeFilterCount = computed(
|
|
698
|
+
() =>
|
|
699
|
+
table.value
|
|
700
|
+
?.getState()
|
|
701
|
+
.columnFilters.filter((filter) => filter.id !== 'status').length ??
|
|
702
|
+
0,
|
|
703
|
+
);
|
|
704
|
+
|
|
705
|
+
function applyFilters() {
|
|
706
|
+
table.value
|
|
707
|
+
?.getColumn('email')
|
|
708
|
+
?.setFilterValue(draft.email || undefined);
|
|
709
|
+
table.value
|
|
710
|
+
?.getColumn('amount')
|
|
711
|
+
?.setFilterValue(
|
|
712
|
+
draft.minAmount ? Number(draft.minAmount) : undefined,
|
|
713
|
+
);
|
|
714
|
+
filterOpen.value = false;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function removeFilters() {
|
|
718
|
+
draft.email = '';
|
|
719
|
+
draft.minAmount = '';
|
|
720
|
+
applyFilters();
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
const selectAllState = computed<boolean | 'indeterminate'>({
|
|
724
|
+
get: () => {
|
|
725
|
+
if (table.value?.getIsAllPageRowsSelected()) return true;
|
|
726
|
+
return table.value?.getIsSomePageRowsSelected()
|
|
727
|
+
? 'indeterminate'
|
|
728
|
+
: false;
|
|
729
|
+
},
|
|
730
|
+
set: (value) => table.value?.toggleAllPageRowsSelected(!!value),
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
return {
|
|
734
|
+
data,
|
|
735
|
+
columns: tableColumns,
|
|
736
|
+
getRowId: (row: Payment) => row.id,
|
|
737
|
+
getRowCanExpand: () => true,
|
|
738
|
+
tableRef,
|
|
739
|
+
table,
|
|
740
|
+
apply,
|
|
741
|
+
reset,
|
|
742
|
+
selectedCount,
|
|
743
|
+
hasSelection,
|
|
744
|
+
activeTab,
|
|
745
|
+
tabs,
|
|
746
|
+
search,
|
|
747
|
+
filterOpen,
|
|
748
|
+
draft,
|
|
749
|
+
activeFilterCount,
|
|
750
|
+
applyFilters,
|
|
751
|
+
removeFilters,
|
|
752
|
+
selectAllState,
|
|
753
|
+
ArchiveIcon,
|
|
754
|
+
DownloadIcon,
|
|
755
|
+
Trash2Icon,
|
|
756
|
+
XIcon,
|
|
757
|
+
};
|
|
758
|
+
},
|
|
759
|
+
template: `
|
|
760
|
+
<div class="flex flex-col gap-4">
|
|
761
|
+
<UiDataTableToolbar>
|
|
762
|
+
<UiTabs v-model="activeTab">
|
|
763
|
+
<UiTabsList>
|
|
764
|
+
<UiTabsTrigger v-for="tab in tabs" :key="tab.value" :value="tab.value">
|
|
765
|
+
{{ tab.label }} ({{ tab.count }})
|
|
766
|
+
</UiTabsTrigger>
|
|
767
|
+
</UiTabsList>
|
|
768
|
+
</UiTabs>
|
|
769
|
+
<template #actions>
|
|
770
|
+
<UiInput
|
|
771
|
+
v-model="search"
|
|
772
|
+
name="bulk-search"
|
|
773
|
+
placeholder="Search content..."
|
|
774
|
+
aria-label="Search content"
|
|
775
|
+
class="h-8 w-full sm:w-[250px]"
|
|
776
|
+
/>
|
|
777
|
+
<UiDataTableFilterMenu
|
|
778
|
+
v-if="table"
|
|
779
|
+
v-model:open="filterOpen"
|
|
780
|
+
:active-count="activeFilterCount"
|
|
781
|
+
>
|
|
782
|
+
<form class="flex flex-col gap-5" @submit.prevent="applyFilters">
|
|
783
|
+
<UiFieldGroup>
|
|
784
|
+
<UiField>
|
|
785
|
+
<UiFieldLabel for="bulk-filter-email">Email</UiFieldLabel>
|
|
786
|
+
<UiFieldContent>
|
|
787
|
+
<UiInput id="bulk-filter-email" v-model="draft.email" name="email" placeholder="Contains..." />
|
|
788
|
+
</UiFieldContent>
|
|
789
|
+
</UiField>
|
|
790
|
+
<UiField>
|
|
791
|
+
<UiFieldLabel for="bulk-filter-amount">Minimum amount</UiFieldLabel>
|
|
792
|
+
<UiFieldContent>
|
|
793
|
+
<UiInput id="bulk-filter-amount" v-model="draft.minAmount" name="min-amount" type="number" placeholder="0" />
|
|
794
|
+
</UiFieldContent>
|
|
795
|
+
</UiField>
|
|
796
|
+
<div class="flex items-center gap-2">
|
|
797
|
+
<UiButton
|
|
798
|
+
v-if="activeFilterCount"
|
|
799
|
+
variant="outline"
|
|
800
|
+
:end-icon="XIcon"
|
|
801
|
+
type="button"
|
|
802
|
+
@click="removeFilters"
|
|
803
|
+
>
|
|
804
|
+
Remove filters
|
|
805
|
+
</UiButton>
|
|
806
|
+
<UiButton type="submit" class="ml-auto">Apply</UiButton>
|
|
807
|
+
</div>
|
|
808
|
+
</UiFieldGroup>
|
|
809
|
+
</form>
|
|
810
|
+
</UiDataTableFilterMenu>
|
|
811
|
+
<UiDataTableColumnFilter v-if="table" :table="table" />
|
|
812
|
+
</template>
|
|
813
|
+
</UiDataTableToolbar>
|
|
814
|
+
|
|
815
|
+
<UiBulkAction v-model:checked="selectAllState" :selected-count="selectedCount" @reset="reset">
|
|
816
|
+
<template v-if="hasSelection">
|
|
817
|
+
<UiFloatActionItems type="separator" />
|
|
818
|
+
<UiFloatActionItems type="icon" :icon="DownloadIcon" aria-label="Export selected" @click="apply('export')" />
|
|
819
|
+
<UiFloatActionItems type="icon" :icon="ArchiveIcon" aria-label="Export and archive selected" @click="apply(['export', 'archive'])" />
|
|
820
|
+
<UiFloatActionItems type="icon" :icon="Trash2Icon" aria-label="Delete selected" @click="apply('delete')" />
|
|
821
|
+
</template>
|
|
822
|
+
</UiBulkAction>
|
|
823
|
+
|
|
824
|
+
<UiDataTable
|
|
825
|
+
ref="tableRef"
|
|
826
|
+
:data="data"
|
|
827
|
+
:columns="columns"
|
|
828
|
+
:get-row-id="getRowId"
|
|
829
|
+
:get-row-can-expand="getRowCanExpand"
|
|
830
|
+
>
|
|
831
|
+
<template #expanded-row="{ row }">
|
|
832
|
+
<div class="body-s-regular rounded-md bg-background-surface-muted p-4">
|
|
833
|
+
<p class="font-medium">Payment {{ row.original.id }}</p>
|
|
834
|
+
<p class="text-content-on-surface-muted">{{ row.original.email }}</p>
|
|
835
|
+
</div>
|
|
836
|
+
</template>
|
|
837
|
+
</UiDataTable>
|
|
838
|
+
<UiDataTablePagination v-if="table" :table="table" :show-page-size="false" />
|
|
839
|
+
</div>
|
|
840
|
+
`,
|
|
841
|
+
}),
|
|
842
|
+
play: async ({ canvasElement, step }) => {
|
|
843
|
+
// Scenario: useBulkAction mirrors the table selection, `apply` passes the
|
|
844
|
+
// selected rows to one action or to a group in order, and reset clears it.
|
|
845
|
+
onExport.mockClear();
|
|
846
|
+
onArchive.mockClear();
|
|
847
|
+
onDelete.mockClear();
|
|
848
|
+
const canvas = within(canvasElement);
|
|
849
|
+
const selectAll = canvas.getByRole('checkbox', {
|
|
850
|
+
name: 'Select all rows on this page',
|
|
851
|
+
});
|
|
852
|
+
await canvas.findAllByRole('checkbox', { name: /^Select .+@/ });
|
|
853
|
+
const rowCheckboxes = () =>
|
|
854
|
+
canvas.getAllByRole('checkbox', { name: /^Select .+@/ });
|
|
855
|
+
|
|
856
|
+
await step(
|
|
857
|
+
'interaction: bulk actions stay hidden until a row is selected',
|
|
858
|
+
async () => {
|
|
859
|
+
await expect(
|
|
860
|
+
canvas.queryByRole('button', { name: 'Export selected' }),
|
|
861
|
+
).not.toBeInTheDocument();
|
|
862
|
+
},
|
|
863
|
+
);
|
|
864
|
+
|
|
865
|
+
await step(
|
|
866
|
+
'interaction: selecting one row reveals the actions and marks select-all as mixed',
|
|
867
|
+
async () => {
|
|
868
|
+
await userEvent.click(rowCheckboxes()[0]);
|
|
869
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'mixed');
|
|
870
|
+
await expect(
|
|
871
|
+
canvas.getByRole('button', { name: 'Export selected' }),
|
|
872
|
+
).toBeVisible();
|
|
873
|
+
},
|
|
874
|
+
);
|
|
875
|
+
|
|
876
|
+
await step(
|
|
877
|
+
'interaction: a single action receives the selected rows',
|
|
878
|
+
async () => {
|
|
879
|
+
await userEvent.click(
|
|
880
|
+
canvas.getByRole('button', { name: 'Export selected' }),
|
|
881
|
+
);
|
|
882
|
+
await expect(onExport).toHaveBeenCalledTimes(1);
|
|
883
|
+
await expect(onExport.mock.calls[0][0]).toHaveLength(1);
|
|
884
|
+
},
|
|
885
|
+
);
|
|
886
|
+
|
|
887
|
+
await step(
|
|
888
|
+
'interaction: a group action runs its actions in order with the same rows',
|
|
889
|
+
async () => {
|
|
890
|
+
onExport.mockClear();
|
|
891
|
+
await userEvent.click(
|
|
892
|
+
canvas.getByRole('button', { name: 'Export and archive selected' }),
|
|
893
|
+
);
|
|
894
|
+
await waitFor(() => expect(onArchive).toHaveBeenCalledTimes(1));
|
|
895
|
+
await expect(onExport.mock.invocationCallOrder[0]).toBeLessThan(
|
|
896
|
+
onArchive.mock.invocationCallOrder[0],
|
|
897
|
+
);
|
|
898
|
+
await expect(onArchive.mock.calls[0][0]).toEqual(
|
|
899
|
+
onExport.mock.calls[0][0],
|
|
900
|
+
);
|
|
901
|
+
},
|
|
902
|
+
);
|
|
903
|
+
|
|
904
|
+
await step(
|
|
905
|
+
'interaction: select-all selects every row on the page',
|
|
906
|
+
async () => {
|
|
907
|
+
await userEvent.click(selectAll);
|
|
908
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'true');
|
|
909
|
+
for (const checkbox of rowCheckboxes()) {
|
|
910
|
+
await expect(checkbox).toHaveAttribute('aria-checked', 'true');
|
|
911
|
+
}
|
|
912
|
+
},
|
|
913
|
+
);
|
|
914
|
+
|
|
915
|
+
await step(
|
|
916
|
+
'interaction: reset clears the selection and hides the actions',
|
|
917
|
+
async () => {
|
|
918
|
+
await userEvent.click(
|
|
919
|
+
canvas.getByRole('button', { name: 'Reset selection' }),
|
|
920
|
+
);
|
|
921
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'false');
|
|
922
|
+
await expect(
|
|
923
|
+
canvas.queryByRole('button', { name: 'Export selected' }),
|
|
924
|
+
).not.toBeInTheDocument();
|
|
925
|
+
await expect(onDelete).not.toHaveBeenCalled();
|
|
926
|
+
},
|
|
927
|
+
);
|
|
928
|
+
},
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
type DemoActionKey =
|
|
932
|
+
| 'approve'
|
|
933
|
+
| 'mark-pending'
|
|
934
|
+
| 'remove-valuations'
|
|
935
|
+
| 'export'
|
|
936
|
+
| 'delete';
|
|
937
|
+
|
|
938
|
+
type DemoResult = {
|
|
939
|
+
message: string;
|
|
940
|
+
failed?: string;
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
type DemoLogEntry = {
|
|
944
|
+
id: number;
|
|
945
|
+
key: DemoActionKey | 'onReset';
|
|
946
|
+
items: string[];
|
|
947
|
+
status: 'running' | 'done';
|
|
948
|
+
message?: string;
|
|
949
|
+
};
|
|
950
|
+
|
|
951
|
+
const DEMO_ACTION_DELAY = 600;
|
|
952
|
+
|
|
953
|
+
const wait = (ms: number) =>
|
|
954
|
+
new Promise<void>((resolve) => {
|
|
955
|
+
setTimeout(resolve, ms);
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
const STATUS_BADGE: Record<Payment['status'], UiBadgeVariants> = {
|
|
959
|
+
success: 'success',
|
|
960
|
+
processing: 'secondary',
|
|
961
|
+
pending: 'warning',
|
|
962
|
+
failed: 'destructive',
|
|
963
|
+
};
|
|
964
|
+
|
|
965
|
+
const ACTION_LABELS: Record<DemoActionKey | 'onReset', string> = {
|
|
966
|
+
approve: 'Marking as success',
|
|
967
|
+
'mark-pending': 'Marking as pending',
|
|
968
|
+
'remove-valuations': 'Removing valuations',
|
|
969
|
+
export: 'Exporting',
|
|
970
|
+
delete: 'Deleting',
|
|
971
|
+
onReset: 'onReset',
|
|
972
|
+
};
|
|
973
|
+
|
|
974
|
+
const demoColumns: ColumnDef<Payment>[] = [
|
|
975
|
+
{
|
|
976
|
+
id: 'select',
|
|
977
|
+
header: () => h('span', { class: 'sr-only' }, 'Select'),
|
|
978
|
+
cell: ({ row }) =>
|
|
979
|
+
h(UiCheckbox, {
|
|
980
|
+
modelValue: row.getIsSelected(),
|
|
981
|
+
'onUpdate:modelValue': (value: boolean) => row.toggleSelected(!!value),
|
|
982
|
+
'aria-label': `Select ${row.original.email}`,
|
|
983
|
+
name: `demo-select-${row.id}`,
|
|
984
|
+
}),
|
|
985
|
+
enableSorting: false,
|
|
986
|
+
size: 40,
|
|
987
|
+
},
|
|
988
|
+
{
|
|
989
|
+
accessorKey: 'status',
|
|
990
|
+
header: ({ column }) =>
|
|
991
|
+
h(UiDataTableColumnHeader, { column, title: 'Status' }),
|
|
992
|
+
cell: ({ row }) =>
|
|
993
|
+
h(
|
|
994
|
+
UiBadge,
|
|
995
|
+
{
|
|
996
|
+
variant: STATUS_BADGE[row.original.status],
|
|
997
|
+
size: 'small',
|
|
998
|
+
class: 'capitalize',
|
|
999
|
+
},
|
|
1000
|
+
() => row.original.status,
|
|
1001
|
+
),
|
|
1002
|
+
size: 120,
|
|
1003
|
+
},
|
|
1004
|
+
{
|
|
1005
|
+
accessorKey: 'email',
|
|
1006
|
+
header: ({ column }) =>
|
|
1007
|
+
h(UiDataTableColumnHeader, { column, title: 'Email' }),
|
|
1008
|
+
size: 250,
|
|
1009
|
+
},
|
|
1010
|
+
{
|
|
1011
|
+
accessorKey: 'amount',
|
|
1012
|
+
header: ({ column }) =>
|
|
1013
|
+
h(UiDataTableColumnHeader, {
|
|
1014
|
+
column,
|
|
1015
|
+
title: 'Amount',
|
|
1016
|
+
class: 'justify-end',
|
|
1017
|
+
}),
|
|
1018
|
+
cell: ({ row }) =>
|
|
1019
|
+
h(
|
|
1020
|
+
'div',
|
|
1021
|
+
{ class: 'text-right font-medium' },
|
|
1022
|
+
formatAmount(Number(row.getValue('amount'))),
|
|
1023
|
+
),
|
|
1024
|
+
size: 120,
|
|
1025
|
+
},
|
|
1026
|
+
];
|
|
1027
|
+
|
|
1028
|
+
const HOW_IT_WORKS = [
|
|
1029
|
+
{
|
|
1030
|
+
title: 'Describe your actions',
|
|
1031
|
+
text: 'Give the composable a list of actions — each a key and a function — plus what reset should do.',
|
|
1032
|
+
code: 'useBulkAction({ actions, onReset })',
|
|
1033
|
+
},
|
|
1034
|
+
{
|
|
1035
|
+
title: 'Feed it the selection',
|
|
1036
|
+
text: 'Write the rows the user selected into selectedItems. The table stays the source of truth.',
|
|
1037
|
+
code: 'selectedItems.value = rows',
|
|
1038
|
+
},
|
|
1039
|
+
{
|
|
1040
|
+
title: 'Run actions from the bar',
|
|
1041
|
+
text: 'Call apply with one key, or an array of keys to run a group in order. Every action gets the selected items.',
|
|
1042
|
+
code: "apply('remove-valuations') · apply(['export', 'approve'])",
|
|
1043
|
+
},
|
|
1044
|
+
];
|
|
1045
|
+
|
|
1046
|
+
const composableCode = {
|
|
1047
|
+
input: `import { useBulkAction } from '@aleph-alpha/ui-library';
|
|
1048
|
+
|
|
1049
|
+
// The keys your bar can run. Typing them makes apply('typo') a compile error.
|
|
1050
|
+
type ActionKey = 'approve' | 'mark-pending' | 'remove-valuations' | 'export' | 'delete';
|
|
1051
|
+
|
|
1052
|
+
const bulk = useBulkAction<Payment, ActionKey>({
|
|
1053
|
+
// ① Every action receives the items that were selected when apply() was called.
|
|
1054
|
+
// Return a promise for async work — apply() waits for it.
|
|
1055
|
+
actions: [
|
|
1056
|
+
{ key: 'approve', action: (items) => api.setStatus(items, 'success') },
|
|
1057
|
+
{ key: 'mark-pending', action: (items) => api.setStatus(items, 'pending') },
|
|
1058
|
+
{ key: 'remove-valuations', action: (items) => api.setAmount(items, 0) },
|
|
1059
|
+
{ key: 'export', action: async (items) => api.export(items) },
|
|
1060
|
+
{ key: 'delete', action: async (items) => api.delete(items) },
|
|
1061
|
+
],
|
|
1062
|
+
|
|
1063
|
+
// ② Called by reset() after the selection is cleared.
|
|
1064
|
+
onReset: () => table.value?.resetRowSelection(),
|
|
1065
|
+
|
|
1066
|
+
// ③ Optional: rows selected on first render.
|
|
1067
|
+
initialSelection: [],
|
|
1068
|
+
});`,
|
|
1069
|
+
output: `const {
|
|
1070
|
+
selectedItems, // Ref<Payment[]> → write the table's selection here
|
|
1071
|
+
selectedCount, // ComputedRef<number> → "3 selected"
|
|
1072
|
+
hasSelection, // ComputedRef<boolean> → show bulk actions only when true
|
|
1073
|
+
apply, // (key | key[]) => Promise<void>
|
|
1074
|
+
reset, // () => void → clears the selection, then calls onReset
|
|
1075
|
+
clearSelection, // () => void → clears the selection only
|
|
1076
|
+
} = bulk;
|
|
1077
|
+
|
|
1078
|
+
// Keep the composable in sync with the table (all pages, filtered or not).
|
|
1079
|
+
watch(
|
|
1080
|
+
() => table.value?.getSelectedRowModel().rows,
|
|
1081
|
+
(rows) => { selectedItems.value = rows?.map((row) => row.original) ?? []; },
|
|
1082
|
+
);
|
|
1083
|
+
|
|
1084
|
+
await apply('approve'); // one action
|
|
1085
|
+
await apply(['export', 'approve']); // a group: export first, then approve
|
|
1086
|
+
reset(); // back to the default state`,
|
|
1087
|
+
template: `<UiBulkAction
|
|
1088
|
+
v-model:checked="selectAll"
|
|
1089
|
+
:selected-count="selectedCount"
|
|
1090
|
+
@reset="reset"
|
|
1091
|
+
>
|
|
1092
|
+
<!-- The select-all checkbox is rendered first, reset last, by UiBulkAction -->
|
|
1093
|
+
<!-- Bulk actions appear only once something is selected -->
|
|
1094
|
+
<template v-if="hasSelection">
|
|
1095
|
+
<span role="status" class="sr-only">{{ selectedCount }} selected</span>
|
|
1096
|
+
<UiFloatActionItems type="separator" />
|
|
1097
|
+
|
|
1098
|
+
<UiDropdownMenu>
|
|
1099
|
+
<UiDropdownMenuTrigger as-child>
|
|
1100
|
+
<UiFloatActionItems type="label" :suffix-icon="ChevronDownIcon">Change status</UiFloatActionItems>
|
|
1101
|
+
</UiDropdownMenuTrigger>
|
|
1102
|
+
<UiDropdownMenuContent align="start">
|
|
1103
|
+
<UiDropdownMenuItem @select="apply('approve')">Success</UiDropdownMenuItem>
|
|
1104
|
+
<UiDropdownMenuItem @select="apply('mark-pending')">Pending</UiDropdownMenuItem>
|
|
1105
|
+
<UiDropdownMenuSeparator />
|
|
1106
|
+
<!-- A group: export first, then approve -->
|
|
1107
|
+
<UiDropdownMenuItem @select="apply(['export', 'approve'])">
|
|
1108
|
+
Export, then mark as success
|
|
1109
|
+
</UiDropdownMenuItem>
|
|
1110
|
+
</UiDropdownMenuContent>
|
|
1111
|
+
</UiDropdownMenu>
|
|
1112
|
+
|
|
1113
|
+
<UiFloatActionItems type="label" :suffix-icon="DeleteIcon" @click="apply('remove-valuations')">
|
|
1114
|
+
Remove valuations
|
|
1115
|
+
</UiFloatActionItems>
|
|
1116
|
+
|
|
1117
|
+
<UiFloatActionItems type="separator" />
|
|
1118
|
+
<UiFloatActionItems type="icon" :icon="UploadIcon" aria-label="Export" @click="apply('export')" />
|
|
1119
|
+
<!-- Destructive: confirm before calling apply('delete') -->
|
|
1120
|
+
<UiFloatActionItems type="icon" :icon="Trash2Icon" aria-label="Delete" @click="confirmDelete = true" />
|
|
1121
|
+
</template>
|
|
1122
|
+
</UiBulkAction>`,
|
|
1123
|
+
};
|
|
1124
|
+
|
|
1125
|
+
const useBulkActionComposableSource = `<script setup lang="ts">
|
|
1126
|
+
import { computed, h, ref, watch } from 'vue';
|
|
1127
|
+
import {
|
|
1128
|
+
UiAlertDialog,
|
|
1129
|
+
UiAlertDialogAction,
|
|
1130
|
+
UiAlertDialogCancel,
|
|
1131
|
+
UiAlertDialogContent,
|
|
1132
|
+
UiAlertDialogDescription,
|
|
1133
|
+
UiAlertDialogFooter,
|
|
1134
|
+
UiAlertDialogHeader,
|
|
1135
|
+
UiAlertDialogTitle,
|
|
1136
|
+
UiBulkAction,
|
|
1137
|
+
UiCheckbox,
|
|
1138
|
+
UiDataTable,
|
|
1139
|
+
UiDropdownMenu,
|
|
1140
|
+
UiDropdownMenuContent,
|
|
1141
|
+
UiDropdownMenuItem,
|
|
1142
|
+
UiDropdownMenuSeparator,
|
|
1143
|
+
UiDropdownMenuTrigger,
|
|
1144
|
+
UiFloatActionItems,
|
|
1145
|
+
useBulkAction,
|
|
1146
|
+
} from '@aleph-alpha/ui-library';
|
|
1147
|
+
import type { ColumnDef } from '@aleph-alpha/ui-library';
|
|
1148
|
+
import { ChevronDownIcon, DeleteIcon, Trash2Icon, UploadIcon } from '@aleph-alpha/ui-library/icons';
|
|
1149
|
+
|
|
1150
|
+
interface Payment {
|
|
1151
|
+
id: string;
|
|
1152
|
+
amount: number;
|
|
1153
|
+
status: 'pending' | 'processing' | 'success' | 'failed';
|
|
1154
|
+
email: string;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
type ActionKey = 'approve' | 'mark-pending' | 'remove-valuations' | 'export' | 'delete';
|
|
1158
|
+
|
|
1159
|
+
const props = defineProps<{ payments: Payment[] }>();
|
|
1160
|
+
const data = ref<Payment[]>([...props.payments]);
|
|
1161
|
+
|
|
1162
|
+
const columns: ColumnDef<Payment>[] = [
|
|
1163
|
+
{
|
|
1164
|
+
id: 'select',
|
|
1165
|
+
header: () => h('span', { class: 'sr-only' }, 'Select'),
|
|
1166
|
+
cell: ({ row }) => h(UiCheckbox, {
|
|
1167
|
+
modelValue: row.getIsSelected(),
|
|
1168
|
+
'onUpdate:modelValue': (value: boolean) => row.toggleSelected(!!value),
|
|
1169
|
+
'aria-label': \`Select \${row.original.email}\`,
|
|
1170
|
+
name: \`select-\${row.id}\`,
|
|
1171
|
+
}),
|
|
1172
|
+
},
|
|
1173
|
+
{ accessorKey: 'status', header: 'Status' },
|
|
1174
|
+
{ accessorKey: 'email', header: 'Email' },
|
|
1175
|
+
{ accessorKey: 'amount', header: 'Amount' },
|
|
1176
|
+
];
|
|
1177
|
+
|
|
1178
|
+
const tableRef = ref<InstanceType<typeof UiDataTable>>();
|
|
1179
|
+
const table = computed(() => tableRef.value?.table);
|
|
1180
|
+
const confirmDelete = ref(false);
|
|
1181
|
+
|
|
1182
|
+
function patch(items: Payment[], change: Partial<Payment>) {
|
|
1183
|
+
const ids = new Set(items.map((item) => item.id));
|
|
1184
|
+
data.value = data.value.map((row) => (ids.has(row.id) ? { ...row, ...change } : row));
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
const { apply, reset, selectedItems, selectedCount, hasSelection } = useBulkAction<Payment, ActionKey>({
|
|
1188
|
+
actions: [
|
|
1189
|
+
{ key: 'approve', action: (items) => patch(items, { status: 'success' }) },
|
|
1190
|
+
{ key: 'mark-pending', action: (items) => patch(items, { status: 'pending' }) },
|
|
1191
|
+
{ key: 'remove-valuations', action: (items) => patch(items, { amount: 0 }) },
|
|
1192
|
+
{ key: 'export', action: async (items) => { await fetch('/api/export', { method: 'POST', body: JSON.stringify(items) }); } },
|
|
1193
|
+
{
|
|
1194
|
+
key: 'delete',
|
|
1195
|
+
action: async (items) => {
|
|
1196
|
+
const ids = new Set(items.map((item) => item.id));
|
|
1197
|
+
data.value = data.value.filter((row) => !ids.has(row.id));
|
|
1198
|
+
reset();
|
|
1199
|
+
},
|
|
1200
|
+
},
|
|
1201
|
+
],
|
|
1202
|
+
onReset: () => table.value?.resetRowSelection(),
|
|
1203
|
+
});
|
|
1204
|
+
|
|
1205
|
+
watch(
|
|
1206
|
+
() => table.value?.getSelectedRowModel().rows,
|
|
1207
|
+
(rows) => { selectedItems.value = rows?.map((row) => row.original) ?? []; },
|
|
1208
|
+
);
|
|
1209
|
+
|
|
1210
|
+
const selectAll = computed({
|
|
1211
|
+
get: () => table.value?.getIsAllPageRowsSelected()
|
|
1212
|
+
|| (table.value?.getIsSomePageRowsSelected() ? 'indeterminate' : false),
|
|
1213
|
+
set: (value) => table.value?.toggleAllPageRowsSelected(!!value),
|
|
1214
|
+
});
|
|
1215
|
+
</script>
|
|
1216
|
+
|
|
1217
|
+
<template>
|
|
1218
|
+
<div class="flex flex-col gap-3">
|
|
1219
|
+
<UiBulkAction v-model:checked="selectAll" :selected-count="selectedCount" @reset="reset">
|
|
1220
|
+
<template v-if="hasSelection">
|
|
1221
|
+
<span role="status" class="sr-only">{{ selectedCount }} selected</span>
|
|
1222
|
+
<UiFloatActionItems type="separator" />
|
|
1223
|
+
<UiDropdownMenu>
|
|
1224
|
+
<UiDropdownMenuTrigger as-child>
|
|
1225
|
+
<UiFloatActionItems type="label" :suffix-icon="ChevronDownIcon">Change status</UiFloatActionItems>
|
|
1226
|
+
</UiDropdownMenuTrigger>
|
|
1227
|
+
<UiDropdownMenuContent align="start">
|
|
1228
|
+
<UiDropdownMenuItem @select="apply('approve')">Success</UiDropdownMenuItem>
|
|
1229
|
+
<UiDropdownMenuItem @select="apply('mark-pending')">Pending</UiDropdownMenuItem>
|
|
1230
|
+
<UiDropdownMenuSeparator />
|
|
1231
|
+
<UiDropdownMenuItem @select="apply(['export', 'approve'])">Export, then mark as success</UiDropdownMenuItem>
|
|
1232
|
+
</UiDropdownMenuContent>
|
|
1233
|
+
</UiDropdownMenu>
|
|
1234
|
+
<UiFloatActionItems type="label" :suffix-icon="DeleteIcon" @click="apply('remove-valuations')">
|
|
1235
|
+
Remove valuations
|
|
1236
|
+
</UiFloatActionItems>
|
|
1237
|
+
<UiFloatActionItems type="separator" />
|
|
1238
|
+
<UiFloatActionItems type="icon" :icon="UploadIcon" aria-label="Export" @click="apply('export')" />
|
|
1239
|
+
<UiFloatActionItems type="icon" :icon="Trash2Icon" aria-label="Delete" @click="confirmDelete = true" />
|
|
1240
|
+
</template>
|
|
1241
|
+
</UiBulkAction>
|
|
1242
|
+
|
|
1243
|
+
<UiDataTable ref="tableRef" :data="data" :columns="columns" :get-row-id="(row) => row.id" />
|
|
1244
|
+
|
|
1245
|
+
<UiAlertDialog v-model:model="confirmDelete">
|
|
1246
|
+
<UiAlertDialogContent>
|
|
1247
|
+
<UiAlertDialogHeader>
|
|
1248
|
+
<UiAlertDialogTitle>Delete {{ selectedCount }} payments?</UiAlertDialogTitle>
|
|
1249
|
+
<UiAlertDialogDescription>This cannot be undone.</UiAlertDialogDescription>
|
|
1250
|
+
</UiAlertDialogHeader>
|
|
1251
|
+
<UiAlertDialogFooter>
|
|
1252
|
+
<UiAlertDialogCancel>Cancel</UiAlertDialogCancel>
|
|
1253
|
+
<UiAlertDialogAction @click="apply('delete')">Delete</UiAlertDialogAction>
|
|
1254
|
+
</UiAlertDialogFooter>
|
|
1255
|
+
</UiAlertDialogContent>
|
|
1256
|
+
</UiAlertDialog>
|
|
1257
|
+
</div>
|
|
1258
|
+
</template>`;
|
|
1259
|
+
|
|
1260
|
+
/**
|
|
1261
|
+
* A guided, end-to-end example of the `useBulkAction` composable.
|
|
1262
|
+
*
|
|
1263
|
+
* - **Input:** `actions` (a `key` and an `action(items)` each) and `onReset`.
|
|
1264
|
+
* - **Output:** `apply`, `reset`, and the reactive `selectedItems`,
|
|
1265
|
+
* `selectedCount` and `hasSelection`.
|
|
1266
|
+
*
|
|
1267
|
+
* The demo covers async actions, a group action run in order, an action that
|
|
1268
|
+
* is unavailable for part of the selection, partial failures reported back to
|
|
1269
|
+
* the user, a confirmed destructive action, and a selection that survives
|
|
1270
|
+
* search and paging. The code tabs below the demo show each part on its own.
|
|
1271
|
+
*/
|
|
1272
|
+
export const UseBulkActionComposable: Story = {
|
|
1273
|
+
name: 'useBulkAction (composable)',
|
|
1274
|
+
parameters: {
|
|
1275
|
+
docs: {
|
|
1276
|
+
source: {
|
|
1277
|
+
code: useBulkActionComposableSource,
|
|
1278
|
+
},
|
|
1279
|
+
},
|
|
1280
|
+
},
|
|
1281
|
+
render: () => ({
|
|
1282
|
+
components: {
|
|
1283
|
+
UiAlert,
|
|
1284
|
+
UiAlertDescription,
|
|
1285
|
+
UiAlertDialog,
|
|
1286
|
+
UiAlertDialogAction,
|
|
1287
|
+
UiAlertDialogCancel,
|
|
1288
|
+
UiAlertDialogContent,
|
|
1289
|
+
UiAlertDialogDescription,
|
|
1290
|
+
UiAlertDialogFooter,
|
|
1291
|
+
UiAlertDialogHeader,
|
|
1292
|
+
UiAlertDialogTitle,
|
|
1293
|
+
UiAlertTitle,
|
|
1294
|
+
UiBadge,
|
|
1295
|
+
UiBulkAction,
|
|
1296
|
+
UiButton,
|
|
1297
|
+
UiCodeBlock,
|
|
1298
|
+
UiDataTable,
|
|
1299
|
+
UiDataTablePagination,
|
|
1300
|
+
UiDataTableToolbar,
|
|
1301
|
+
UiDropdownMenu,
|
|
1302
|
+
UiDropdownMenuContent,
|
|
1303
|
+
UiDropdownMenuItem,
|
|
1304
|
+
UiDropdownMenuSeparator,
|
|
1305
|
+
UiDropdownMenuTrigger,
|
|
1306
|
+
UiFloatActionItems,
|
|
1307
|
+
UiIcon,
|
|
1308
|
+
UiInput,
|
|
1309
|
+
UiSpinner,
|
|
1310
|
+
UiTabs,
|
|
1311
|
+
UiTabsContent,
|
|
1312
|
+
UiTabsList,
|
|
1313
|
+
UiTabsTrigger,
|
|
1314
|
+
UiTooltip,
|
|
1315
|
+
},
|
|
1316
|
+
setup() {
|
|
1317
|
+
const seed = () => paymentsSmall.map((row) => ({ ...row }));
|
|
1318
|
+
const data = ref<Payment[]>(seed());
|
|
1319
|
+
const tableRef = ref<InstanceType<typeof UiDataTable>>();
|
|
1320
|
+
const table = computed(() => tableRef.value?.table);
|
|
1321
|
+
|
|
1322
|
+
const running = ref<DemoActionKey | null>(null);
|
|
1323
|
+
const log = ref<DemoLogEntry[]>([]);
|
|
1324
|
+
const confirmDelete = ref(false);
|
|
1325
|
+
let logId = 0;
|
|
1326
|
+
|
|
1327
|
+
function patch(items: Payment[], change: Partial<Payment>) {
|
|
1328
|
+
const ids = new Set(items.map((item) => item.id));
|
|
1329
|
+
data.value = data.value.map((row) =>
|
|
1330
|
+
ids.has(row.id) ? { ...row, ...change } : row,
|
|
1331
|
+
);
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
function emails(items: Payment[]) {
|
|
1335
|
+
return items.map((item) => item.email);
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
function track(
|
|
1339
|
+
key: DemoActionKey,
|
|
1340
|
+
run: (items: Payment[]) => DemoResult,
|
|
1341
|
+
) {
|
|
1342
|
+
return {
|
|
1343
|
+
key,
|
|
1344
|
+
action: async (items: Payment[]) => {
|
|
1345
|
+
running.value = key;
|
|
1346
|
+
logId += 1;
|
|
1347
|
+
const entry: DemoLogEntry = {
|
|
1348
|
+
id: logId,
|
|
1349
|
+
key,
|
|
1350
|
+
items: emails(items),
|
|
1351
|
+
status: 'running',
|
|
1352
|
+
};
|
|
1353
|
+
log.value = [entry, ...log.value].slice(0, 10);
|
|
1354
|
+
await wait(DEMO_ACTION_DELAY);
|
|
1355
|
+
const { message, failed } = run(items);
|
|
1356
|
+
markDone(entry.id, failed ? `${message} — ${failed}` : message);
|
|
1357
|
+
},
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
function markDone(id: number, message: string) {
|
|
1362
|
+
log.value = log.value.map((current) =>
|
|
1363
|
+
current.id === id ? { ...current, status: 'done', message } : current,
|
|
1364
|
+
);
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
const { apply, reset, selectedItems, selectedCount, hasSelection } =
|
|
1368
|
+
useBulkAction<Payment, DemoActionKey>({
|
|
1369
|
+
actions: [
|
|
1370
|
+
track('approve', (items) => {
|
|
1371
|
+
patch(items, { status: 'success' });
|
|
1372
|
+
return { message: `Approved ${items.length} payments` };
|
|
1373
|
+
}),
|
|
1374
|
+
track('mark-pending', (items) => {
|
|
1375
|
+
patch(items, { status: 'pending' });
|
|
1376
|
+
return { message: `Marked ${items.length} payments as pending` };
|
|
1377
|
+
}),
|
|
1378
|
+
track('remove-valuations', (items) => {
|
|
1379
|
+
patch(items, { amount: 0 });
|
|
1380
|
+
return {
|
|
1381
|
+
message: `Removed valuations from ${items.length} payments`,
|
|
1382
|
+
};
|
|
1383
|
+
}),
|
|
1384
|
+
track('export', (items) => {
|
|
1385
|
+
const blocked = items.filter((item) => item.status === 'pending');
|
|
1386
|
+
const exported = items.length - blocked.length;
|
|
1387
|
+
const noun = blocked.length === 1 ? 'payment' : 'payments';
|
|
1388
|
+
return {
|
|
1389
|
+
message: `Exported ${exported} of ${items.length} payments`,
|
|
1390
|
+
failed: blocked.length
|
|
1391
|
+
? `${blocked.length} pending ${noun} can't be exported: ${emails(blocked).join(', ')}`
|
|
1392
|
+
: undefined,
|
|
1393
|
+
};
|
|
1394
|
+
}),
|
|
1395
|
+
track('delete', (items) => {
|
|
1396
|
+
const ids = new Set(items.map((item) => item.id));
|
|
1397
|
+
data.value = data.value.filter((row) => !ids.has(row.id));
|
|
1398
|
+
reset();
|
|
1399
|
+
return { message: `Deleted ${items.length} payments` };
|
|
1400
|
+
}),
|
|
1401
|
+
],
|
|
1402
|
+
onReset: () => {
|
|
1403
|
+
table.value?.resetRowSelection();
|
|
1404
|
+
logId += 1;
|
|
1405
|
+
log.value = [
|
|
1406
|
+
{
|
|
1407
|
+
id: logId,
|
|
1408
|
+
key: 'onReset' as const,
|
|
1409
|
+
items: [],
|
|
1410
|
+
status: 'done' as const,
|
|
1411
|
+
},
|
|
1412
|
+
...log.value,
|
|
1413
|
+
].slice(0, 10);
|
|
1414
|
+
},
|
|
1415
|
+
});
|
|
1416
|
+
|
|
1417
|
+
watch(
|
|
1418
|
+
() => table.value?.getSelectedRowModel().rows,
|
|
1419
|
+
(rows) => {
|
|
1420
|
+
selectedItems.value = rows?.map((row) => row.original) ?? [];
|
|
1421
|
+
},
|
|
1422
|
+
);
|
|
1423
|
+
|
|
1424
|
+
async function run(keys: DemoActionKey | DemoActionKey[]) {
|
|
1425
|
+
if (running.value) return;
|
|
1426
|
+
try {
|
|
1427
|
+
await apply(keys);
|
|
1428
|
+
} finally {
|
|
1429
|
+
running.value = null;
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
const selectAll = computed<boolean | 'indeterminate'>({
|
|
1434
|
+
get: () => {
|
|
1435
|
+
if (table.value?.getIsAllPageRowsSelected()) return true;
|
|
1436
|
+
return table.value?.getIsSomePageRowsSelected()
|
|
1437
|
+
? 'indeterminate'
|
|
1438
|
+
: false;
|
|
1439
|
+
},
|
|
1440
|
+
set: (value) => table.value?.toggleAllPageRowsSelected(!!value),
|
|
1441
|
+
});
|
|
1442
|
+
|
|
1443
|
+
const search = computed({
|
|
1444
|
+
get: () => (table.value?.getState().globalFilter as string) ?? '',
|
|
1445
|
+
set: (value) => table.value?.setGlobalFilter(value),
|
|
1446
|
+
});
|
|
1447
|
+
|
|
1448
|
+
const hiddenSelected = computed(
|
|
1449
|
+
() =>
|
|
1450
|
+
selectedCount.value -
|
|
1451
|
+
(table.value?.getFilteredSelectedRowModel().rows.length ??
|
|
1452
|
+
selectedCount.value),
|
|
1453
|
+
);
|
|
1454
|
+
|
|
1455
|
+
const approveBlockedReason = computed(() => {
|
|
1456
|
+
const failed = selectedItems.value.filter(
|
|
1457
|
+
(item) => item.status === 'failed',
|
|
1458
|
+
).length;
|
|
1459
|
+
if (!failed) return '';
|
|
1460
|
+
const noun = failed === 1 ? 'payment' : 'payments';
|
|
1461
|
+
return `${failed} failed ${noun} selected. Deselect ${failed === 1 ? 'it' : 'them'} to continue.`;
|
|
1462
|
+
});
|
|
1463
|
+
|
|
1464
|
+
function deselectFailed() {
|
|
1465
|
+
table.value
|
|
1466
|
+
?.getSelectedRowModel()
|
|
1467
|
+
.rows.filter((row) => row.original.status === 'failed')
|
|
1468
|
+
.forEach((row) => row.toggleSelected(false));
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
const selectedPreview = computed(() =>
|
|
1472
|
+
selectedItems.value.slice(0, 4).map((item) => item.email),
|
|
1473
|
+
);
|
|
1474
|
+
|
|
1475
|
+
function restore() {
|
|
1476
|
+
data.value = seed();
|
|
1477
|
+
log.value = [];
|
|
1478
|
+
reset();
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
return {
|
|
1482
|
+
data,
|
|
1483
|
+
columns: demoColumns,
|
|
1484
|
+
getRowId: (row: Payment) => row.id,
|
|
1485
|
+
tableRef,
|
|
1486
|
+
table,
|
|
1487
|
+
selectAll,
|
|
1488
|
+
search,
|
|
1489
|
+
hiddenSelected,
|
|
1490
|
+
selectedItems,
|
|
1491
|
+
selectedCount,
|
|
1492
|
+
hasSelection,
|
|
1493
|
+
selectedPreview,
|
|
1494
|
+
approveBlockedReason,
|
|
1495
|
+
deselectFailed,
|
|
1496
|
+
running,
|
|
1497
|
+
run,
|
|
1498
|
+
reset,
|
|
1499
|
+
restore,
|
|
1500
|
+
confirmDelete,
|
|
1501
|
+
log,
|
|
1502
|
+
actionLabels: ACTION_LABELS,
|
|
1503
|
+
steps: HOW_IT_WORKS,
|
|
1504
|
+
code: composableCode,
|
|
1505
|
+
fullSource: useBulkActionComposableSource,
|
|
1506
|
+
BanIcon,
|
|
1507
|
+
ChevronDownIcon,
|
|
1508
|
+
DeleteIcon,
|
|
1509
|
+
Trash2Icon,
|
|
1510
|
+
UploadIcon,
|
|
1511
|
+
XIcon,
|
|
1512
|
+
};
|
|
1513
|
+
},
|
|
1514
|
+
template: `
|
|
1515
|
+
<div class="flex flex-col gap-8">
|
|
1516
|
+
<!-- How it works -->
|
|
1517
|
+
<section class="flex flex-col gap-3">
|
|
1518
|
+
<h2 class="heading-s text-content-on-surface-primary">How it works</h2>
|
|
1519
|
+
<ol class="grid gap-3 md:grid-cols-3">
|
|
1520
|
+
<li
|
|
1521
|
+
v-for="(step, index) in steps"
|
|
1522
|
+
:key="step.title"
|
|
1523
|
+
class="flex flex-col gap-2 rounded-xl bg-background-surface-secondary p-4 shadow-lg"
|
|
1524
|
+
>
|
|
1525
|
+
<span class="flex items-center gap-2">
|
|
1526
|
+
<span class="label-s-medium inline-flex h-6 w-6 items-center justify-center rounded-full bg-background-surface-muted text-content-on-surface-primary">
|
|
1527
|
+
{{ index + 1 }}
|
|
1528
|
+
</span>
|
|
1529
|
+
<span class="body-m-medium text-content-on-surface-primary">{{ step.title }}</span>
|
|
1530
|
+
</span>
|
|
1531
|
+
<p class="body-s-regular text-content-on-surface-muted">{{ step.text }}</p>
|
|
1532
|
+
<code class="body-xs-regular mt-auto rounded-md bg-background-surface-muted px-2 py-1 font-mono text-content-on-surface-primary">
|
|
1533
|
+
{{ step.code }}
|
|
1534
|
+
</code>
|
|
1535
|
+
</li>
|
|
1536
|
+
</ol>
|
|
1537
|
+
</section>
|
|
1538
|
+
|
|
1539
|
+
<!-- Live demo -->
|
|
1540
|
+
<section class="flex flex-col gap-3">
|
|
1541
|
+
<div class="flex flex-wrap items-end justify-between gap-2">
|
|
1542
|
+
<h2 class="heading-s text-content-on-surface-primary">Try it</h2>
|
|
1543
|
+
<UiButton variant="link" class="px-0" @click="restore">Restore sample data</UiButton>
|
|
1544
|
+
</div>
|
|
1545
|
+
|
|
1546
|
+
<UiAlert>
|
|
1547
|
+
<UiAlertTitle>Things to try</UiAlertTitle>
|
|
1548
|
+
<UiAlertDescription>
|
|
1549
|
+
<ul class="list-disc pl-5">
|
|
1550
|
+
<li>Select a few rows, including a <strong>failed</strong> one, and open <strong>Change status</strong> to see why <strong>Success</strong> is unavailable.</li>
|
|
1551
|
+
<li>Run <strong>Change status → Export, then mark as success</strong> — two actions run in order, and the action log reports pending rows that could not be exported.</li>
|
|
1552
|
+
<li><strong>Remove valuations</strong> sets the amount of the selected rows to zero.</li>
|
|
1553
|
+
<li>Select rows, then search for <strong>gmail</strong> or go to page 2 — the selection is kept and hidden rows are called out.</li>
|
|
1554
|
+
<li><strong>Delete</strong> asks for confirmation first. The reset button on the right clears everything.</li>
|
|
1555
|
+
</ul>
|
|
1556
|
+
</UiAlertDescription>
|
|
1557
|
+
</UiAlert>
|
|
1558
|
+
|
|
1559
|
+
<div class="grid gap-6 xl:grid-cols-[minmax(0,1.6fr)_minmax(0,1fr)]">
|
|
1560
|
+
<div class="flex min-w-0 flex-col gap-3">
|
|
1561
|
+
<UiDataTableToolbar>
|
|
1562
|
+
<UiInput
|
|
1563
|
+
v-model="search"
|
|
1564
|
+
name="demo-search"
|
|
1565
|
+
placeholder="Search payments..."
|
|
1566
|
+
aria-label="Search payments"
|
|
1567
|
+
class="h-8 w-full sm:w-[250px]"
|
|
1568
|
+
/>
|
|
1569
|
+
</UiDataTableToolbar>
|
|
1570
|
+
|
|
1571
|
+
<UiBulkAction v-model:checked="selectAll" :selected-count="selectedCount" @reset="reset">
|
|
1572
|
+
<template v-if="hasSelection">
|
|
1573
|
+
<span role="status" class="sr-only">{{ selectedCount }} selected</span>
|
|
1574
|
+
<UiFloatActionItems v-if="running" type="text">
|
|
1575
|
+
<UiSpinner aria-label="Running" class="mr-1 size-4" />
|
|
1576
|
+
{{ actionLabels[running] }}…
|
|
1577
|
+
</UiFloatActionItems>
|
|
1578
|
+
<UiFloatActionItems type="separator" />
|
|
1579
|
+
|
|
1580
|
+
<UiDropdownMenu>
|
|
1581
|
+
<UiDropdownMenuTrigger as-child>
|
|
1582
|
+
<UiFloatActionItems type="label" :suffix-icon="ChevronDownIcon" :disabled="!!running">
|
|
1583
|
+
Change status
|
|
1584
|
+
</UiFloatActionItems>
|
|
1585
|
+
</UiDropdownMenuTrigger>
|
|
1586
|
+
<UiDropdownMenuContent align="start" class="w-72">
|
|
1587
|
+
<UiDropdownMenuItem
|
|
1588
|
+
:aria-label="approveBlockedReason ? 'Success, unavailable: ' + approveBlockedReason : undefined"
|
|
1589
|
+
:class="approveBlockedReason ? 'h-auto cursor-not-allowed items-start' : undefined"
|
|
1590
|
+
@select="(event) => (approveBlockedReason ? event.preventDefault() : run('approve'))"
|
|
1591
|
+
>
|
|
1592
|
+
<span
|
|
1593
|
+
aria-hidden="true"
|
|
1594
|
+
class="size-2 shrink-0 rounded-full bg-background-status-success"
|
|
1595
|
+
:class="approveBlockedReason ? 'mt-1.5 opacity-50' : undefined"
|
|
1596
|
+
/>
|
|
1597
|
+
<span class="flex min-w-0 flex-1 flex-col gap-0.5">
|
|
1598
|
+
<span :class="approveBlockedReason ? 'text-content-on-surface-muted' : undefined">Success</span>
|
|
1599
|
+
<span v-if="approveBlockedReason" class="body-xs-regular text-content-on-surface-secondary">
|
|
1600
|
+
{{ approveBlockedReason }}
|
|
1601
|
+
</span>
|
|
1602
|
+
</span>
|
|
1603
|
+
<UiIcon
|
|
1604
|
+
v-if="approveBlockedReason"
|
|
1605
|
+
:name="BanIcon"
|
|
1606
|
+
:size="16"
|
|
1607
|
+
aria-hidden="true"
|
|
1608
|
+
class="mt-0.5 shrink-0 text-content-on-surface-muted"
|
|
1609
|
+
/>
|
|
1610
|
+
</UiDropdownMenuItem>
|
|
1611
|
+
<UiDropdownMenuItem @select="run('mark-pending')">
|
|
1612
|
+
<span aria-hidden="true" class="size-2 shrink-0 rounded-full bg-background-status-warning" />
|
|
1613
|
+
Pending
|
|
1614
|
+
</UiDropdownMenuItem>
|
|
1615
|
+
<UiDropdownMenuSeparator />
|
|
1616
|
+
<UiDropdownMenuItem
|
|
1617
|
+
:aria-label="approveBlockedReason ? 'Export, then mark as success, unavailable: ' + approveBlockedReason : undefined"
|
|
1618
|
+
:class="approveBlockedReason ? 'cursor-not-allowed' : undefined"
|
|
1619
|
+
@select="(event) => (approveBlockedReason ? event.preventDefault() : run(['export', 'approve']))"
|
|
1620
|
+
>
|
|
1621
|
+
<span class="flex-1" :class="approveBlockedReason ? 'text-content-on-surface-muted' : undefined">
|
|
1622
|
+
Export, then mark as success
|
|
1623
|
+
</span>
|
|
1624
|
+
<UiIcon
|
|
1625
|
+
v-if="approveBlockedReason"
|
|
1626
|
+
:name="BanIcon"
|
|
1627
|
+
:size="16"
|
|
1628
|
+
aria-hidden="true"
|
|
1629
|
+
class="shrink-0 text-content-on-surface-muted"
|
|
1630
|
+
/>
|
|
1631
|
+
</UiDropdownMenuItem>
|
|
1632
|
+
<template v-if="approveBlockedReason">
|
|
1633
|
+
<UiDropdownMenuSeparator />
|
|
1634
|
+
<UiDropdownMenuItem @select="(event) => { event.preventDefault(); deselectFailed(); }">
|
|
1635
|
+
<UiIcon :name="XIcon" :size="16" aria-hidden="true" class="shrink-0" />
|
|
1636
|
+
Deselect failed payments
|
|
1637
|
+
</UiDropdownMenuItem>
|
|
1638
|
+
</template>
|
|
1639
|
+
</UiDropdownMenuContent>
|
|
1640
|
+
</UiDropdownMenu>
|
|
1641
|
+
|
|
1642
|
+
<UiFloatActionItems
|
|
1643
|
+
type="label"
|
|
1644
|
+
:suffix-icon="DeleteIcon"
|
|
1645
|
+
:disabled="!!running"
|
|
1646
|
+
@click="run('remove-valuations')"
|
|
1647
|
+
>
|
|
1648
|
+
Remove valuations
|
|
1649
|
+
</UiFloatActionItems>
|
|
1650
|
+
|
|
1651
|
+
<UiFloatActionItems type="separator" />
|
|
1652
|
+
<UiTooltip content="Export">
|
|
1653
|
+
<UiFloatActionItems
|
|
1654
|
+
type="icon"
|
|
1655
|
+
:icon="UploadIcon"
|
|
1656
|
+
aria-label="Export"
|
|
1657
|
+
:disabled="!!running"
|
|
1658
|
+
@click="run('export')"
|
|
1659
|
+
/>
|
|
1660
|
+
</UiTooltip>
|
|
1661
|
+
<UiTooltip content="Delete">
|
|
1662
|
+
<UiFloatActionItems
|
|
1663
|
+
type="icon"
|
|
1664
|
+
:icon="Trash2Icon"
|
|
1665
|
+
aria-label="Delete"
|
|
1666
|
+
:disabled="!!running"
|
|
1667
|
+
@click="confirmDelete = true"
|
|
1668
|
+
/>
|
|
1669
|
+
</UiTooltip>
|
|
1670
|
+
</template>
|
|
1671
|
+
</UiBulkAction>
|
|
1672
|
+
|
|
1673
|
+
<p v-if="hiddenSelected > 0" role="status" class="body-s-regular text-content-on-surface-muted">
|
|
1674
|
+
{{ hiddenSelected }} selected {{ hiddenSelected === 1 ? 'row is' : 'rows are' }} hidden by the search and still included in bulk actions.
|
|
1675
|
+
</p>
|
|
1676
|
+
<UiDataTable ref="tableRef" :data="data" :columns="columns" :get-row-id="getRowId" />
|
|
1677
|
+
<UiDataTablePagination v-if="table" :table="table" :show-page-size="false" />
|
|
1678
|
+
</div>
|
|
1679
|
+
|
|
1680
|
+
<!-- Output -->
|
|
1681
|
+
<aside class="flex min-w-0 flex-col gap-4">
|
|
1682
|
+
<section class="flex flex-col gap-3 rounded-xl bg-background-surface-secondary p-4 shadow-lg">
|
|
1683
|
+
<div>
|
|
1684
|
+
<h3 class="body-m-medium text-content-on-surface-primary">Output</h3>
|
|
1685
|
+
<p class="body-s-regular text-content-on-surface-muted">Live values returned by useBulkAction().</p>
|
|
1686
|
+
</div>
|
|
1687
|
+
<dl class="grid grid-cols-2 gap-2">
|
|
1688
|
+
<div class="rounded-md bg-background-surface-muted p-3">
|
|
1689
|
+
<dt class="label-xs-medium font-mono text-content-on-surface-muted">selectedCount</dt>
|
|
1690
|
+
<dd class="heading-m text-content-on-surface-primary">{{ selectedCount }}</dd>
|
|
1691
|
+
</div>
|
|
1692
|
+
<div class="rounded-md bg-background-surface-muted p-3">
|
|
1693
|
+
<dt class="label-xs-medium font-mono text-content-on-surface-muted">hasSelection</dt>
|
|
1694
|
+
<dd class="pt-1">
|
|
1695
|
+
<UiBadge :variant="hasSelection ? 'success' : 'outline'" size="small">{{ hasSelection }}</UiBadge>
|
|
1696
|
+
</dd>
|
|
1697
|
+
</div>
|
|
1698
|
+
<div class="col-span-2 rounded-md bg-background-surface-muted p-3">
|
|
1699
|
+
<dt class="label-xs-medium font-mono text-content-on-surface-muted">selectedItems</dt>
|
|
1700
|
+
<dd class="flex flex-wrap gap-1 pt-1">
|
|
1701
|
+
<span v-if="!selectedItems.length" class="body-s-regular text-content-on-surface-muted">[ ]</span>
|
|
1702
|
+
<UiBadge v-for="email in selectedPreview" :key="email" variant="outline" size="small">{{ email }}</UiBadge>
|
|
1703
|
+
<UiBadge v-if="selectedItems.length > selectedPreview.length" variant="secondary" size="small">
|
|
1704
|
+
+{{ selectedItems.length - selectedPreview.length }} more
|
|
1705
|
+
</UiBadge>
|
|
1706
|
+
</dd>
|
|
1707
|
+
</div>
|
|
1708
|
+
</dl>
|
|
1709
|
+
</section>
|
|
1710
|
+
|
|
1711
|
+
<section class="flex flex-col gap-3 rounded-xl bg-background-surface-secondary p-4 shadow-lg">
|
|
1712
|
+
<div>
|
|
1713
|
+
<h3 class="body-m-medium text-content-on-surface-primary">Action calls</h3>
|
|
1714
|
+
<p class="body-s-regular text-content-on-surface-muted">
|
|
1715
|
+
What each action received. A group shows one entry per action, in the order they ran.
|
|
1716
|
+
</p>
|
|
1717
|
+
</div>
|
|
1718
|
+
<p v-if="!log.length" class="body-s-regular rounded-md border border-dashed border-border-surface-default p-4 text-center text-content-on-surface-muted">
|
|
1719
|
+
No calls yet — select rows and run an action.
|
|
1720
|
+
</p>
|
|
1721
|
+
<div v-else role="log" aria-label="Action calls">
|
|
1722
|
+
<ol class="flex flex-col gap-2">
|
|
1723
|
+
<li v-for="entry in log" :key="entry.id" class="flex flex-col gap-1 rounded-md bg-background-surface-muted px-3 py-2">
|
|
1724
|
+
<div class="flex items-center justify-between gap-2">
|
|
1725
|
+
<code class="label-s-medium font-mono text-content-on-surface-primary">
|
|
1726
|
+
{{ entry.key === 'onReset' ? 'onReset()' : "action('" + entry.key + "')" }}
|
|
1727
|
+
</code>
|
|
1728
|
+
<UiBadge :variant="entry.status === 'running' ? 'warning' : 'success'" size="small">
|
|
1729
|
+
{{ entry.status }}
|
|
1730
|
+
</UiBadge>
|
|
1731
|
+
</div>
|
|
1732
|
+
<p v-if="entry.key !== 'onReset'" class="body-xs-regular break-all text-content-on-surface-muted">
|
|
1733
|
+
items ({{ entry.items.length }}): {{ entry.items.join(', ') }}
|
|
1734
|
+
</p>
|
|
1735
|
+
<p v-if="entry.message" class="body-xs-regular text-content-on-surface-primary">→ {{ entry.message }}</p>
|
|
1736
|
+
</li>
|
|
1737
|
+
</ol>
|
|
1738
|
+
</div>
|
|
1739
|
+
</section>
|
|
1740
|
+
</aside>
|
|
1741
|
+
</div>
|
|
1742
|
+
</section>
|
|
1743
|
+
|
|
1744
|
+
<!-- Code -->
|
|
1745
|
+
<section class="flex flex-col gap-3">
|
|
1746
|
+
<div>
|
|
1747
|
+
<h2 class="heading-s text-content-on-surface-primary">Code</h2>
|
|
1748
|
+
<p class="body-s-regular text-content-on-surface-muted">Each step on its own, then the complete component.</p>
|
|
1749
|
+
</div>
|
|
1750
|
+
<UiTabs default-value="input">
|
|
1751
|
+
<UiTabsList>
|
|
1752
|
+
<UiTabsTrigger value="input">1 · Input</UiTabsTrigger>
|
|
1753
|
+
<UiTabsTrigger value="output">2 · Output</UiTabsTrigger>
|
|
1754
|
+
<UiTabsTrigger value="template">3 · Template</UiTabsTrigger>
|
|
1755
|
+
<UiTabsTrigger value="full">Full example</UiTabsTrigger>
|
|
1756
|
+
</UiTabsList>
|
|
1757
|
+
<UiTabsContent value="input" class="pt-3">
|
|
1758
|
+
<UiCodeBlock :code="code.input" language="ts" filename="useBulkAction — options" show-line-numbers />
|
|
1759
|
+
</UiTabsContent>
|
|
1760
|
+
<UiTabsContent value="output" class="pt-3">
|
|
1761
|
+
<UiCodeBlock :code="code.output" language="ts" filename="useBulkAction — return value" show-line-numbers />
|
|
1762
|
+
</UiTabsContent>
|
|
1763
|
+
<UiTabsContent value="template" class="pt-3">
|
|
1764
|
+
<UiCodeBlock :code="code.template" language="vue-html" filename="Template" show-line-numbers />
|
|
1765
|
+
</UiTabsContent>
|
|
1766
|
+
<UiTabsContent value="full" class="pt-3">
|
|
1767
|
+
<UiCodeBlock :code="fullSource" language="vue" filename="PaymentsBulkActions.vue" show-line-numbers max-height="560px" />
|
|
1768
|
+
</UiTabsContent>
|
|
1769
|
+
</UiTabs>
|
|
1770
|
+
</section>
|
|
1771
|
+
|
|
1772
|
+
<UiAlertDialog v-model:model="confirmDelete">
|
|
1773
|
+
<UiAlertDialogContent>
|
|
1774
|
+
<UiAlertDialogHeader>
|
|
1775
|
+
<UiAlertDialogTitle>Delete {{ selectedCount }} payments?</UiAlertDialogTitle>
|
|
1776
|
+
<UiAlertDialogDescription>
|
|
1777
|
+
This removes the selected payments, including any hidden by the search. This cannot be undone.
|
|
1778
|
+
</UiAlertDialogDescription>
|
|
1779
|
+
</UiAlertDialogHeader>
|
|
1780
|
+
<UiAlertDialogFooter>
|
|
1781
|
+
<UiAlertDialogCancel>Cancel</UiAlertDialogCancel>
|
|
1782
|
+
<UiAlertDialogAction @click="run('delete')">Delete</UiAlertDialogAction>
|
|
1783
|
+
</UiAlertDialogFooter>
|
|
1784
|
+
</UiAlertDialogContent>
|
|
1785
|
+
</UiAlertDialog>
|
|
1786
|
+
</div>
|
|
1787
|
+
`,
|
|
1788
|
+
}),
|
|
1789
|
+
play: async ({ canvasElement, step }) => {
|
|
1790
|
+
// Scenario: an action that does not apply to the selection stays
|
|
1791
|
+
// focusable and explains why, a group runs in order, and delete asks first.
|
|
1792
|
+
const canvas = within(canvasElement);
|
|
1793
|
+
const page = within(document.body);
|
|
1794
|
+
const selectRow = async (email: string) =>
|
|
1795
|
+
userEvent.click(
|
|
1796
|
+
await canvas.findByRole('checkbox', { name: `Select ${email}` }),
|
|
1797
|
+
);
|
|
1798
|
+
const openChangeStatus = async () => {
|
|
1799
|
+
await userEvent.click(
|
|
1800
|
+
canvas.getByRole('button', { name: 'Change status' }),
|
|
1801
|
+
);
|
|
1802
|
+
return page.findByRole('menu');
|
|
1803
|
+
};
|
|
1804
|
+
|
|
1805
|
+
await step(
|
|
1806
|
+
'interaction: Success is unavailable while a failed payment is selected, and says why',
|
|
1807
|
+
async () => {
|
|
1808
|
+
await selectRow('jane.johnson@yahoo.com');
|
|
1809
|
+
await selectRow('alice.brown@outlook.com');
|
|
1810
|
+
const menu = within(await openChangeStatus());
|
|
1811
|
+
const success = menu.getByRole('menuitem', {
|
|
1812
|
+
name: 'Success, unavailable: 1 failed payment selected. Deselect it to continue.',
|
|
1813
|
+
});
|
|
1814
|
+
await expect(success).toHaveTextContent(
|
|
1815
|
+
'1 failed payment selected. Deselect it to continue.',
|
|
1816
|
+
);
|
|
1817
|
+
await expect(
|
|
1818
|
+
menu.getByRole('menuitem', {
|
|
1819
|
+
name: /^Export, then mark as success, unavailable:/,
|
|
1820
|
+
}),
|
|
1821
|
+
).toBeInTheDocument();
|
|
1822
|
+
await userEvent.click(success);
|
|
1823
|
+
await expect(page.getByRole('menu')).toBeVisible();
|
|
1824
|
+
await expect(canvas.queryByRole('log')).not.toBeInTheDocument();
|
|
1825
|
+
},
|
|
1826
|
+
);
|
|
1827
|
+
|
|
1828
|
+
await step(
|
|
1829
|
+
'interaction: Deselect failed payments makes Success available again',
|
|
1830
|
+
async () => {
|
|
1831
|
+
await userEvent.click(
|
|
1832
|
+
page.getByRole('menuitem', { name: 'Deselect failed payments' }),
|
|
1833
|
+
);
|
|
1834
|
+
await expect(
|
|
1835
|
+
page.getByRole('menuitem', { name: 'Success' }),
|
|
1836
|
+
).toBeInTheDocument();
|
|
1837
|
+
await userEvent.keyboard('{Escape}');
|
|
1838
|
+
await waitFor(() =>
|
|
1839
|
+
expect(page.queryByRole('menu')).not.toBeInTheDocument(),
|
|
1840
|
+
);
|
|
1841
|
+
await expect(
|
|
1842
|
+
canvas.getByRole('checkbox', {
|
|
1843
|
+
name: 'Select alice.brown@outlook.com',
|
|
1844
|
+
}),
|
|
1845
|
+
).toHaveAttribute('aria-checked', 'false');
|
|
1846
|
+
},
|
|
1847
|
+
);
|
|
1848
|
+
|
|
1849
|
+
await step(
|
|
1850
|
+
'interaction: a group action runs export, then approve, on the same rows',
|
|
1851
|
+
async () => {
|
|
1852
|
+
await selectRow('john.smith@gmail.com');
|
|
1853
|
+
await selectRow('bob.williams@hotmail.com');
|
|
1854
|
+
const menu = within(await openChangeStatus());
|
|
1855
|
+
await userEvent.click(
|
|
1856
|
+
menu.getByRole('menuitem', { name: 'Export, then mark as success' }),
|
|
1857
|
+
);
|
|
1858
|
+
const log = await canvas.findByRole('log');
|
|
1859
|
+
await waitFor(
|
|
1860
|
+
() => {
|
|
1861
|
+
const entries = within(log).getAllByRole('listitem');
|
|
1862
|
+
expect(entries).toHaveLength(2);
|
|
1863
|
+
expect(entries[0]).toHaveTextContent("action('approve')");
|
|
1864
|
+
expect(entries[0]).toHaveTextContent('done');
|
|
1865
|
+
expect(entries[1]).toHaveTextContent("action('export')");
|
|
1866
|
+
expect(entries[1]).toHaveTextContent(
|
|
1867
|
+
"1 pending payment can't be exported: john.smith@gmail.com",
|
|
1868
|
+
);
|
|
1869
|
+
},
|
|
1870
|
+
{ timeout: 4000 },
|
|
1871
|
+
);
|
|
1872
|
+
},
|
|
1873
|
+
);
|
|
1874
|
+
|
|
1875
|
+
await step(
|
|
1876
|
+
'interaction: Delete asks for confirmation and Cancel keeps the rows',
|
|
1877
|
+
async () => {
|
|
1878
|
+
await userEvent.click(canvas.getByRole('button', { name: 'Delete' }));
|
|
1879
|
+
const dialog = await page.findByRole('alertdialog', {
|
|
1880
|
+
name: 'Delete 3 payments?',
|
|
1881
|
+
});
|
|
1882
|
+
await userEvent.click(
|
|
1883
|
+
within(dialog).getByRole('button', { name: 'Cancel' }),
|
|
1884
|
+
);
|
|
1885
|
+
await waitFor(() =>
|
|
1886
|
+
expect(page.queryByRole('alertdialog')).not.toBeInTheDocument(),
|
|
1887
|
+
);
|
|
1888
|
+
await expect(
|
|
1889
|
+
canvas.getByRole('checkbox', { name: 'Select john.smith@gmail.com' }),
|
|
1890
|
+
).toHaveAttribute('aria-checked', 'true');
|
|
1891
|
+
},
|
|
1892
|
+
);
|
|
1893
|
+
},
|
|
1894
|
+
};
|
|
1895
|
+
|
|
1896
|
+
/*
|
|
1897
|
+
* Automated accessibility tests
|
|
1898
|
+
*
|
|
1899
|
+
* Hidden, test-only stories (`name: 'test - …'`, `tags: ['!dev', '!autodocs']`)
|
|
1900
|
+
* that run in the test-runner alongside the axe audit. Together with the
|
|
1901
|
+
* co-located `interaction:` plays above they cover all five categories:
|
|
1902
|
+
* keyboard, screen reader, zoom and mobile.
|
|
1903
|
+
*/
|
|
1904
|
+
|
|
1905
|
+
const onTestReset = fn().mockName('reset');
|
|
1906
|
+
const onTestExport = fn().mockName('export');
|
|
1907
|
+
|
|
1908
|
+
const testBarRender = () => ({
|
|
1909
|
+
components: { UiBulkAction, UiFloatActionItems },
|
|
1910
|
+
setup() {
|
|
1911
|
+
const checked = ref<boolean | 'indeterminate'>('indeterminate');
|
|
1912
|
+
return { checked, onTestReset, onTestExport, MoreHorizontalIcon };
|
|
1913
|
+
},
|
|
1914
|
+
template: `
|
|
1915
|
+
<div class="flex min-h-48 items-center justify-center p-8">
|
|
1916
|
+
<UiBulkAction v-model:checked="checked" :selected-count="3" @reset="onTestReset" v-slot="{ selectedCount }">
|
|
1917
|
+
<span role="status">
|
|
1918
|
+
<UiFloatActionItems type="text">{{ selectedCount }} selected</UiFloatActionItems>
|
|
1919
|
+
</span>
|
|
1920
|
+
<UiFloatActionItems type="separator" />
|
|
1921
|
+
<UiFloatActionItems type="label" @click="onTestExport">Export</UiFloatActionItems>
|
|
1922
|
+
<UiFloatActionItems type="icon" :icon="MoreHorizontalIcon" aria-label="More actions" />
|
|
1923
|
+
</UiBulkAction>
|
|
1924
|
+
</div>
|
|
1925
|
+
`,
|
|
1926
|
+
});
|
|
1927
|
+
|
|
1928
|
+
/**
|
|
1929
|
+
* Keyboard support: the select-all checkbox is its own Tab stop, then one Tab
|
|
1930
|
+
* enters the toolbar, where Arrow keys, Home and End move roving focus and
|
|
1931
|
+
* Enter / Space activate. Hidden test-only story.
|
|
1932
|
+
*/
|
|
1933
|
+
export const KeyboardNavigation: Story = {
|
|
1934
|
+
name: 'test - Keyboard navigation',
|
|
1935
|
+
tags: ['!dev', '!autodocs'],
|
|
1936
|
+
render: testBarRender,
|
|
1937
|
+
parameters: {
|
|
1938
|
+
chromatic: { disableSnapshot: true },
|
|
1939
|
+
},
|
|
1940
|
+
play: async ({ canvasElement, step }) => {
|
|
1941
|
+
onTestReset.mockClear();
|
|
1942
|
+
onTestExport.mockClear();
|
|
1943
|
+
const canvas = within(canvasElement);
|
|
1944
|
+
const selectAll = canvas.getByRole('checkbox', {
|
|
1945
|
+
name: 'Select all rows on this page',
|
|
1946
|
+
});
|
|
1947
|
+
const exportButton = canvas.getByRole('button', { name: 'Export' });
|
|
1948
|
+
const more = canvas.getByRole('button', { name: 'More actions' });
|
|
1949
|
+
const reset = canvas.getByRole('button', { name: 'Reset selection' });
|
|
1950
|
+
|
|
1951
|
+
await step(
|
|
1952
|
+
'keyboard: Tab reaches the select-all checkbox first and Space toggles it (WCAG 2.1.1)',
|
|
1953
|
+
async () => {
|
|
1954
|
+
await userEvent.tab();
|
|
1955
|
+
await expect(selectAll).toHaveFocus();
|
|
1956
|
+
await userEvent.keyboard(' ');
|
|
1957
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'true');
|
|
1958
|
+
},
|
|
1959
|
+
);
|
|
1960
|
+
|
|
1961
|
+
await step(
|
|
1962
|
+
'keyboard: the next Tab enters the actions, Arrow keys move between them (WCAG 2.1.1)',
|
|
1963
|
+
async () => {
|
|
1964
|
+
await userEvent.tab();
|
|
1965
|
+
await expect(exportButton).toHaveFocus();
|
|
1966
|
+
await userEvent.keyboard('{ArrowRight}');
|
|
1967
|
+
await expect(more).toHaveFocus();
|
|
1968
|
+
await userEvent.keyboard('{ArrowLeft}');
|
|
1969
|
+
await expect(exportButton).toHaveFocus();
|
|
1970
|
+
},
|
|
1971
|
+
);
|
|
1972
|
+
|
|
1973
|
+
await step(
|
|
1974
|
+
'keyboard: End moves to reset and Home back to the first action (WCAG 2.4.3)',
|
|
1975
|
+
async () => {
|
|
1976
|
+
await userEvent.keyboard('{End}');
|
|
1977
|
+
await expect(reset).toHaveFocus();
|
|
1978
|
+
await userEvent.keyboard('{Home}');
|
|
1979
|
+
await expect(exportButton).toHaveFocus();
|
|
1980
|
+
},
|
|
1981
|
+
);
|
|
1982
|
+
|
|
1983
|
+
await step(
|
|
1984
|
+
'keyboard: Enter activates an action and reset (WCAG 2.1.1)',
|
|
1985
|
+
async () => {
|
|
1986
|
+
await userEvent.keyboard('{Enter}');
|
|
1987
|
+
await expect(onTestExport).toHaveBeenCalledTimes(1);
|
|
1988
|
+
await userEvent.keyboard('{End}');
|
|
1989
|
+
await userEvent.keyboard('{Enter}');
|
|
1990
|
+
await expect(onTestReset).toHaveBeenCalledTimes(1);
|
|
1991
|
+
},
|
|
1992
|
+
);
|
|
1993
|
+
|
|
1994
|
+
await step(
|
|
1995
|
+
'keyboard: Shift+Tab returns from the toolbar to the checkbox (WCAG 2.4.3)',
|
|
1996
|
+
async () => {
|
|
1997
|
+
await userEvent.tab({ shift: true });
|
|
1998
|
+
await expect(selectAll).toHaveFocus();
|
|
1999
|
+
},
|
|
2000
|
+
);
|
|
2001
|
+
},
|
|
2002
|
+
};
|
|
2003
|
+
|
|
2004
|
+
/**
|
|
2005
|
+
* Screen reader support: the toolbar, the select-all checkbox and the reset
|
|
2006
|
+
* button are named, the partial selection is reported as mixed, the count is a
|
|
2007
|
+
* live status, and every built-in label can be overridden. Hidden test-only
|
|
2008
|
+
* story.
|
|
2009
|
+
*/
|
|
2010
|
+
export const ScreenReader: Story = {
|
|
2011
|
+
name: 'test - Screen reader',
|
|
2012
|
+
tags: ['!dev', '!autodocs'],
|
|
2013
|
+
render: () => ({
|
|
2014
|
+
components: { UiBulkAction, UiFloatActionItems },
|
|
2015
|
+
template: `
|
|
2016
|
+
<div class="flex flex-col items-start gap-6 p-8">
|
|
2017
|
+
<UiBulkAction checked="indeterminate" :selected-count="2" v-slot="{ selectedCount }">
|
|
2018
|
+
<span role="status">
|
|
2019
|
+
<UiFloatActionItems type="text">{{ selectedCount }} selected</UiFloatActionItems>
|
|
2020
|
+
</span>
|
|
2021
|
+
</UiBulkAction>
|
|
2022
|
+
<UiBulkAction
|
|
2023
|
+
aria-label="Bulk invoice actions"
|
|
2024
|
+
select-all-label="Select all invoices"
|
|
2025
|
+
reset-label="Clear invoice selection"
|
|
2026
|
+
/>
|
|
2027
|
+
</div>
|
|
2028
|
+
`,
|
|
2029
|
+
}),
|
|
2030
|
+
play: async ({ canvasElement, step }) => {
|
|
2031
|
+
const canvas = within(canvasElement);
|
|
2032
|
+
const toolbar = canvas.getByRole('toolbar', { name: 'Bulk actions' });
|
|
2033
|
+
|
|
2034
|
+
await step(
|
|
2035
|
+
'screen-reader: the bar is a toolbar named "Bulk actions" (WCAG 4.1.2)',
|
|
2036
|
+
async () => {
|
|
2037
|
+
await expect(toolbar).toHaveAttribute('aria-orientation', 'horizontal');
|
|
2038
|
+
},
|
|
2039
|
+
);
|
|
2040
|
+
|
|
2041
|
+
await step(
|
|
2042
|
+
'screen-reader: the select-all checkbox is named and reports a partial selection as mixed (WCAG 4.1.2)',
|
|
2043
|
+
async () => {
|
|
2044
|
+
const selectAll = within(toolbar).getByRole('checkbox', {
|
|
2045
|
+
name: 'Select all rows on this page',
|
|
2046
|
+
});
|
|
2047
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'mixed');
|
|
2048
|
+
},
|
|
2049
|
+
);
|
|
2050
|
+
|
|
2051
|
+
await step(
|
|
2052
|
+
'screen-reader: the icon-only reset button has an accessible name (WCAG 1.1.1)',
|
|
2053
|
+
async () => {
|
|
2054
|
+
const reset = within(toolbar).getByRole('button', {
|
|
2055
|
+
name: 'Reset selection',
|
|
2056
|
+
});
|
|
2057
|
+
await expect(reset.querySelector('svg')).toHaveAttribute(
|
|
2058
|
+
'aria-hidden',
|
|
2059
|
+
'true',
|
|
2060
|
+
);
|
|
2061
|
+
},
|
|
2062
|
+
);
|
|
2063
|
+
|
|
2064
|
+
await step(
|
|
2065
|
+
'screen-reader: the selected count is announced through role="status" (WCAG 4.1.3)',
|
|
2066
|
+
async () => {
|
|
2067
|
+
await expect(within(toolbar).getByRole('status')).toHaveTextContent(
|
|
2068
|
+
'2 selected',
|
|
2069
|
+
);
|
|
2070
|
+
},
|
|
2071
|
+
);
|
|
2072
|
+
|
|
2073
|
+
await step(
|
|
2074
|
+
'screen-reader: ariaLabel, selectAllLabel and resetLabel override the built-in names',
|
|
2075
|
+
async () => {
|
|
2076
|
+
const custom = canvas.getByRole('toolbar', {
|
|
2077
|
+
name: 'Bulk invoice actions',
|
|
2078
|
+
});
|
|
2079
|
+
await expect(
|
|
2080
|
+
within(custom).getByRole('checkbox', { name: 'Select all invoices' }),
|
|
2081
|
+
).toHaveAttribute('aria-checked', 'false');
|
|
2082
|
+
await expect(
|
|
2083
|
+
within(custom).getByRole('button', {
|
|
2084
|
+
name: 'Clear invoice selection',
|
|
2085
|
+
}),
|
|
2086
|
+
).toBeInTheDocument();
|
|
2087
|
+
},
|
|
2088
|
+
);
|
|
2089
|
+
},
|
|
2090
|
+
};
|
|
2091
|
+
|
|
2092
|
+
/**
|
|
2093
|
+
* At 200% zoom the bar stays visible, keeps its names and remains operable.
|
|
2094
|
+
* Hidden test-only story.
|
|
2095
|
+
*/
|
|
2096
|
+
export const ZoomMagnification: Story = {
|
|
2097
|
+
name: 'test - Zoom magnification',
|
|
2098
|
+
tags: ['!dev', '!autodocs'],
|
|
2099
|
+
globals: { zoom: '200' },
|
|
2100
|
+
render: testBarRender,
|
|
2101
|
+
parameters: {
|
|
2102
|
+
// The accessible-reflow Chromatic mode already covers magnified snapshots.
|
|
2103
|
+
chromatic: { disableSnapshot: true },
|
|
2104
|
+
docs: {
|
|
2105
|
+
description: {
|
|
2106
|
+
story:
|
|
2107
|
+
'Use the Zoom toolbar (200% resize text, 400% reflow) to magnify the preview.',
|
|
2108
|
+
},
|
|
2109
|
+
},
|
|
2110
|
+
},
|
|
2111
|
+
play: async ({ canvasElement, step }) => {
|
|
2112
|
+
onTestReset.mockClear();
|
|
2113
|
+
onTestExport.mockClear();
|
|
2114
|
+
const canvas = within(canvasElement);
|
|
2115
|
+
const selectAll = canvas.getByRole('checkbox', {
|
|
2116
|
+
name: 'Select all rows on this page',
|
|
2117
|
+
});
|
|
2118
|
+
|
|
2119
|
+
await step(
|
|
2120
|
+
'zoom: preview is magnified to 200% (WCAG 1.4.4 Resize Text)',
|
|
2121
|
+
async () => {
|
|
2122
|
+
await expect(document.documentElement.style.zoom).toBe('200%');
|
|
2123
|
+
},
|
|
2124
|
+
);
|
|
2125
|
+
|
|
2126
|
+
await step(
|
|
2127
|
+
'zoom: the checkbox, actions and reset stay visible with their names',
|
|
2128
|
+
async () => {
|
|
2129
|
+
await expect(
|
|
2130
|
+
canvas.getByRole('toolbar', { name: 'Bulk actions' }),
|
|
2131
|
+
).toBeVisible();
|
|
2132
|
+
await expect(selectAll).toBeVisible();
|
|
2133
|
+
await expect(
|
|
2134
|
+
canvas.getByRole('button', { name: 'Export' }),
|
|
2135
|
+
).toBeVisible();
|
|
2136
|
+
await expect(
|
|
2137
|
+
canvas.getByRole('button', { name: 'Reset selection' }),
|
|
2138
|
+
).toBeVisible();
|
|
2139
|
+
},
|
|
2140
|
+
);
|
|
2141
|
+
|
|
2142
|
+
await step('zoom: the bar remains operable when magnified', async () => {
|
|
2143
|
+
await userEvent.click(selectAll);
|
|
2144
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'true');
|
|
2145
|
+
await userEvent.click(canvas.getByRole('button', { name: 'Export' }));
|
|
2146
|
+
await expect(onTestExport).toHaveBeenCalledTimes(1);
|
|
2147
|
+
await userEvent.click(
|
|
2148
|
+
canvas.getByRole('button', { name: 'Reset selection' }),
|
|
2149
|
+
);
|
|
2150
|
+
await expect(onTestReset).toHaveBeenCalledTimes(1);
|
|
2151
|
+
});
|
|
2152
|
+
},
|
|
2153
|
+
};
|
|
2154
|
+
|
|
2155
|
+
/**
|
|
2156
|
+
* At the 320px breakpoint the bar fits the screen, every control meets the
|
|
2157
|
+
* touch-target size, and the whole checkbox cell toggles the selection.
|
|
2158
|
+
* Hidden test-only story.
|
|
2159
|
+
*/
|
|
2160
|
+
export const MobileBreakpoint: Story = {
|
|
2161
|
+
name: 'test - Mobile breakpoint',
|
|
2162
|
+
tags: ['!dev', '!autodocs'],
|
|
2163
|
+
globals: { mediaBreakpoint: 'reflow' },
|
|
2164
|
+
render: testBarRender,
|
|
2165
|
+
parameters: {
|
|
2166
|
+
// Reflow snapshots are covered by the accessible-reflow Chromatic mode,
|
|
2167
|
+
// and the play moves focus, so skip this snapshot.
|
|
2168
|
+
chromatic: { disableSnapshot: true },
|
|
2169
|
+
docs: {
|
|
2170
|
+
description: {
|
|
2171
|
+
story:
|
|
2172
|
+
'Switch the Media breakpoint toolbar to Mobile to preview the 320px layout.',
|
|
2173
|
+
},
|
|
2174
|
+
},
|
|
2175
|
+
},
|
|
2176
|
+
play: async ({ canvasElement, step }) => {
|
|
2177
|
+
const canvas = within(canvasElement);
|
|
2178
|
+
const toolbar = canvas.getByRole('toolbar', { name: 'Bulk actions' });
|
|
2179
|
+
const selectAll = canvas.getByRole('checkbox', {
|
|
2180
|
+
name: 'Select all rows on this page',
|
|
2181
|
+
});
|
|
2182
|
+
const checkboxCell = selectAll.closest('label');
|
|
2183
|
+
if (!checkboxCell) {
|
|
2184
|
+
throw new Error('The select-all checkbox is not wrapped in its label');
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
await step(
|
|
2188
|
+
'mobile: the bar is visible and fits the 320px viewport (WCAG 1.4.10)',
|
|
2189
|
+
async () => {
|
|
2190
|
+
await expect(toolbar).toBeVisible();
|
|
2191
|
+
await expect(toolbar.getBoundingClientRect().right).toBeLessThanOrEqual(
|
|
2192
|
+
window.innerWidth,
|
|
2193
|
+
);
|
|
2194
|
+
await expect(document.documentElement.scrollWidth).toBeLessThanOrEqual(
|
|
2195
|
+
window.innerWidth,
|
|
2196
|
+
);
|
|
2197
|
+
},
|
|
2198
|
+
);
|
|
2199
|
+
|
|
2200
|
+
await step(
|
|
2201
|
+
'mobile: every button and the checkbox cell meet the 24px touch target (WCAG 2.5.8)',
|
|
2202
|
+
async () => {
|
|
2203
|
+
const targets = [checkboxCell, ...canvas.getAllByRole('button')];
|
|
2204
|
+
for (const target of targets) {
|
|
2205
|
+
const { width, height } = target.getBoundingClientRect();
|
|
2206
|
+
await expect(width).toBeGreaterThanOrEqual(24);
|
|
2207
|
+
await expect(height).toBeGreaterThanOrEqual(24);
|
|
2208
|
+
}
|
|
2209
|
+
},
|
|
2210
|
+
);
|
|
2211
|
+
|
|
2212
|
+
await step(
|
|
2213
|
+
'mobile: tapping anywhere in the checkbox cell toggles the selection',
|
|
2214
|
+
async () => {
|
|
2215
|
+
await expect(selectAll).toHaveAttribute('aria-checked', 'mixed');
|
|
2216
|
+
checkboxCell.click();
|
|
2217
|
+
await waitFor(() =>
|
|
2218
|
+
expect(selectAll).toHaveAttribute('aria-checked', 'true'),
|
|
2219
|
+
);
|
|
2220
|
+
},
|
|
2221
|
+
);
|
|
2222
|
+
|
|
2223
|
+
await step(
|
|
2224
|
+
'mobile: stays keyboard operable — Tab enters the toolbar and Arrow keys move focus (WCAG 2.1.1)',
|
|
2225
|
+
async () => {
|
|
2226
|
+
selectAll.focus();
|
|
2227
|
+
await userEvent.tab();
|
|
2228
|
+
await expect(
|
|
2229
|
+
canvas.getByRole('button', { name: 'Export' }),
|
|
2230
|
+
).toHaveFocus();
|
|
2231
|
+
await userEvent.keyboard('{ArrowRight}');
|
|
2232
|
+
await expect(
|
|
2233
|
+
canvas.getByRole('button', { name: 'More actions' }),
|
|
2234
|
+
).toHaveFocus();
|
|
2235
|
+
},
|
|
2236
|
+
);
|
|
2237
|
+
},
|
|
2238
|
+
};
|