UNPKG

@atlaskit/dynamic-table

Version:

A dynamic table displays rows of data with built-in pagination, sorting, and re-ordering functionality.

121 lines (119 loc) 5.97 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _react = _interopRequireDefault(require("react")); var _validateSortKey = require("../internal/validate-sort-key"); var _head = require("../styled/head"); var _tableHeadCell = _interopRequireDefault(require("./rankable/table-head-cell")); var _tableHeadCell2 = _interopRequireDefault(require("./table-head-cell")); var _excluded = ["cells"], _excluded2 = ["ascendingSortTooltip", "buttonAriaRoleDescription", "colSpan", "content", "descendingSortTooltip", "isIconOnlyHeader", "isSortable", "key", "shouldTruncate", "testId", "width"]; function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } // eslint-disable-next-line @repo/internal/react/no-class-components var TableHead = /*#__PURE__*/function (_React$Component) { function TableHead(props) { var _this; (0, _classCallCheck2.default)(this, TableHead); _this = _callSuper(this, TableHead, [props]); _this.state = { activeSortButtonId: null }; return _this; } (0, _inherits2.default)(TableHead, _React$Component); return (0, _createClass2.default)(TableHead, [{ key: "UNSAFE_componentWillMount", value: function UNSAFE_componentWillMount() { (0, _validateSortKey.validateSortKey)(this.props.sortKey, this.props.head); } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { if (this.props.sortKey !== nextProps.sortKey || this.props.head !== nextProps.head) { (0, _validateSortKey.validateSortKey)(nextProps.sortKey, nextProps.head); } } }, { key: "render", value: function render() { var _this2 = this; var _this$props = this.props, head = _this$props.head, sortKey = _this$props.sortKey, sortOrder = _this$props.sortOrder, isFixedSize = _this$props.isFixedSize, onSort = _this$props.onSort, isRanking = _this$props.isRanking, isRankable = _this$props.isRankable, testId = _this$props.testId; var activeSortButtonId = this.state.activeSortButtonId; if (!head) { return null; } var HeadCellComponent = isRankable ? _tableHeadCell.default : _tableHeadCell2.default; // TODO: Remove `rest` props and use only what is explicitly in the API. // Some tests use this to pass in onClick and other stuff within the `head` // variable here, but considering it's not in the API, it should probably // be removed. var cells = head.cells, rest = (0, _objectWithoutProperties2.default)(head, _excluded); return /*#__PURE__*/_react.default.createElement(_head.Head, (0, _extends2.default)({}, rest, { isRanking: isRanking, testId: testId && "".concat(testId, "--head") }), /*#__PURE__*/_react.default.createElement("tr", null, cells.map(function (cell, index) { var ascendingSortTooltip = cell.ascendingSortTooltip, buttonAriaRoleDescription = cell.buttonAriaRoleDescription, colSpan = cell.colSpan, content = cell.content, descendingSortTooltip = cell.descendingSortTooltip, isIconOnlyHeader = cell.isIconOnlyHeader, isSortable = cell.isSortable, key = cell.key, shouldTruncate = cell.shouldTruncate, cellTestId = cell.testId, width = cell.width, restCellProps = (0, _objectWithoutProperties2.default)(cell, _excluded2); var headCellId = "head-cell-".concat(index); var handleClick = function handleClick() { _this2.setState({ activeSortButtonId: headCellId }); if (isSortable) { onSort(cell)(); } }; return /*#__PURE__*/_react.default.createElement(HeadCellComponent, (0, _extends2.default)({ colSpan: colSpan, content: content, isFixedSize: isFixedSize, isIconOnlyHeader: isIconOnlyHeader, isSortable: !!isSortable, isRanking: isRanking, key: key || index, headCellId: headCellId, activeSortButtonId: activeSortButtonId, onClick: handleClick, testId: cellTestId || testId, shouldTruncate: shouldTruncate, sortOrder: key === sortKey ? sortOrder : undefined, width: width, ascendingSortTooltip: ascendingSortTooltip, descendingSortTooltip: descendingSortTooltip, buttonAriaRoleDescription: buttonAriaRoleDescription }, restCellProps)); }))); } }]); }(_react.default.Component); // eslint-disable-next-line @repo/internal/react/require-jsdoc var _default = exports.default = TableHead;