/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import "./table-cell.scss"; import type { TableSortBy, TableSortParams } from "./table"; import type { ReactNode } from "react"; import React from "react"; import { autoBind, cssNames } from "../../utils"; import { Icon } from "../icon"; import { Checkbox } from "../checkbox"; export type TableCellElem = React.ReactElement; export interface TableCellProps extends React.DOMAttributes { /** * used for configuration visibility of columns */ id?: string; /** * Any css class names for this table cell. Only used if `title` is a "simple" react node */ className?: string; /** * The actual value of the cell */ title?: ReactNode; /** * content inside could be scrolled */ scrollable?: boolean; /** * render cell with a checkbox */ checkbox?: boolean; /** * mark checkbox as checked or not */ isChecked?: boolean; /** * column name, must be same as key in sortable object */ sortBy?: TableSortBy; /** * id of the column which follow same visibility rules */ showWithColumn?: string; /** * @internal */ _sorting?: Partial; /** * @internal */ _sort?(sortBy: TableSortBy): void; /** * @internal * indicator, might come from parent , don't use this prop outside (!) */ _nowrap?: boolean; } export class TableCell extends React.Component { constructor(props: TableCellProps) { super(props); autoBind(this); } onClick(evt: React.MouseEvent) { const { _sort, sortBy, onClick } = this.props; onClick?.(evt); if (_sort && typeof sortBy === "string") { _sort(sortBy); } } renderSortIcon() { const { sortBy, _sorting } = this.props; if (!_sorting || !sortBy) { return null; } const sortActive = _sorting.sortBy === sortBy; const sortIconName = (!sortActive || _sorting.orderBy === "desc") ? "arrow_drop_down" : "arrow_drop_up"; return ( ); } renderCheckbox() { const { checkbox, isChecked } = this.props; const showCheckbox = isChecked !== undefined; if (checkbox && showCheckbox) { return ; } return null; } render() { const { className, checkbox, isChecked, scrollable, sortBy, _sort, _sorting, _nowrap, children, title, showWithColumn, ...cellProps } = this.props; const classNames = cssNames("TableCell", className, { checkbox, scrollable, nowrap: _nowrap, sorting: _sort && typeof sortBy === "string", }); const content = title || children; return (
{this.renderCheckbox()} {_nowrap ?
{content}
: content} {this.renderSortIcon()}
); } }