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

package.src.core.cell.ts Maven / Gradle / Ivy

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

export interface CellContext {
  table: Table
  column: Column
  row: Row
  cell: Cell
  getValue: Getter
  renderValue: Getter
}

export interface CoreCell {
  id: string
  getValue: CellContext['getValue']
  renderValue: CellContext['renderValue']
  row: Row
  column: Column
  getContext: () => CellContext
}

export function createCell(
  table: Table,
  row: Row,
  column: Column,
  columnId: string
): Cell {
  const getRenderValue = () =>
    cell.getValue() ?? table.options.renderFallbackValue

  const cell: CoreCell = {
    id: `${row.id}_${column.id}`,
    row,
    column,
    getValue: () => row.getValue(columnId),
    renderValue: getRenderValue,
    getContext: memo(
      () => [table, column, row, cell],
      (table, column, row, cell) => ({
        table,
        column,
        row,
        cell: cell as Cell,
        getValue: cell.getValue,
        renderValue: cell.renderValue,
      }),
      {
        key: process.env.NODE_ENV === 'development' && 'cell.getContext',
        debug: () => table.options.debugAll,
      }
    ),
  }

  table._features.forEach(feature => {
    Object.assign(
      cell,
      feature.createCell?.(
        cell as Cell,
        column,
        row as Row,
        table
      )
    )
  }, {})

  return cell as Cell
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy