compote-ui 0.83.0 → 0.85.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/components/data-table-v9/create-table.svelte.js +55 -5
  2. package/dist/components/data-table-v9/export/csv-exporter.d.ts +14 -0
  3. package/dist/components/data-table-v9/export/csv-exporter.js +43 -0
  4. package/dist/components/data-table-v9/export/export-data.d.ts +98 -0
  5. package/dist/components/data-table-v9/export/export-data.js +55 -0
  6. package/dist/components/data-table-v9/export/types.d.ts +23 -0
  7. package/dist/components/data-table-v9/export/types.js +1 -0
  8. package/dist/components/data-table-v9/index.d.ts +6 -0
  9. package/dist/components/data-table-v9/index.js +4 -0
  10. package/dist/components/data-table-v9/toolbar/data-table-export.svelte +146 -0
  11. package/dist/components/data-table-v9/toolbar/data-table-export.svelte.d.ts +35 -0
  12. package/dist/components/data-table-v9/types.d.ts +23 -0
  13. package/dist/components/data-table-v9/virtual/index.d.ts +2 -2
  14. package/dist/components/data-table-v9/virtual/index.js +1 -1
  15. package/dist/components/data-table-v9/xlsx/index.d.ts +29 -0
  16. package/dist/components/data-table-v9/xlsx/index.js +84 -0
  17. package/dist/components/data-table-v9/xlsx/xlsx-cell.d.ts +51 -0
  18. package/dist/components/data-table-v9/xlsx/xlsx-cell.js +123 -0
  19. package/dist/icons/PhDownloadSimple.svelte +18 -0
  20. package/dist/icons/PhDownloadSimple.svelte.d.ts +5 -0
  21. package/dist/icons/index.d.ts +1 -0
  22. package/dist/icons/index.js +1 -0
  23. package/dist/utils/csv.d.ts +13 -0
  24. package/dist/utils/csv.js +18 -0
  25. package/dist/utils/download.d.ts +5 -0
  26. package/dist/utils/download.js +17 -0
  27. package/dist/utils/select-filter.js +6 -1
  28. package/package.json +13 -3
  29. package/skills/data-table/SKILL.md +42 -4
  30. package/skills/data-table/references/column-options.md +4 -0
  31. package/skills/data-table/references/export.md +160 -0
@@ -0,0 +1,160 @@
1
+ # Export
2
+
3
+ `DataTable.Export` downloads the table as a file. It sits in the toolbar like `ColumnVisibility`
4
+ and takes the `table`.
5
+
6
+ ```svelte
7
+ <script lang="ts">
8
+ import * as DataTable from 'compote-ui/data-table';
9
+ </script>
10
+
11
+ <DataTable.Toolbar>
12
+ {#snippet right()}
13
+ <DataTable.Export {table} filename="invoices" />
14
+ <DataTable.ColumnVisibility {table} />
15
+ {/snippet}
16
+ </DataTable.Toolbar>
17
+ ```
18
+
19
+ Props:
20
+
21
+ - `table: DataTableInstance<T>`
22
+ - `exporters?: DataTableExporter[]` — defaults to `[csvExporter]`.
23
+ - `filename?: string` — without extension, defaults to `'export'`.
24
+ - `triggerLabel?: string` — icon-only with a tooltip when omitted.
25
+
26
+ One exporter and no selected rows renders a plain button. More exporters, or any selected row,
27
+ renders a menu. When rows are selected the menu offers **All rows (n)** / **Selected rows (k)**.
28
+ `k` counts the selected rows the current filter keeps, exactly what gets exported.
29
+
30
+ ## What gets exported
31
+
32
+ - **Columns:** the visible ones, in the order the table shows them (pinned left, center, pinned
33
+ right). Columns with `enableExport: false` are skipped, and so are `type: 'action'` columns by
34
+ default. A grouped column's header is `Group / Column`.
35
+ - **Rows:** the filtered and sorted rows (`table.getRowModel()`). The table has no pagination,
36
+ so this is every matching row, not one page.
37
+ - **Cells:** the text the table shows (`cell`, else type formatting; `boolean`, `url` and
38
+ `phone` as the table draws them, which ignores `cell`). Empty cells are blank, not `-`. A
39
+ column with `exportValue` and no `cell` formats `exportValue` output by its `type`.
40
+
41
+ ## CSV
42
+
43
+ `csvExporter` writes a UTF-8 BOM (so Excel reads č/ć/š/đ/ž correctly), `;` as the separator
44
+ (what Excel expects under most European locales), and CRLF line endings. Fields starting with
45
+ `= + - @`, tab or CR get a `'` prefix so spreadsheets don't run them as formulas. Exempt, so
46
+ negative numbers and phone numbers stay as they are: text made only of digits, spaces and number
47
+ punctuation (`-5`, `+381 60 123`), and number, currency and percent columns without their own
48
+ `cell`.
49
+
50
+ ```ts
51
+ import { createCsvExporter } from 'compote-ui/data-table';
52
+
53
+ const commaCsv = createCsvExporter({ id: 'csv-comma', label: 'CSV (comma)', separator: ',' });
54
+ // Also: bom?: boolean (default true)
55
+ ```
56
+
57
+ CSV holds the formatted text (`1,234.56 RSD`), so a spreadsheet can't sum it.
58
+
59
+ ## Excel (.xlsx)
60
+
61
+ `xlsxExporter` lives in its own subpath and needs the optional peer `write-excel-file`
62
+ (about 19 KB gzip, loaded only when an export runs, so importing the subpath is SSR-safe):
63
+
64
+ ```sh
65
+ bun add write-excel-file
66
+ ```
67
+
68
+ ```svelte
69
+ <script lang="ts">
70
+ import * as DataTable from 'compote-ui/data-table';
71
+ import { xlsxExporter } from 'compote-ui/data-table/xlsx';
72
+ </script>
73
+
74
+ <DataTable.Export {table} filename="invoices" exporters={[DataTable.csvExporter, xlsxExporter]} />
75
+ ```
76
+
77
+ Cells stay computable in Excel:
78
+
79
+ - `number`, `currency`, `percent` → numbers, also from numeric strings (`"1234.56"`), with an
80
+ Excel format built from the column's Intl options: grouping, the decimals Intl would show,
81
+ `%`, and the currency code (`#,##0.00 "USD"`). Options Excel can't express (`notation`,
82
+ significant digits, `signDisplay`, `currencyDisplay: 'name'`, …) fall back to the text.
83
+ - `date`, `time`, `date-time` → Excel dates, on the day and time the table shows in the column's
84
+ zone (`formatOptions.timeZone`, else local). A `date` column drops the time; a `time` column
85
+ keeps only the time of day, so it sorts by time. Formats default to `dd.mm.yyyy`, `hh:mm` and
86
+ `dd.mm.yyyy hh:mm`.
87
+ - `boolean` → TRUE/FALSE for real `true`/`false`; other values are blank, as the table shows
88
+ `-` for them.
89
+ - Everything else, and any column with its own `cell` but no `exportValue`, is the text the
90
+ table shows. Typed cells can't hold formulas, so there is no `'` prefix.
91
+
92
+ The header row is bold and frozen. Each column is as wide as its header or its longest cell
93
+ text (longest line for multi-line text) plus 1 character, between 8 and 60
94
+ (`xlsxColumnWidths(data)` computes them).
95
+
96
+ ```ts
97
+ import { createXlsxExporter } from 'compote-ui/data-table/xlsx';
98
+
99
+ const excel = createXlsxExporter({
100
+ sheet: 'Invoices',
101
+ date: 'yyyy-mm-dd',
102
+ dateTime: 'yyyy-mm-dd hh:mm'
103
+ // also: time, id, label
104
+ });
105
+ ```
106
+
107
+ `exportDataToXlsxBlob(data, options)` returns the file as a `Blob` (e.g. to upload instead of
108
+ download), and `toXlsxCell` maps one cell.
109
+
110
+ ## Custom exporter
111
+
112
+ An exporter is `{ id, label, icon?, export(context) }`. `context` holds `table`, `scope`
113
+ (`'filtered' | 'selected'`), `filename` and `getData()`, which returns `{ columns, rows }`. Each
114
+ cell is `{ value, text }`. If `export` returns a promise, the trigger stays busy until it
115
+ settles. Errors are not caught.
116
+
117
+ Export on the server, from the table state:
118
+
119
+ ```ts
120
+ import type { DataTableExporter } from 'compote-ui/data-table';
121
+
122
+ const serverExcel: DataTableExporter = {
123
+ id: 'server-xlsx',
124
+ label: 'Excel',
125
+ export: async ({ table }) => {
126
+ const url = await requestExport({
127
+ sorting: table.atoms.sorting.get(),
128
+ columnFilters: table.atoms.columnFilters.get(),
129
+ globalFilter: table.atoms.globalFilter.get(),
130
+ columns: table.getVisibleLeafColumns().map((column) => column.id)
131
+ });
132
+ window.location.assign(url);
133
+ }
134
+ };
135
+ ```
136
+
137
+ Use a library the app already has instead of `write-excel-file` (SheetJS here):
138
+
139
+ ```ts
140
+ import { utils, writeFileXLSX } from 'xlsx';
141
+ import type { DataTableExporter } from 'compote-ui/data-table';
142
+
143
+ const sheetJs: DataTableExporter = {
144
+ id: 'xlsx',
145
+ label: 'Excel',
146
+ export: ({ filename, getData }) => {
147
+ const { columns, rows } = getData();
148
+ const sheet = utils.aoa_to_sheet([
149
+ columns.map((column) => column.header),
150
+ ...rows.map((cells) => cells.map((cell) => cell.text))
151
+ ]);
152
+ const book = utils.book_new();
153
+ utils.book_append_sheet(book, sheet, 'Sheet1');
154
+ writeFileXLSX(book, `${filename}.xlsx`);
155
+ }
156
+ };
157
+ ```
158
+
159
+ `getExportData(table, scope)`, `getExportRows(table, scope)` and `getExportColumns(table)` are
160
+ exported for building export UI outside `DataTable.Export`.