@carbon/react
Version:
React components for the Carbon Design System
107 lines (103 loc) • 3.69 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import PropTypes from 'prop-types';
import React from 'react';
import cx from 'classnames';
import { usePrefix } from '../../internal/usePrefix.js';
var _span, _span2;
const DataTableSkeleton = ({
headers,
rowCount = 5,
columnCount = 5,
zebra = false,
compact = false,
className,
showHeader = true,
showToolbar = true,
...rest
}) => {
const prefix = usePrefix();
const dataTableSkeletonClasses = cx(className, {
[`${prefix}--skeleton`]: true,
[`${prefix}--data-table`]: true,
[`${prefix}--data-table--zebra`]: zebra,
[`${prefix}--data-table--compact`]: compact
});
const rowRepeat = rowCount;
const rows = Array(rowRepeat);
const columnsArray = Array.from({
length: columnCount
}, (_, index) => index);
for (let i = 0; i < rowRepeat; i++) {
rows[i] = /*#__PURE__*/React.createElement("tr", {
key: i
}, columnsArray.map(j => /*#__PURE__*/React.createElement("td", {
key: j
}, _span || (_span = /*#__PURE__*/React.createElement("span", null)))));
}
return /*#__PURE__*/React.createElement("div", {
className: `${prefix}--skeleton ${prefix}--data-table-container`
}, showHeader ? /*#__PURE__*/React.createElement("div", {
className: `${prefix}--data-table-header`
}, /*#__PURE__*/React.createElement("div", {
className: `${prefix}--data-table-header__title`
}), /*#__PURE__*/React.createElement("div", {
className: `${prefix}--data-table-header__description`
})) : null, showToolbar ? /*#__PURE__*/React.createElement("section", {
"aria-label": "data table toolbar",
className: `${prefix}--table-toolbar`
}, /*#__PURE__*/React.createElement("div", {
className: `${prefix}--toolbar-content`
}, /*#__PURE__*/React.createElement("span", {
className: `${prefix}--skeleton ${prefix}--btn ${prefix}--btn--sm`
}))) : null, /*#__PURE__*/React.createElement("table", _extends({
className: dataTableSkeletonClasses
}, rest), /*#__PURE__*/React.createElement("thead", null, /*#__PURE__*/React.createElement("tr", null, columnsArray.map(i => /*#__PURE__*/React.createElement("th", {
key: i
}, headers ? /*#__PURE__*/React.createElement("div", {
className: "cds--table-header-label"
}, headers[i]?.header) : _span2 || (_span2 = /*#__PURE__*/React.createElement("span", null)))))), /*#__PURE__*/React.createElement("tbody", null, rows)));
};
DataTableSkeleton.propTypes = {
/**
* Specify an optional className to add.
*/
className: PropTypes.string,
/**
* Specify the number of columns that you want to render in the skeleton state
*/
columnCount: PropTypes.number,
/**
* Optionally specify whether you want the Skeleton to be rendered as a
* compact DataTable
*/
compact: PropTypes.bool,
/**
* Optionally specify the displayed headers
*/
headers: PropTypes.arrayOf(PropTypes.shape({
header: PropTypes.node.isRequired
}).isRequired),
/**
* Specify the number of rows that you want to render in the skeleton state
*/
rowCount: PropTypes.number,
/**
* Specify if the table header should be rendered as part of the skeleton.
*/
showHeader: PropTypes.bool,
/**
* Specify if the table toolbar should be rendered as part of the skeleton.
*/
showToolbar: PropTypes.bool,
/**
* Optionally specify whether you want the DataTable to be zebra striped
*/
zebra: PropTypes.bool
};
export { DataTableSkeleton as default };