Skip to content

Commit

Permalink
Better attempt takes null and array into account, which are allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielliwerant committed Sep 12, 2019
1 parent e63cf6d commit 4dbffa1
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/MUIDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,22 +441,24 @@ class MUIDataTable extends React.Component {
};

transformData = (columns, data) => {
const leaf = (obj, path) => path.split('.').reduce((value, el) => {
if (typeof value[el] === 'object') throw Error(`Cannot accept objects for cell data from field "${el}". Cells need to contain strings | numbers. It\'s possible this error is the result of a missing dot in the column name field (e.g. name: "company" instead of name: "company.id")`);
return value ? value[el] : undefined;
}, obj);
const leaf = (obj, path) => path.split('.').reduce((value, el) => (value ? value[el] : undefined), obj);

return Array.isArray(data[0])
const transformedData = Array.isArray(data[0])
? data.map(row => {
let i = -1;

return columns.map(col => {
if (!col.empty) i++;
if (typeof row[i] === 'object') throw Error(`Cannot accept objects for cell data. Cells need to contain strings | numbers. It\'s possible this error is the result of a missing dot in the column name field (e.g. name: "company" instead of name: "company.id")`);
return col.empty ? undefined : row[i];
});
})
: data.map(row => columns.map(col => leaf(row, col.name)));

// We need to determine if object data exists in the transformed structure, as this is currently not allowed
const hasInvalidData = transformedData.filter(data => data.filter(d => typeof d === 'object' && d !== null && !Array.isArray(d)).length > 0).length > 0;
if (hasInvalidData) throw Error(`Cannot accept objects for cell data. Cells need to contain strings | numbers. It\'s possible this error is the result of a missing dot in the column name field (e.g. name: "company" instead of name: "company.id")`);

return transformedData;
};

setTableData(props, status, callback = () => {}) {
Expand Down

0 comments on commit 4dbffa1

Please sign in to comment.