Sha256: 20f64d487a166bd6e27eb94c2b716d9c1e564a2601eca4a77e29ea9b011d6926

Contents?: true

Size: 1.72 KB

Versions: 30

Compression:

Stored size: 1.72 KB

Contents

import { RowModel } from "@tanstack/react-table"
import { DataType, ExpandedStateObject } from "./types"

const filterExpandableRows = (expandedState: Record<string, boolean>) => {
  for (const expandedRow in expandedState) {
    if (expandedState[expandedRow] === false) {
      delete expandedState[expandedRow]
    }
  }
  return expandedState
}

export const updateExpandAndCollapseState = (
  tableRows: RowModel<DataType>,
  expanded: Record<string, boolean>,
  targetParent: string
) => {
  const updateExpandedRows: Record<string, boolean> = {}
  const rows = tableRows.flatRows
  // Variable checks if all rows in a section have same expansion state or not
  let isExpansionConsistent = true
  const areRowsExpanded = new Set<boolean>()

  // Update isExpansionConsistent variable
  for (const row of rows) {
    if (
      row.getCanExpand() &&
      (targetParent === undefined
        ? row.depth === 0
        : targetParent === row.parentId)
    ) {
      areRowsExpanded.add(row.getIsExpanded())
      if (areRowsExpanded.size > 1) {
        isExpansionConsistent = false
        break
      }
    }
  }

  // The if statement runs only for row depth 0, the else statement for the rest
  if (targetParent === undefined) {
    rows.forEach(row => {
      if (row.depth === 0) {
        updateExpandedRows[row.id] = !isExpansionConsistent
          ? true
          : !row.getIsExpanded()
      }
    })
  } else {
    for (const row of rows) {
      if (row.getCanExpand() && targetParent === row.parentId) {
        updateExpandedRows[row.id] = !isExpansionConsistent
          ? true
          : !row.getIsExpanded()
      }
    }
  }

  return filterExpandableRows({
    ...(expanded as ExpandedStateObject),
    ...updateExpandedRows,
  })
}

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
playbook_ui-13.16.0.pre.alpha.powercentrainplaybookpt22125 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.16.0.pre.alpha.play1141iconkitusinglibrary2107 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.16.0.pre.alpha.PBNTR181stripedtable2103 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.16.0.pre.alpha.play1141iconkitusinglibrary2100 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.16.0.pre.alpha.PBNTR184betaflaginmenuyml2085 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.16.0.pre.alpha.PBNTR178AdvancedTableDocs2072 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.17.0 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.16.0.pre.alpha.PBNTR177NewAdvancedTableKit2035 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.16.0.pre.alpha.PBNTR177NewAdvancedTableKit2034 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.16.0.pre.alpha.PBNTR177NewAdvancedTableKit2023 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx