@snack-uikit/table 0.20.0 → 0.20.1-preview-e6ffbdb2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/exportTable.js +20 -11
- package/package.json +2 -2
- package/src/exportTable.ts +25 -11
package/dist/exportTable.js
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
import { utils as xlsxUtils, writeFileXLSX } from 'xlsx';
|
|
2
|
-
function
|
|
3
|
-
if (
|
|
4
|
-
return String(column
|
|
2
|
+
function getColumnValue(column, key) {
|
|
3
|
+
if (key in column && column[key] && typeof column[key] !== 'object' && typeof column[key] !== 'function') {
|
|
4
|
+
return String(column[key]);
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
7
|
function getFilteredColumnsIds(columnDefinitions) {
|
|
8
|
+
const accessorKey = 'accessorKey';
|
|
8
9
|
return columnDefinitions
|
|
9
|
-
.filter(column => { var _a; return
|
|
10
|
-
.map(column =>
|
|
10
|
+
.filter(column => { var _a; return getColumnValue(column, accessorKey) && !((_a = column.meta) === null || _a === void 0 ? void 0 : _a.skipOnExport); })
|
|
11
|
+
.map(column => getColumnValue(column, accessorKey));
|
|
12
|
+
}
|
|
13
|
+
function getFilteredColumnsHeaders(columnDefinitions) {
|
|
14
|
+
const accessorKey = 'accessorKey';
|
|
15
|
+
const header = 'header';
|
|
16
|
+
return columnDefinitions
|
|
17
|
+
.filter(column => { var _a; return getColumnValue(column, accessorKey) && !((_a = column.meta) === null || _a === void 0 ? void 0 : _a.skipOnExport); })
|
|
18
|
+
.map(column => getColumnValue(column, header) || getColumnValue(column, accessorKey));
|
|
11
19
|
}
|
|
12
20
|
function getXlsxFormatTable({ data, columnDefinitions, }) {
|
|
13
21
|
const filteredIds = getFilteredColumnsIds(columnDefinitions);
|
|
@@ -26,10 +34,11 @@ function getXlsxFormatTable({ data, columnDefinitions, }) {
|
|
|
26
34
|
}
|
|
27
35
|
export function exportToCSV({ columnDefinitions, fileName = 'Table', data, }) {
|
|
28
36
|
const xlsxData = getXlsxFormatTable({ data, columnDefinitions });
|
|
29
|
-
const
|
|
30
|
-
const table = [
|
|
31
|
-
const csv = table.map(line => line.map(el => `"${el}"`).join(',')).join('\n');
|
|
32
|
-
const
|
|
37
|
+
const headers = getFilteredColumnsHeaders(columnDefinitions);
|
|
38
|
+
const table = [headers, ...xlsxData];
|
|
39
|
+
const csv = table.map(line => line.map(el => (el === undefined ? `""` : `"${el}"`)).join(',')).join('\n');
|
|
40
|
+
const utf8Prefix = new Uint8Array([0xef, 0xbb, 0xbf]);
|
|
41
|
+
const blob = new Blob([utf8Prefix, csv], { type: 'text/csv' });
|
|
33
42
|
const url = window.URL.createObjectURL(blob);
|
|
34
43
|
const tempLink = Object.assign(document.createElement('a'), {
|
|
35
44
|
target: '_blank',
|
|
@@ -42,9 +51,9 @@ export function exportToCSV({ columnDefinitions, fileName = 'Table', data, }) {
|
|
|
42
51
|
}
|
|
43
52
|
export function exportToXLSX({ columnDefinitions, fileName = 'Table', data, }) {
|
|
44
53
|
const xlsxData = getXlsxFormatTable({ data, columnDefinitions });
|
|
45
|
-
const
|
|
54
|
+
const headers = getFilteredColumnsHeaders(columnDefinitions);
|
|
46
55
|
const workbook = xlsxUtils.book_new();
|
|
47
|
-
const worksheet = xlsxUtils.aoa_to_sheet([
|
|
56
|
+
const worksheet = xlsxUtils.aoa_to_sheet([headers, ...xlsxData]);
|
|
48
57
|
xlsxUtils.book_append_sheet(workbook, worksheet);
|
|
49
58
|
writeFileXLSX(workbook, `${fileName}.xlsx`);
|
|
50
59
|
return worksheet;
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
6
|
"title": "Table",
|
|
7
|
-
"version": "0.20.0",
|
|
7
|
+
"version": "0.20.1-preview-e6ffbdb2.0",
|
|
8
8
|
"sideEffects": [
|
|
9
9
|
"*.css",
|
|
10
10
|
"*.woff",
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"@snack-uikit/locale": "*"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "d42f6d50b6ef811271257f767a199907f8084bb3"
|
|
61
61
|
}
|
package/src/exportTable.ts
CHANGED
|
@@ -8,16 +8,29 @@ type ExportTableData<TData> = {
|
|
|
8
8
|
fileName?: string;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
type ColumnKey<TData> = keyof ColumnDefinition<TData>;
|
|
12
|
+
|
|
13
|
+
function getColumnValue<TData>(column: ColumnDefinition<TData>, key: ColumnKey<TData>): string | undefined {
|
|
14
|
+
if (key in column && column[key] && typeof column[key] !== 'object' && typeof column[key] !== 'function') {
|
|
15
|
+
return String(column[key]);
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
function getFilteredColumnsIds<TData extends object>(columnDefinitions: ColumnDefinition<TData>[]) {
|
|
20
|
+
const accessorKey = 'accessorKey' as ColumnKey<TData>;
|
|
21
|
+
|
|
22
|
+
return (columnDefinitions as ColumnDefinition<TData>[])
|
|
23
|
+
.filter(column => getColumnValue(column, accessorKey) && !column.meta?.skipOnExport)
|
|
24
|
+
.map(column => getColumnValue(column, accessorKey));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getFilteredColumnsHeaders<TData extends object>(columnDefinitions: ColumnDefinition<TData>[]) {
|
|
28
|
+
const accessorKey = 'accessorKey' as ColumnKey<TData>;
|
|
29
|
+
const header = 'header' as ColumnKey<TData>;
|
|
30
|
+
|
|
18
31
|
return (columnDefinitions as ColumnDefinition<TData>[])
|
|
19
|
-
.filter(column =>
|
|
20
|
-
.map(column =>
|
|
32
|
+
.filter(column => getColumnValue(column, accessorKey) && !column.meta?.skipOnExport)
|
|
33
|
+
.map(column => getColumnValue(column, header) || getColumnValue(column, accessorKey));
|
|
21
34
|
}
|
|
22
35
|
|
|
23
36
|
function getXlsxFormatTable<TData extends object>({
|
|
@@ -49,11 +62,12 @@ export function exportToCSV<TData extends object>({
|
|
|
49
62
|
data,
|
|
50
63
|
}: ExportTableData<TData>) {
|
|
51
64
|
const xlsxData = getXlsxFormatTable({ data, columnDefinitions });
|
|
52
|
-
const
|
|
53
|
-
const table = [
|
|
54
|
-
const csv = table.map(line => line.map(el => `"${el}"`).join(',')).join('\n');
|
|
65
|
+
const headers = getFilteredColumnsHeaders(columnDefinitions);
|
|
66
|
+
const table = [headers, ...xlsxData];
|
|
67
|
+
const csv = table.map(line => line.map(el => (el === undefined ? `""` : `"${el}"`)).join(',')).join('\n');
|
|
55
68
|
|
|
56
|
-
const
|
|
69
|
+
const utf8Prefix = new Uint8Array([0xef, 0xbb, 0xbf]);
|
|
70
|
+
const blob = new Blob([utf8Prefix, csv], { type: 'text/csv' });
|
|
57
71
|
const url = window.URL.createObjectURL(blob);
|
|
58
72
|
const tempLink = Object.assign(document.createElement('a'), {
|
|
59
73
|
target: '_blank',
|
|
@@ -72,9 +86,9 @@ export function exportToXLSX<TData extends object>({
|
|
|
72
86
|
data,
|
|
73
87
|
}: ExportTableData<TData>) {
|
|
74
88
|
const xlsxData = getXlsxFormatTable({ data, columnDefinitions });
|
|
75
|
-
const
|
|
89
|
+
const headers = getFilteredColumnsHeaders(columnDefinitions);
|
|
76
90
|
const workbook = xlsxUtils.book_new();
|
|
77
|
-
const worksheet = xlsxUtils.aoa_to_sheet([
|
|
91
|
+
const worksheet = xlsxUtils.aoa_to_sheet([headers, ...xlsxData]);
|
|
78
92
|
xlsxUtils.book_append_sheet(workbook, worksheet);
|
|
79
93
|
writeFileXLSX(workbook, `${fileName}.xlsx`);
|
|
80
94
|
|