@carbon/react
Version:
React components for the Carbon Design System
82 lines (78 loc) • 2.59 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 cx from 'classnames';
import PropTypes from 'prop-types';
import React, { useMemo } from 'react';
import { usePrefix } from '../../internal/usePrefix.js';
import { useId } from '../../internal/useId.js';
import { TableContext } from './TableContext.js';
import { Section, Heading } from '../Heading/index.js';
const TableContainer = ({
aiEnabled,
className,
children,
title,
description,
stickyHeader,
useStaticWidth,
...rest
}) => {
const baseId = useId('tc');
const titleId = `${baseId}-title`;
const descriptionId = `${baseId}-description`;
const prefix = usePrefix();
const tableContainerClasses = cx(className, `${prefix}--data-table-container`, {
[`${prefix}--data-table--max-width`]: stickyHeader,
[`${prefix}--data-table-container--static`]: useStaticWidth,
[`${prefix}--data-table-container--ai-enabled`]: aiEnabled
});
const value = useMemo(() => {
return {
titleId: title ? titleId : undefined,
descriptionId: description ? descriptionId : undefined
};
}, [title, description, titleId, descriptionId]);
return /*#__PURE__*/React.createElement(TableContext.Provider, {
value: value
}, /*#__PURE__*/React.createElement(Section, _extends({}, rest, {
className: tableContainerClasses
}), (title || description) && /*#__PURE__*/React.createElement("div", {
className: `${prefix}--data-table-header`
}, title && /*#__PURE__*/React.createElement(Heading, {
className: `${prefix}--data-table-header__title`,
id: titleId
}, title), description && /*#__PURE__*/React.createElement("p", {
className: `${prefix}--data-table-header__description`,
id: descriptionId
}, description)), children));
};
TableContainer.propTypes = {
/**
* Specify if the entire table has AI generated contents
*/
aiEnabled: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
/**
* Optional description text for the Table
*/
description: PropTypes.node,
/**
* Specify whether the table should have a sticky header
*/
stickyHeader: PropTypes.bool,
/**
* Provide a title for the Table
*/
title: PropTypes.node,
/**
* If true, will use a width of 'fit-content' to match the inner table width
*/
useStaticWidth: PropTypes.bool
};
export { TableContainer as default };