diff --git a/.size-snapshot.json b/.size-snapshot.json index f6ba185..db5190a 100644 --- a/.size-snapshot.json +++ b/.size-snapshot.json @@ -19,21 +19,21 @@ } }, "dist\\index.js": { - "bundled": 105386, - "minified": 49565, - "gzipped": 13008 + "bundled": 104977, + "minified": 48560, + "gzipped": 13057 }, "dist\\index.es.js": { - "bundled": 104543, - "minified": 48815, - "gzipped": 12856, + "bundled": 104090, + "minified": 47770, + "gzipped": 12900, "treeshaked": { "rollup": { "code": 80, "import_statements": 21 }, "webpack": { - "code": 8444 + "code": 8227 } } } diff --git a/src/plugin-hooks/tests/useGroupBy.test.js b/src/plugin-hooks/tests/useGroupBy.test.js index 1a88e62..075250f 100644 --- a/src/plugin-hooks/tests/useGroupBy.test.js +++ b/src/plugin-hooks/tests/useGroupBy.test.js @@ -64,6 +64,9 @@ function Table({ columns, data }) { columns, data, defaultColumn, + initialState: { + groupBy: ["Column Doesn't Exist"], + }, }, useGroupBy, useExpanded diff --git a/src/plugin-hooks/useGroupBy.js b/src/plugin-hooks/useGroupBy.js index 2312423..7d463c0 100755 --- a/src/plugin-hooks/useGroupBy.js +++ b/src/plugin-hooks/useGroupBy.js @@ -85,7 +85,9 @@ function flatColumns(flatColumns, { state: { groupBy } }) { // Sort grouped columns to the start of the column list // before the headers are built - const groupByColumns = groupBy.map(g => flatColumns.find(col => col.id === g)) + const groupByColumns = groupBy + .map(g => flatColumns.find(col => col.id === g)) + .filter(col => !!col) const nonGroupByColumns = flatColumns.filter(col => !groupBy.includes(col.id)) flatColumns = [...groupByColumns, ...nonGroupByColumns] @@ -180,6 +182,11 @@ function useInstance(instance) { return [rows, flatRows] } + // Ensure that the list of filtered columns exist + const existingGroupBy = groupBy.filter(g => + flatColumns.find(col => col.id === g) + ) + // Find the columns that can or are aggregating // Uses each column to aggregate rows into a single value const aggregateRowsToValues = (rows, isAggregated) => { @@ -187,7 +194,7 @@ function useInstance(instance) { flatColumns.forEach(column => { // Don't aggregate columns that are in the groupBy - if (groupBy.includes(column.id)) { + if (existingGroupBy.includes(column.id)) { values[column.id] = rows[0] ? rows[0].values[column.id] : null return } @@ -234,11 +241,11 @@ function useInstance(instance) { // Recursively group the data const groupRecursively = (rows, depth = 0, parentId) => { // This is the last level, just return the rows - if (depth === groupBy.length) { + if (depth === existingGroupBy.length) { return rows } - const columnId = groupBy[depth] + const columnId = existingGroupBy[depth] // Group the rows together for this level let groupedRows = groupByFn(rows, columnId) @@ -251,7 +258,10 @@ function useInstance(instance) { subRows = groupRecursively(subRows, depth + 1, id) - const values = aggregateRowsToValues(subRows, depth < groupBy.length) + const values = aggregateRowsToValues( + subRows, + depth < existingGroupBy.length + ) const row = { id,