Sha256: c2a6a25a18b7ae8cb256632a0a60ce04ddf49edc5bfd5a922592fb12c09c2c30

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

import { RowModel } from "@tanstack/react-table"
import { ExpandedStateObject } from "./types"
import { GenericObject } 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<GenericObject>,
  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 (
      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 (targetParent === row.parentId) {
        updateExpandedRows[row.id] = !isExpansionConsistent
          ? true
          : !row.getIsExpanded()
      }
    }
  }

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
playbook_ui-13.21.0.pre.alpha.PBNTR224letuserresetrowexpansion2466 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.21.0.pre.alpha.PLAY120322465 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.21.0.pre.alpha.PBNTR225advancedtablefeedback2438 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx
playbook_ui-13.20.0.pre.alpha.PBNTR225advancedtablefeedback2375 app/pb_kits/playbook/pb_advanced_table/Utilities/ExpansionControlHelpers.tsx