From 4dbffa10d72d0b071e518aee5eb9afec0a38c7eb Mon Sep 17 00:00:00 2001 From: Gabriel Liwerant Date: Thu, 12 Sep 2019 04:02:09 -0400 Subject: [PATCH] Better attempt takes null and array into account, which are allowed --- src/MUIDataTable.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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 = () => {}) {