UNPKG

@atlaskit/table-tree

Version:

A table tree is an expandable table for showing nested hierarchies of information.

83 lines (80 loc) • 2.68 kB
import React, { useCallback, useMemo, useState } from 'react'; import Cell from './cell'; import Header from './header'; import Headers from './headers'; import { TableTreeContext } from './internal/context'; import Row from './row'; import Rows from './rows'; /** * This is hard-coded here because our actual <TableTree /> has no typings * for its props. * * Adding types for real *might* break things so will need a little care. * * Defining it here for now lets us provide *something* without much headache. */ const emptyColumnWidths = []; function TableTree({ children, columns, columnWidths: defaultColumnWidths = emptyColumnWidths, headers, shouldExpandOnClick, items, mainColumnForExpandCollapseLabel, label, referencedLabel }) { const [columnWidths, setColumnWidths] = useState(defaultColumnWidths); const setColumnWidth = useCallback((columnIndex, width) => { if (width === columnWidths[columnIndex]) { return; } setColumnWidths(columnWidths => { const newColumnWidths = [...columnWidths]; newColumnWidths[columnIndex] = width; return newColumnWidths; }); }, [columnWidths]); const getColumnWidth = useCallback(columnIndex => { return columnWidths[columnIndex] || null; }, [columnWidths]); const contextValue = useMemo(() => ({ setColumnWidth, getColumnWidth }), [setColumnWidth, getColumnWidth]); const heads = headers && /*#__PURE__*/React.createElement(Headers, null, headers.map((header, index) => /*#__PURE__*/React.createElement(Header, { key: index, columnIndex: index, width: columnWidths[index] }, header))); return /*#__PURE__*/React.createElement(TableTreeContext.Provider, { value: contextValue }, /*#__PURE__*/React.createElement("div", { role: "treegrid", "aria-readonly": true, "aria-label": label, "aria-labelledby": referencedLabel }, heads, columns && items && /*#__PURE__*/React.createElement(Rows, { items: items, render: ({ id, children, content }) => /*#__PURE__*/React.createElement(Row, { itemId: id, items: children, hasChildren: !!children && children.length > 0, shouldExpandOnClick: shouldExpandOnClick, mainColumnForExpandCollapseLabel: mainColumnForExpandCollapseLabel }, columns.map((CellContent, index) => { return /*#__PURE__*/React.createElement(Cell, { key: `cell-${index}`, columnIndex: index, width: columnWidths[index] }, /*#__PURE__*/React.createElement(CellContent, content)); })) }), children)); } // eslint-disable-next-line @repo/internal/react/require-jsdoc export default TableTree;