From 42b41d1260fab3c6635b071ca984dce253bf23ab Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Mon, 9 Mar 2020 10:43:59 -0600 Subject: [PATCH] Fix #1944 Fix #1944 --- src/hooks/useTable.js | 88 ++++++++++--------------------- src/plugin-hooks/usePagination.js | 1 - src/utils.js | 8 --- 3 files changed, 29 insertions(+), 68 deletions(-) diff --git a/src/hooks/useTable.js b/src/hooks/useTable.js index 18f2924..23f8780 100755 --- a/src/hooks/useTable.js +++ b/src/hooks/useTable.js @@ -9,7 +9,6 @@ import { unpreparedAccessWarning, makeHeaderGroups, decorateColumn, - dedupeBy, } from '../utils' import { @@ -184,7 +183,7 @@ export const useTable = (props, ...plugins) => { getInstance().allColumns = allColumns // Access the row model using initial columns - const coreDataModel = React.useMemo(() => { + const [rows, flatRows, rowsById] = React.useMemo(() => { let rows = [] let flatRows = [] const rowsById = {} @@ -206,69 +205,18 @@ export const useTable = (props, ...plugins) => { }) } - return { rows, flatRows, rowsById } + return [rows, flatRows, rowsById] }, [allColumns, data, getRowId, getSubRows, getHooks, getInstance]) - // Allow materialized columns to also access data - const [rows, flatRows, rowsById, materializedColumns] = React.useMemo(() => { - const { rows, flatRows, rowsById } = coreDataModel - const materializedColumns = reduceHooks( - getHooks().materializedColumns, - [], - { - instance: getInstance(), - } - ) - - materializedColumns.forEach(d => assignColumnAccessor(d)) - - const materializedColumnsQueue = [...materializedColumns] - - while (materializedColumnsQueue.length) { - const column = materializedColumnsQueue.shift() - accessRowsForColumn({ - data, - rows, - flatRows, - rowsById, - column, - getRowId, - getSubRows, - accessValueHooks: getHooks().accessValue, - getInstance, - }) - } - - return [rows, flatRows, rowsById, materializedColumns] - }, [ - coreDataModel, - getHooks, - getInstance, - data, - getRowId, - getSubRows, - // eslint-disable-next-line react-hooks/exhaustive-deps - ...reduceHooks(getHooks().materializedColumnsDeps, [], { - instance: getInstance(), - }), - ]) - Object.assign(getInstance(), { rows, flatRows, rowsById, - materializedColumns, + // materializedColumns, }) loopHooks(getHooks().useInstanceAfterData, getInstance()) - // Combine new materialized columns with all columns (dedupe prefers later columns) - allColumns = React.useMemo( - () => dedupeBy([...allColumns, ...materializedColumns], d => d.id), - [allColumns, materializedColumns] - ) - getInstance().allColumns = allColumns - // Get the flat list of all columns AFTER the rows // have been access, and allow hooks to decorate // those columns (and trigger this memoization via deps) @@ -290,12 +238,34 @@ export const useTable = (props, ...plugins) => { ) // Combine new visible columns with all columns (dedupe prefers later columns) - allColumns = React.useMemo( - () => dedupeBy([...allColumns, ...visibleColumns], d => d.id), - [allColumns, visibleColumns] - ) + allColumns = React.useMemo(() => { + const columns = [...allColumns] + + visibleColumns.forEach(column => { + if (!columns.find(d => d.id === column.id)) { + columns.push(column) + } + }) + + return columns + }, [allColumns, visibleColumns]) getInstance().allColumns = allColumns + if (process.env.NODE_ENV !== 'production') { + const duplicateColumns = allColumns.filter((column, i) => { + return allColumns.findIndex(d => d.id === column.id) !== i + }) + + if (duplicateColumns.length) { + console.info(allColumns) + throw new Error( + `Duplicate columns were found with ids: "${duplicateColumns + .map(d => d.id) + .join(', ')}" in the columns array above` + ) + } + } + // Make the headerGroups const headerGroups = React.useMemo( () => diff --git a/src/plugin-hooks/usePagination.js b/src/plugin-hooks/usePagination.js index bca2cbd..8a9e4bb 100755 --- a/src/plugin-hooks/usePagination.js +++ b/src/plugin-hooks/usePagination.js @@ -101,7 +101,6 @@ function useInstance(instance) { useMountedLayoutEffect(() => { if (getAutoResetPage()) { - console.log('reset') dispatch({ type: actions.resetPage }) } }, [ diff --git a/src/utils.js b/src/utils.js index 26958be..d5975b7 100755 --- a/src/utils.js +++ b/src/utils.js @@ -65,14 +65,6 @@ export function assignColumnAccessor(column) { return column } -// Find the depth of the columns -export function dedupeBy(arr, fn) { - return [...arr] - .reverse() - .filter((d, i, all) => all.findIndex(dd => fn(dd) === fn(d)) === i) - .reverse() -} - export function decorateColumn(column, userDefaultColumn) { if (!userDefaultColumn) { throw new Error()