mirror of
https://github.com/gosticks/react-table.git
synced 2025-10-16 11:55:36 +00:00
parent
3aa016e056
commit
42b41d1260
@ -9,7 +9,6 @@ import {
|
|||||||
unpreparedAccessWarning,
|
unpreparedAccessWarning,
|
||||||
makeHeaderGroups,
|
makeHeaderGroups,
|
||||||
decorateColumn,
|
decorateColumn,
|
||||||
dedupeBy,
|
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -184,7 +183,7 @@ export const useTable = (props, ...plugins) => {
|
|||||||
getInstance().allColumns = allColumns
|
getInstance().allColumns = allColumns
|
||||||
|
|
||||||
// Access the row model using initial columns
|
// Access the row model using initial columns
|
||||||
const coreDataModel = React.useMemo(() => {
|
const [rows, flatRows, rowsById] = React.useMemo(() => {
|
||||||
let rows = []
|
let rows = []
|
||||||
let flatRows = []
|
let flatRows = []
|
||||||
const rowsById = {}
|
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])
|
}, [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(), {
|
Object.assign(getInstance(), {
|
||||||
rows,
|
rows,
|
||||||
flatRows,
|
flatRows,
|
||||||
rowsById,
|
rowsById,
|
||||||
materializedColumns,
|
// materializedColumns,
|
||||||
})
|
})
|
||||||
|
|
||||||
loopHooks(getHooks().useInstanceAfterData, getInstance())
|
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
|
// Get the flat list of all columns AFTER the rows
|
||||||
// have been access, and allow hooks to decorate
|
// have been access, and allow hooks to decorate
|
||||||
// those columns (and trigger this memoization via deps)
|
// 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)
|
// Combine new visible columns with all columns (dedupe prefers later columns)
|
||||||
allColumns = React.useMemo(
|
allColumns = React.useMemo(() => {
|
||||||
() => dedupeBy([...allColumns, ...visibleColumns], d => d.id),
|
const columns = [...allColumns]
|
||||||
[allColumns, visibleColumns]
|
|
||||||
)
|
visibleColumns.forEach(column => {
|
||||||
|
if (!columns.find(d => d.id === column.id)) {
|
||||||
|
columns.push(column)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return columns
|
||||||
|
}, [allColumns, visibleColumns])
|
||||||
getInstance().allColumns = allColumns
|
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
|
// Make the headerGroups
|
||||||
const headerGroups = React.useMemo(
|
const headerGroups = React.useMemo(
|
||||||
() =>
|
() =>
|
||||||
|
|||||||
@ -101,7 +101,6 @@ function useInstance(instance) {
|
|||||||
|
|
||||||
useMountedLayoutEffect(() => {
|
useMountedLayoutEffect(() => {
|
||||||
if (getAutoResetPage()) {
|
if (getAutoResetPage()) {
|
||||||
console.log('reset')
|
|
||||||
dispatch({ type: actions.resetPage })
|
dispatch({ type: actions.resetPage })
|
||||||
}
|
}
|
||||||
}, [
|
}, [
|
||||||
|
|||||||
@ -65,14 +65,6 @@ export function assignColumnAccessor(column) {
|
|||||||
return 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) {
|
export function decorateColumn(column, userDefaultColumn) {
|
||||||
if (!userDefaultColumn) {
|
if (!userDefaultColumn) {
|
||||||
throw new Error()
|
throw new Error()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user