@carbon/react
Version:
React components for the Carbon Design System
138 lines (129 loc) • 5.3 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.
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var cx = require('classnames');
var PropTypes = require('prop-types');
var React = require('react');
var iconsReact = require('@carbon/icons-react');
var TableCell = require('./TableCell.js');
var usePrefix = require('../../internal/usePrefix.js');
var TableSlugRow = require('./TableSlugRow.js');
var TableDecoratorRow = require('./TableDecoratorRow.js');
var index = require('../AILabel/index.js');
var utils = require('../../internal/utils.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx);
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
const TableExpandRow = /*#__PURE__*/React__default["default"].forwardRef(({
['aria-controls']: ariaControls,
['aria-label']: ariaLabel,
ariaLabel: deprecatedAriaLabel,
className: rowClassName,
children,
isExpanded,
onExpand,
expandIconDescription,
isSelected,
expandHeader = 'expand',
...rest
}, ref) => {
const prefix = usePrefix.usePrefix();
// We need to put the AILabel and Decorator before the expansion arrow and all other table cells after the arrow.
let rowHasAILabel;
const decorator = React.Children.toArray(children).map(child => {
if (utils.isComponentElement(child, TableSlugRow["default"])) {
if (child.props.slug) {
rowHasAILabel = true;
}
return child;
} else if (utils.isComponentElement(child, TableDecoratorRow["default"])) {
if (utils.isComponentElement(child.props.decorator, index.AILabel)) {
rowHasAILabel = true;
}
return child;
}
});
const normalizedChildren = React.Children.toArray(children).map(child => {
if (/*#__PURE__*/React.isValidElement(child) && child.type !== TableSlugRow["default"] && child.type !== TableDecoratorRow["default"]) {
return child;
}
});
const className = cx__default["default"]({
[`${prefix}--parent-row`]: true,
[`${prefix}--expandable-row`]: isExpanded,
[`${prefix}--data-table--selected`]: isSelected,
[`${prefix}--data-table--slug-row ${prefix}--data-table--ai-label-row`]: rowHasAILabel
}, rowClassName);
const previousValue = isExpanded ? 'collapsed' : undefined;
return /*#__PURE__*/React__default["default"].createElement("tr", _rollupPluginBabelHelpers["extends"]({}, rest, {
ref: ref,
className: className,
"data-parent-row": true
}), decorator, /*#__PURE__*/React__default["default"].createElement(TableCell["default"], {
className: `${prefix}--table-expand`,
"data-previous-value": previousValue,
headers: expandHeader
}, /*#__PURE__*/React__default["default"].createElement("button", {
type: "button",
className: `${prefix}--table-expand__button`,
onClick: onExpand,
title: expandIconDescription,
"aria-label": deprecatedAriaLabel || ariaLabel,
"aria-expanded": isExpanded,
"aria-controls": ariaControls
}, /*#__PURE__*/React__default["default"].createElement(iconsReact.ChevronRight, {
className: `${prefix}--table-expand__svg`,
"aria-label": expandIconDescription
}))), normalizedChildren);
});
TableExpandRow.propTypes = {
/**
* Space separated list of one or more ID values referencing the TableExpandedRow(s) being controlled by the TableExpandRow
* TODO: make this required in v12
*/
['aria-controls']: PropTypes__default["default"].string,
/**
* Specify the string read by a voice reader when the expand trigger is
* focused
*/
/**@ts-ignore*/
['aria-label']: PropTypes__default["default"].string,
/**
* Deprecated, please use `aria-label` instead.
* Specify the string read by a voice reader when the expand trigger is
* focused
*/
ariaLabel: PropTypes__default["default"].string,
children: PropTypes__default["default"].node,
className: PropTypes__default["default"].string,
/**
* The id of the matching th node in the table head. Addresses a11y concerns outlined here: https://www.ibm.com/able/guidelines/ci162/info_and_relationships.html and https://www.w3.org/TR/WCAG20-TECHS/H43
*/
expandHeader: PropTypes__default["default"].string,
/**
* The description of the chevron right icon, to be put in its SVG `<title>` element.
*/
expandIconDescription: PropTypes__default["default"].string,
/**
* Specify whether this row is expanded or not. This helps coordinate data
* attributes so that `TableExpandRow` and `TableExpandedRow` work together
*/
isExpanded: PropTypes__default["default"].bool,
/**
* Specify if the row is selected
*/
isSelected: PropTypes__default["default"].bool,
/**
* Hook for when a listener initiates a request to expand the given row
*/
onExpand: PropTypes__default["default"].func.isRequired
};
TableExpandRow.displayName = 'TableExpandRow';
exports["default"] = TableExpandRow;