dce-reactkit 4.1.2 → 4.1.4
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/cjs/index.js +13 -6
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/components/CSVDownloadButton.d.ts +1 -0
- package/dist/cjs/types/helpers/genCSV.d.ts +5 -1
- package/dist/esm/index.js +13 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/components/CSVDownloadButton.d.ts +1 -0
- package/dist/esm/types/helpers/genCSV.d.ts +5 -1
- package/dist/index.d.ts +6 -1
- package/package.json +1 -1
- package/src/components/CSVDownloadButton.tsx +4 -1
- package/src/helpers/genCSV.test.ts +18 -4
- package/src/helpers/genCSV.ts +12 -5
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
* Generate a CSV file
|
|
3
3
|
* @author Gabe Abrams
|
|
4
4
|
* @param data list of row data in the form of json objects
|
|
5
|
-
* @param columns list of columns to include in the csv
|
|
5
|
+
* @param columns list of columns to include in the csv where each column is an object with:
|
|
6
|
+
* - title: the column title
|
|
7
|
+
* - param: the key in the data object to use for this column
|
|
8
|
+
* - textIfUndefined: optional text to use if the value is undefined (defaults to empty string)
|
|
6
9
|
* @returns multiline csv string
|
|
7
10
|
*/
|
|
8
11
|
declare const genCSV: (data: {
|
|
@@ -10,5 +13,6 @@ declare const genCSV: (data: {
|
|
|
10
13
|
}[], columns: {
|
|
11
14
|
title: string;
|
|
12
15
|
param: string;
|
|
16
|
+
textIfUndefined?: string;
|
|
13
17
|
}[]) => string;
|
|
14
18
|
export default genCSV;
|
package/dist/index.d.ts
CHANGED
|
@@ -543,6 +543,7 @@ declare const IntelliTable: React$1.FC<Props$7>;
|
|
|
543
543
|
type Props$6 = {
|
|
544
544
|
filename: string;
|
|
545
545
|
csv: string;
|
|
546
|
+
label?: string;
|
|
546
547
|
id?: string;
|
|
547
548
|
className?: string;
|
|
548
549
|
ariaLabel?: string;
|
|
@@ -1269,7 +1270,10 @@ declare const getMonthName: (month: number) => {
|
|
|
1269
1270
|
* Generate a CSV file
|
|
1270
1271
|
* @author Gabe Abrams
|
|
1271
1272
|
* @param data list of row data in the form of json objects
|
|
1272
|
-
* @param columns list of columns to include in the csv
|
|
1273
|
+
* @param columns list of columns to include in the csv where each column is an object with:
|
|
1274
|
+
* - title: the column title
|
|
1275
|
+
* - param: the key in the data object to use for this column
|
|
1276
|
+
* - textIfUndefined: optional text to use if the value is undefined (defaults to empty string)
|
|
1273
1277
|
* @returns multiline csv string
|
|
1274
1278
|
*/
|
|
1275
1279
|
declare const genCSV: (data: {
|
|
@@ -1277,6 +1281,7 @@ declare const genCSV: (data: {
|
|
|
1277
1281
|
}[], columns: {
|
|
1278
1282
|
title: string;
|
|
1279
1283
|
param: string;
|
|
1284
|
+
textIfUndefined?: string;
|
|
1280
1285
|
}[]) => string;
|
|
1281
1286
|
|
|
1282
1287
|
/**
|
package/package.json
CHANGED
|
@@ -20,6 +20,8 @@ type Props = {
|
|
|
20
20
|
filename: string,
|
|
21
21
|
// The contents of the CSV file to download
|
|
22
22
|
csv: string,
|
|
23
|
+
// Label for the button
|
|
24
|
+
label?: string,
|
|
23
25
|
// Id of the button
|
|
24
26
|
id?: string,
|
|
25
27
|
// Class name of the button
|
|
@@ -55,6 +57,7 @@ const CSVDownloadButton: React.FC<Props> = (props) => {
|
|
|
55
57
|
style,
|
|
56
58
|
onClick,
|
|
57
59
|
children,
|
|
60
|
+
label = 'Download CSV',
|
|
58
61
|
} = props;
|
|
59
62
|
|
|
60
63
|
/*------------------------------------------------------------------------*/
|
|
@@ -86,7 +89,7 @@ const CSVDownloadButton: React.FC<Props> = (props) => {
|
|
|
86
89
|
icon={faCloudDownloadAlt}
|
|
87
90
|
/>
|
|
88
91
|
{' '}
|
|
89
|
-
|
|
92
|
+
{label}
|
|
90
93
|
</>
|
|
91
94
|
)}
|
|
92
95
|
{children}
|
|
@@ -23,7 +23,7 @@ describe('genCSV', () => {
|
|
|
23
23
|
{ name: 'Bob', age: 30, email: 'bob@example.com' },
|
|
24
24
|
{ name: 'Charlie', age: 35, email: 'charlie@example.com' },
|
|
25
25
|
];
|
|
26
|
-
const expected =
|
|
26
|
+
const expected = 'Name,Age,Email\nAlice,25,alice@example.com\nBob,30,bob@example.com\nCharlie,35,charlie@example.com';
|
|
27
27
|
const result = genCSV(data, columns);
|
|
28
28
|
expect(result).toEqual(expected);
|
|
29
29
|
});
|
|
@@ -39,7 +39,7 @@ describe('genCSV', () => {
|
|
|
39
39
|
{ name: 'Bob', age: null, email: 'bob@example.com' },
|
|
40
40
|
{ name: 'Charlie', age: 35, email: undefined },
|
|
41
41
|
];
|
|
42
|
-
const expected =
|
|
42
|
+
const expected = 'Name,Age,Email\nAlice,,\nBob,,bob@example.com\nCharlie,35,';
|
|
43
43
|
const result = genCSV(data, columns);
|
|
44
44
|
expect(result).toEqual(expected);
|
|
45
45
|
});
|
|
@@ -54,8 +54,22 @@ describe('genCSV', () => {
|
|
|
54
54
|
{ name: 'Bob', data: [1, 2, 3] },
|
|
55
55
|
{ name: 'Charlie', data: null },
|
|
56
56
|
];
|
|
57
|
-
const expected =
|
|
57
|
+
const expected = 'Name,Data\nAlice,"{""foo"":""bar""}"\nBob,"[1,2,3]"\nCharlie,';
|
|
58
58
|
const result = genCSV(data, columns);
|
|
59
59
|
expect(result).toEqual(expected);
|
|
60
60
|
});
|
|
61
|
-
|
|
61
|
+
|
|
62
|
+
it('should use textIfUndefined for undefined values', () => {
|
|
63
|
+
const columns = [
|
|
64
|
+
{ title: 'Name', param: 'name' },
|
|
65
|
+
{ title: 'Age', param: 'age', textIfUndefined: 'N/A' },
|
|
66
|
+
];
|
|
67
|
+
const data = [
|
|
68
|
+
{ name: 'Alice', age: undefined },
|
|
69
|
+
{ name: 'Bob', age: 30 },
|
|
70
|
+
];
|
|
71
|
+
const expected = 'Name,Age\nAlice,N/A\nBob,30';
|
|
72
|
+
const result = genCSV(data, columns);
|
|
73
|
+
expect(result).toEqual(expected);
|
|
74
|
+
});
|
|
75
|
+
});
|
package/src/helpers/genCSV.ts
CHANGED
|
@@ -21,7 +21,10 @@ const escapeCellText = (text: string): string => {
|
|
|
21
21
|
* Generate a CSV file
|
|
22
22
|
* @author Gabe Abrams
|
|
23
23
|
* @param data list of row data in the form of json objects
|
|
24
|
-
* @param columns list of columns to include in the csv
|
|
24
|
+
* @param columns list of columns to include in the csv where each column is an object with:
|
|
25
|
+
* - title: the column title
|
|
26
|
+
* - param: the key in the data object to use for this column
|
|
27
|
+
* - textIfUndefined: optional text to use if the value is undefined (defaults to empty string)
|
|
25
28
|
* @returns multiline csv string
|
|
26
29
|
*/
|
|
27
30
|
const genCSV = (
|
|
@@ -31,6 +34,7 @@ const genCSV = (
|
|
|
31
34
|
columns: {
|
|
32
35
|
title: string,
|
|
33
36
|
param: string,
|
|
37
|
+
textIfUndefined?: string,
|
|
34
38
|
}[],
|
|
35
39
|
): string => {
|
|
36
40
|
let csv = '';
|
|
@@ -55,13 +59,16 @@ const genCSV = (
|
|
|
55
59
|
if (
|
|
56
60
|
typeof cell === 'string'
|
|
57
61
|
|| typeof cell === 'number'
|
|
62
|
+
|| typeof cell === 'boolean'
|
|
58
63
|
) {
|
|
59
64
|
contents = String(cell);
|
|
60
|
-
} else if (
|
|
61
|
-
typeof cell === 'undefined'
|
|
62
|
-
|| cell === null
|
|
63
|
-
) {
|
|
65
|
+
} else if (cell === null) {
|
|
64
66
|
contents = '';
|
|
67
|
+
} else if (typeof cell === 'undefined') {
|
|
68
|
+
contents = (
|
|
69
|
+
column.textIfUndefined
|
|
70
|
+
?? ''
|
|
71
|
+
);
|
|
65
72
|
} else if (typeof cell === 'object') {
|
|
66
73
|
contents = JSON.stringify(cell);
|
|
67
74
|
} else {
|