Skip to content
Source
Sign inSign up
TanStack/table

toggleAllRowsSelected fires onRowSelectionChange on a no-op, unlike toggleAllRowsExpanded after #6501 #6538

Send to my agent
ClosedMILLERMARRU(GitHub profile, opens in a new tab) opened this issue 0 comments

#6501 (closing #6136) added a no-op guard to table_toggleAllRowsExpanded so it stops calling onExpandedChange when the requested state already matches the current one. table_toggleAllRowsSelected has the identical shape of API (imperative toggle-all with an optional explicit boolean) and never got the same guard.

rowExpandingFeature.utils.ts, post-#6501:

export function table_toggleAllRowsExpanded(table, expanded?) {
  const currentExpanded = table.atoms.expanded?.get() ?? {}
  if (expanded ?? !table_getIsAllRowsExpanded(table)) {
    if (currentExpanded === true) return
    if (!table_getCanSomeRowsExpand(table)) return
    table_setExpanded(table, true)
  } else {
    if (currentExpanded !== true && !Object.keys(currentExpanded).length) return
    table_setExpanded(table, makeObjectMap())
  }
}

rowSelectionFeature.utils.ts, current main:

export function table_toggleAllRowsSelected(table, value?, opts?) {
  table._lastSelectedRowId = null
  table_setRowSelection(table, (old) => {
    value = typeof value !== 'undefined' ? value : !callMemoOrStaticFn(table, 'getIsAllRowsSelected', table_getIsAllRowsSelected)
    if (opts?.deselectAll && !value) return makeObjectMap<true>()
    const rowSelection = Object.assign(makeObjectMap<true>(), old)
    // ...builds the full selection map unconditionally...
  })
}

No early return for the case where the resulting selection already matches old. table_setRowSelection itself is intentionally unguarded too (there's a comment explaining why: a full deep-equal over an arbitrary-sized selection map would be real overhead for genuine changes), but that rationale doesn't apply to toggleAllRowsSelected specifically, the O(1)-ish checks table_toggleAllRowsExpanded added (getIsAllRowsSelected() is already memoized, an empty-map check) are the same shape of cheap guard, not a full map comparison.

Repro

table.toggleAllRowsSelected(true) // selects everything, fires onRowSelectionChange
table.toggleAllRowsSelected(true) // everything is already selected...
// onRowSelectionChange still fires, with a functionally identical resulting map

Same symptom #6136 described for expansion: a consumer syncing rowSelection to external state (URL, server) sees redundant writes on every no-op toggle, and any useEffect/watcher keyed on onRowSelectionChange re-runs for nothing.

Might be intentional given the differing rationale for the general setter, flagging with that caveat rather than assuming it's simply missed, but the parity with the just-fixed expansion API made it worth raising explicitly.

Sign in to join this conversation.