Skip to content

Commit 4928d66

Browse files
committed
react-csv#153 : Changes to handle if at any point the nested keys passed do not exist then looks for key with dot notation in the object.
1 parent d9b1692 commit 4928d66

File tree

5 files changed

+91
-10
lines changed

5 files changed

+91
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ data = [
151151
{ details: { firstName: 'John', lastName: 'Jones' }, job: 'developer'},
152152
];
153153
```
154+
Note: if at any point the nested keys passed do not exist then looks for key with dot notation in the object.
154155

155156
### - **separator** Props:
156157

sample-site/src/Table.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ class Table extends React.Component {
44
state= {};
55
renderHeaders() {
66
return (<thead><tr>
7-
{this.props.headers.map((header, i) => <th key={"th"+i}>{header}</th>)}
7+
{this.props.headers.map((header, i) => <th key={"th"+i}>{typeof(header) === 'object' ? header.label : header}</th>)}
88
</tr></thead>);
99
}
1010

1111
renderRow(row, key) {
1212
return (<tr key={'tbody-tr'+key}>
13-
{row.map((cell, i) => <td key={'td-'+key+'-'+i}>{cell}</td>)}
13+
{Array.isArray(row) ?
14+
row.map((cell, i) => {
15+
return <td key={'td-'+key+'-'+i}>{typeof(cell) === 'object' ? cell.name : cell}</td>
16+
}) :
17+
Object.values(row).map((cell, i) => {
18+
return <td key={'td-'+key+'-'+i}>{typeof(cell) === 'object' ? cell.name : cell}</td>
19+
})
20+
}
1421
</tr>);
1522
}
1623

sample-site/src/app.jsx

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import React from 'react';
22
import {CSVLink, CSVDownload} from 'react-csv';
33
import Table from './Table.jsx';
44

5-
const csvHeaders = [
5+
const csvHeaders1 = [
66
"Company","جهة الإتصال ","王玉普"
77
]
8-
const csvData =[
8+
const csvData1 = [
99
['Alfreds Futterkiste' ,'Maria Anders', 'Germany'] ,
1010
['Rathath IT', 'Abdennour TM' , 'تونس'] ,
1111
['Sinopec', '王玉普' , '中国'],
@@ -17,9 +17,30 @@ const csvData =[
1717
['Software hourse', 'Soro' , 'Poland']
1818
];
1919

20+
const csvHeaders2 = [
21+
{ label: 'First. Name', key: 'first.name' },
22+
{ label: 'Last Name.', key: 'lastname' },
23+
{ label: 'E.m.a.i.l.', key: 'email' }
24+
];
25+
26+
const csvData2 = [
27+
{ first:{name: 'Ahm.ed'}, lastname: 'Tomi', email: '[email protected]' },
28+
{ first:{name: 'Raed'}, lastname: 'Lab.es', email: '[email protected]' },
29+
{ first:{name: 'Yezzi'}, lastname: 'Min l3b', email: '[email protected]' }
30+
];
31+
32+
const csvData3 = [
33+
{ 'first.name': 'Ahm.ed', lastname: 'Tomi', email: '[email protected]' },
34+
{ 'first.name': 'Raed', lastname: 'Lab.es', email: '[email protected]' },
35+
{ 'first.name': 'Yezzi', lastname: 'Min l3b', email: '[email protected]' }
36+
];
37+
2038
class App extends React.Component {
2139

22-
state= {};
40+
state= {
41+
csvHeaders: csvHeaders1,
42+
csvData: csvData1
43+
};
2344
getFileName() {
2445
if (!this.state.filename) return undefined;
2546
if (!this.state.filename.endsWith('.csv')) return this.state.filename + '.csv';
@@ -29,8 +50,28 @@ class App extends React.Component {
2950
return (
3051
<div>
3152
<div ><h1>Pretty Example "React-csv"</h1></div>
53+
<div className="row">
54+
<div className="large-3 columns">
55+
<a className="btn"
56+
onClick={() => this.setState({csvHeaders: csvHeaders1, csvData: csvData1})}>
57+
Sample Array Data
58+
</a>
59+
</div>
60+
<div className="large-3 columns">
61+
<a className="btn"
62+
onClick={() => this.setState({csvHeaders: csvHeaders2, csvData: csvData2})}>Sample Object Data1</a>
63+
</div>
64+
<div className="large-3 columns">
65+
<a className="btn"
66+
onClick={() => this.setState({csvHeaders: csvHeaders2, csvData: csvData3})}>Sample Object Data2</a>
67+
</div>
68+
<div className="large-3 columns">
69+
<a className="btn"
70+
onClick={() => this.setState({csvHeaders: csvHeaders1, csvData: csvData2})}>Sample Object Data3</a>
71+
</div>
72+
</div>
3273
<div>
33-
<Table headers={csvHeaders} data={csvData} />
74+
<Table headers={this.state.csvHeaders} data={this.state.csvData} />
3475
</div>
3576
<div className="row">
3677
<div className="large-6 columns"></div>
@@ -41,15 +82,21 @@ class App extends React.Component {
4182
</div>
4283
<div className="large-2 columns">
4384
<CSVLink
44-
headers={csvHeaders}
45-
data={csvData}
85+
headers={this.state.csvHeaders}
86+
data={this.state.csvData}
4687
filename={this.getFileName()}
4788
className="btn"
4889
>
4990
Export to CSV ⬇
5091
</CSVLink>
5192
</div>
5293
</div>
94+
<div>
95+
<b>Header :</b>
96+
<div><pre>{JSON.stringify(this.state.csvHeaders, null, 2) }</pre></div>
97+
<b>Data : </b>
98+
<div><pre>{JSON.stringify(this.state.csvData, null, 2) }</pre></div>
99+
</div>
53100

54101
</div>
55102
);

src/core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export const getHeaderValue = (property, obj) => {
4343
return o[p];
4444
}
4545
}, obj);
46-
47-
return (foundValue === undefined) ? '' : foundValue;
46+
// if at any point the nested keys passed do not exist then looks for key `property` in object obj
47+
return (foundValue === undefined) ? ((property in obj) ? obj[property] : '') : foundValue;
4848
}
4949

5050
export const elementOrEmpty = (element) => element || element === 0 ? element : '';

test/coreSpec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,32 @@ describe('In browser environment', () => {
165165
];
166166
expect(actual).toEqual(expected);
167167
});
168+
it(`accepts dot notation in headers and map to key when nested keys are not found`, () => {
169+
fixtures = [{
170+
maths: '90'
171+
}, {
172+
sport: '97'
173+
}, {
174+
maths: '77',
175+
sport: 0
176+
}, {
177+
people: {
178+
name: 'john'
179+
}
180+
}, {
181+
'people.age': '20'
182+
}]
183+
const headers = ['maths', 'sport', 'phy', 'ch', 'people.name', 'people.age'];
184+
const actual = jsons2arrays(fixtures, headers);
185+
const expected = [
186+
headers, ['90', '', '', '', '', ''],
187+
['', '97', '', '', '', ''],
188+
['77', 0, '', '', '', ''],
189+
['', '', '', '', 'john', ''],
190+
['', '', '', '', '', '20']
191+
];
192+
expect(actual).toEqual(expected);
193+
});
168194
});
169195

170196
describe(`core::arrays2csv`, () => {

0 commit comments

Comments
 (0)