UNPKG

@atlaskit/table-tree

Version:

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

93 lines (90 loc) • 3.42 kB
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; 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. */ var emptyColumnWidths = []; function TableTree(_ref) { var children = _ref.children, columns = _ref.columns, _ref$columnWidths = _ref.columnWidths, defaultColumnWidths = _ref$columnWidths === void 0 ? emptyColumnWidths : _ref$columnWidths, headers = _ref.headers, shouldExpandOnClick = _ref.shouldExpandOnClick, items = _ref.items, mainColumnForExpandCollapseLabel = _ref.mainColumnForExpandCollapseLabel, label = _ref.label, referencedLabel = _ref.referencedLabel; var _useState = useState(defaultColumnWidths), _useState2 = _slicedToArray(_useState, 2), columnWidths = _useState2[0], setColumnWidths = _useState2[1]; var setColumnWidth = useCallback(function (columnIndex, width) { if (width === columnWidths[columnIndex]) { return; } setColumnWidths(function (columnWidths) { var newColumnWidths = _toConsumableArray(columnWidths); newColumnWidths[columnIndex] = width; return newColumnWidths; }); }, [columnWidths]); var getColumnWidth = useCallback(function (columnIndex) { return columnWidths[columnIndex] || null; }, [columnWidths]); var contextValue = useMemo(function () { return { setColumnWidth: setColumnWidth, getColumnWidth: getColumnWidth }; }, [setColumnWidth, getColumnWidth]); var heads = headers && /*#__PURE__*/React.createElement(Headers, null, headers.map(function (header, index) { return /*#__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: function render(_ref2) { var id = _ref2.id, children = _ref2.children, content = _ref2.content; return /*#__PURE__*/React.createElement(Row, { itemId: id, items: children, hasChildren: !!children && children.length > 0, shouldExpandOnClick: shouldExpandOnClick, mainColumnForExpandCollapseLabel: mainColumnForExpandCollapseLabel }, columns.map(function (CellContent, index) { return /*#__PURE__*/React.createElement(Cell, { key: "cell-".concat(index), columnIndex: index, width: columnWidths[index] }, /*#__PURE__*/React.createElement(CellContent, content)); })); } }), children)); } // eslint-disable-next-line @repo/internal/react/require-jsdoc export default TableTree;