Skip to content

Commit

Permalink
fix(useRowState): change argument for "initialRowStateAccessor" and "…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
ivaskevych authored Oct 4, 2020
1 parent bd90f10 commit 188a392
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/src/pages/docs/api/useRowState.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>`
- `initialRowStateAccessor: Function(row) => Object<any>`
- 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<any>`
- `initialCellStateAccessor: Function(cell) => Object<any>`
- **Optional**
- Defaults to: `cell => ({})`
- This function should return the initial state for a cell.
Expand Down
18 changes: 10 additions & 8 deletions src/plugin-hooks/useRowState.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
useGetLatest,
} from '../publicUtils'

const defaultInitialRowStateAccessor = originalRow => ({})
const defaultInitialCellStateAccessor = originalRow => ({})
const defaultInitialRowStateAccessor = row => ({})
const defaultInitialCellStateAccessor = cell => ({})

// Actions
actions.setRowState = 'setRowState'
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

1 comment on commit 188a392

@vercel
Copy link

@vercel vercel bot commented on 188a392 Oct 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.