import React, { useState } from "react"; import classNames from "classnames"; import { get } from "lodash"; import { VuiCheckbox, VuiTextInput } from "../form"; import { VuiSpacer } from "../spacer/Spacer"; import { Props as TableRowActionsProps, VuiTableRowActions } from "./TableRowActions"; import { VuiTableCell } from "./TableCell"; import { Props as TableHeaderCellProps, VuiTableHeaderCell } from "./TableHeaderCell"; import { Pagination, VuiTablePagination } from "./TablePagination"; import { Pager, VuiTablePager } from "./TablePager"; import { VuiFlexContainer } from "../flex/FlexContainer"; import { VuiFlexItem } from "../flex/FlexItem"; import { VuiText } from "../typography/Text"; import { Props as TableBulkActionProps, VuiTableBulkActions } from "./TableBulkActions"; import { VuiSpinner } from "../spinner/Spinner"; import { VuiTableContent } from "./TableContent"; import { VuiButtonSecondary } from "../button/ButtonSecondary"; import { Row } from "./types"; // Type guard to determine type of pagination. const isComplexPagination = (pagination: Pagination | Pager): pagination is Pagination => { return (pagination as Pagination).onSelectPage !== undefined; }; type Column = { name: string; width?: string; header: TableHeaderCellProps["header"]; render?: (row: T) => React.ReactNode; className?: string; }; type Props = { isLoading?: boolean; idField: string | ((row: T) => string); columns: Column[]; rows: T[]; actions?: TableRowActionsProps["actions"]; actionsTestIdProvider?: (row: T) => string; pagination?: Pagination | Pager; selection?: Selection; search?: Search; onSort?: TableHeaderCellProps["onSort"]; onReload?: () => void; content?: React.ReactNode; className?: string; fluid?: boolean; }; type Selection = { bulkActions?: TableBulkActionProps["actions"]; onSelectRow?: (selectedRows: T[]) => void; selectedRows?: T[]; }; type Search = { searchValue?: string; searchPlaceholder?: string; onSearchChange?: (value: string) => void; "data-testid"?: string; }; const extractId = (row: Record, idField: Props["idField"]) => { if (typeof idField === "string") { return get(row, idField); } return idField(row); }; // https://github.com/typescript-eslint/typescript-eslint/issues/4062 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint export const VuiTable = ({ isLoading, idField, columns, rows, actions, actionsTestIdProvider, pagination, selection, search, onSort, onReload, content, className, fluid, ...rest }: Props) => { const [rowBeingActedUpon, setRowBeingActedUpon] = useState(undefined); const { bulkActions, onSelectRow, selectedRows } = selection || {}; const { searchValue, searchPlaceholder, onSearchChange } = search || {}; const isEmpty = !isLoading && rows.length === 0; // The user interacts with the table rows by selecting them or performing actions on them. // This is only allowed if there is no “content” (which is an error or similar state that // replaces the rows), and if it’s not in a loading state or empty state. const isInteractive = Boolean(!content && !isLoading && !isEmpty); const allRowsSelected = selectedRows?.length === rows.length; const selectedIds: Record = selectedRows?.reduce((acc, row) => { acc[extractId(row, idField)] = true; return acc; }, {} as Record) || {}; const hasSearch = searchValue !== undefined && onSearchChange; const hasBulkActions = bulkActions !== undefined; const columnCount = columns.length + (onSelectRow ? 1 : 0) + (actions ? 1 : 0); const classes = classNames( "vuiTable", { "vuiTable--fluid": fluid }, className ); let tableContent; if (content) { tableContent = {content}; } else if (isLoading) { tableContent = (

Loading

); } else if (searchValue && isEmpty) { tableContent = (

No matches found

); } else { tableContent = rows.map((row) => { const rowId = extractId(row, idField); return ( {/* Checkbox column */} {onSelectRow && ( { if (selectedIds[rowId]) { delete selectedIds[rowId]; } else { selectedIds[rowId] = true; } const selectedRowIds = Object.keys(selectedIds); // Map selected row IDs to selected rows. const selectedRows = selectedRowIds.map((id) => rows.find((row) => row.id === id) as T); onSelectRow(selectedRows); }} /> )} {/* Row info */} {columns.map((column) => { const { name, render, className } = column; return ( {render ? render(row) : row[column.name]} ); })} {/* Actions column */} {actions && ( { if (isSelected) { setRowBeingActedUpon(row); } else { setRowBeingActedUpon(undefined); } }} testId={actionsTestIdProvider?.(row) ?? undefined} /> )} ); }); } return ( <> {(hasSearch || (hasBulkActions && selectedRows && selectedRows.length > 0) || Boolean(onReload)) && ( <> {/* Bulk actions */} {selectedRows && selectedRows.length > 0 && hasBulkActions && ( )} {/* Search */} {hasSearch && ( onSearchChange(event.target.value)} data-testid={search?.["data-testid"]} /> )} {/* Reload */} {onReload && ( onReload()}> Reload )} )} {/* Checkbox column */} {onSelectRow && ( )} {/* Row info */} {columns.map((column) => { const { name, header, width } = column; const styles = width ? { width } : undefined; return ( ); })} {/* Actions column */} {actions && {tableContent}
{ let newSelectedRows: T[]; if (allRowsSelected) { newSelectedRows = []; } else { newSelectedRows = rows.reduce((acc, row) => { acc.push(row); return acc; }, [] as T[]); } onSelectRow(newSelectedRows); }} /> }
{/* Pagination */} {!pagination ? undefined : isComplexPagination(pagination) ? ( <> ) : ( <> )} ); };