@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.
@@ -1,13 +1,21 @@
1
1
  import { utils as xlsxUtils, writeFileXLSX } from 'xlsx';
2
- function getColumnAccessorKey(column) {
3
- if ('accessorKey' in column && column.accessorKey) {
4
- return String(column.accessorKey);
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 getColumnAccessorKey(column) && !((_a = column.meta) === null || _a === void 0 ? void 0 : _a.skipOnExport); })
10
- .map(column => getColumnAccessorKey(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 filteredIds = getFilteredColumnsIds(columnDefinitions);
30
- const table = [filteredIds, ...xlsxData];
31
- const csv = table.map(line => line.map(el => `"${el}"`).join(',')).join('\n');
32
- const blob = new Blob([csv], { type: 'text/csv' });
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 filteredIds = getFilteredColumnsIds(columnDefinitions);
54
+ const headers = getFilteredColumnsHeaders(columnDefinitions);
46
55
  const workbook = xlsxUtils.book_new();
47
- const worksheet = xlsxUtils.aoa_to_sheet([filteredIds, ...xlsxData]);
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": "58701999be0e38e97b7182901945b3657231e58c"
60
+ "gitHead": "d42f6d50b6ef811271257f767a199907f8084bb3"
61
61
  }
@@ -8,16 +8,29 @@ type ExportTableData<TData> = {
8
8
  fileName?: string;
9
9
  };
10
10
 
11
- function getColumnAccessorKey<TData>(column: ColumnDefinition<TData>): string | undefined {
12
- if ('accessorKey' in column && column.accessorKey) {
13
- return String(column.accessorKey);
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 => getColumnAccessorKey(column) && !column.meta?.skipOnExport)
20
- .map(column => getColumnAccessorKey(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 filteredIds = getFilteredColumnsIds(columnDefinitions);
53
- const table = [filteredIds, ...xlsxData];
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 blob = new Blob([csv], { type: 'text/csv' });
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 filteredIds = getFilteredColumnsIds(columnDefinitions);
89
+ const headers = getFilteredColumnsHeaders(columnDefinitions);
76
90
  const workbook = xlsxUtils.book_new();
77
- const worksheet = xlsxUtils.aoa_to_sheet([filteredIds, ...xlsxData]);
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