diff --git a/src/MUIDataTable.js b/src/MUIDataTable.js index e9330aaf2..e0c248732 100644 --- a/src/MUIDataTable.js +++ b/src/MUIDataTable.js @@ -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 = () => {}) {