All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.src.utils.getExpandedRowModel.ts Maven / Gradle / Ivy

The newest version!
import { Table, Row, RowModel, RowData } from '../types'
import { memo } from '../utils'

export function getExpandedRowModel(): (
  table: Table
) => () => RowModel {
  return table =>
    memo(
      () => [
        table.getState().expanded,
        table.getPreExpandedRowModel(),
        table.options.paginateExpandedRows,
      ],
      (expanded, rowModel, paginateExpandedRows) => {
        if (
          !rowModel.rows.length ||
          (expanded !== true && !Object.keys(expanded ?? {}).length)
        ) {
          return rowModel
        }

        if (!paginateExpandedRows) {
          // Only expand rows at this point if they are being paginated
          return rowModel
        }

        return expandRows(rowModel)
      },
      {
        key: process.env.NODE_ENV === 'development' && 'getExpandedRowModel',
        debug: () => table.options.debugAll ?? table.options.debugTable,
      }
    )
}

export function expandRows(rowModel: RowModel) {
  const expandedRows: Row[] = []

  const handleRow = (row: Row) => {
    expandedRows.push(row)

    if (row.subRows?.length && row.getIsExpanded()) {
      row.subRows.forEach(handleRow)
    }
  }

  rowModel.rows.forEach(handleRow)

  return {
    rows: expandedRows,
    flatRows: rowModel.flatRows,
    rowsById: rowModel.rowsById,
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy