From 188a392909bd9e5aa55e30daa235b4053c507c3b Mon Sep 17 00:00:00 2001 From: Roman Ivaskevych Date: Sun, 4 Oct 2020 22:22:13 +0300 Subject: [PATCH] fix(useRowState): change argument for "initialRowStateAccessor" and "initialCellStateAccessor" (#2719) * fix(usepagination resetpage on globalfilter change): pageIndex reset autoReset pageIndex on globalFilter change "fix #1812" * fix(useRowState): change argument for `initialRowStateAccessor` and `initialCellStateAccessor` pass `row` / `cell` to `initialRowStateAccessor` / `initialCellStateAccessor` accordingly closes(#2102, #2701) * fix(useRowState): change argument for "initialRowStateAccessor" and "initialCellStateAccessor" pass "row" / "cell" to "initialRowStateAccessor" / "initialCellStateAccessor" accordingly closes(#2102, #2701) --- docs/src/pages/docs/api/useRowState.md | 4 ++-- src/plugin-hooks/useRowState.js | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/docs/src/pages/docs/api/useRowState.md b/docs/src/pages/docs/api/useRowState.md index 31045341dc..f10bf14496 100644 --- a/docs/src/pages/docs/api/useRowState.md +++ b/docs/src/pages/docs/api/useRowState.md @@ -15,12 +15,12 @@ The following options are supported via the main options object passed to `useTa - If a row's ID is found in this array, it will have the state of the value corresponding to that key. - Individual row states can contain anything, but they also contain a `cellState` key, which provides cell-level state based on column ID's to every **prepared** cell in the table. -- `initialRowStateAccessor: Function(originalRow) => Object` +- `initialRowStateAccessor: Function(row) => Object` - Optional - Defaults to: `row => ({})` - This function should return the initial state for a row. - If this function is defined, it will be passed a `Row` object, from which you can return a value to use as the initial state, eg. `row => row.original.initialState` -- `initialCellStateAccessor: Function(originalRow) => Object` +- `initialCellStateAccessor: Function(cell) => Object` - **Optional** - Defaults to: `cell => ({})` - This function should return the initial state for a cell. diff --git a/src/plugin-hooks/useRowState.js b/src/plugin-hooks/useRowState.js index 484f24e4b3..6616835670 100644 --- a/src/plugin-hooks/useRowState.js +++ b/src/plugin-hooks/useRowState.js @@ -7,8 +7,8 @@ import { useGetLatest, } from '../publicUtils' -const defaultInitialRowStateAccessor = originalRow => ({}) -const defaultInitialCellStateAccessor = originalRow => ({}) +const defaultInitialRowStateAccessor = row => ({}) +const defaultInitialCellStateAccessor = cell => ({}) // Actions actions.setRowState = 'setRowState' @@ -50,7 +50,7 @@ function reducer(state, action, previousState, instance) { const oldRowState = typeof state.rowState[rowId] !== 'undefined' ? state.rowState[rowId] - : initialRowStateAccessor(rowsById[rowId].original) + : initialRowStateAccessor(rowsById[rowId]) return { ...state, @@ -67,12 +67,14 @@ function reducer(state, action, previousState, instance) { const oldRowState = typeof state.rowState[rowId] !== 'undefined' ? state.rowState[rowId] - : initialRowStateAccessor(rowsById[rowId].original) + : initialRowStateAccessor(rowsById[rowId]) const oldCellState = typeof oldRowState?.cellState?.[columnId] !== 'undefined' ? oldRowState.cellState[columnId] - : initialCellStateAccessor(rowsById[rowId].original) + : initialCellStateAccessor( + rowsById[rowId]?.cells?.find(cell => cell.column.id === columnId) + ) return { ...state, @@ -135,11 +137,11 @@ function prepareRow(row, { instance }) { state: { rowState }, } = instance - if (row.original) { + if (row) { row.state = typeof rowState[row.id] !== 'undefined' ? rowState[row.id] - : initialRowStateAccessor(row.original) + : initialRowStateAccessor(row) row.setState = updater => { return instance.setRowState(row.id, updater) @@ -153,7 +155,7 @@ function prepareRow(row, { instance }) { cell.state = typeof row.state.cellState[cell.column.id] !== 'undefined' ? row.state.cellState[cell.column.id] - : initialCellStateAccessor(row.original) + : initialCellStateAccessor(cell) cell.setState = updater => { return instance.setCellState(row.id, cell.column.id, updater)