@carbon/react
Version:
React components for the Carbon Design System
132 lines (125 loc) • 4.64 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.
*/
'use strict';
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');
const TableExpandRow = /*#__PURE__*/React.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({
[`${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.createElement("tr", _rollupPluginBabelHelpers.extends({}, rest, {
ref: ref,
className: className,
"data-parent-row": true
}), decorator, /*#__PURE__*/React.createElement(TableCell.default, {
className: `${prefix}--table-expand`,
"data-previous-value": previousValue,
headers: expandHeader
}, /*#__PURE__*/React.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.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.string,
/**
* Specify the string read by a voice reader when the expand trigger is
* focused
*/
/**@ts-ignore*/
['aria-label']: PropTypes.string,
/**
* Deprecated, please use `aria-label` instead.
* Specify the string read by a voice reader when the expand trigger is
* focused
*/
ariaLabel: PropTypes.string,
children: PropTypes.node,
className: PropTypes.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.string,
/**
* The description of the chevron right icon, to be put in its SVG `<title>` element.
*/
expandIconDescription: PropTypes.string,
/**
* Specify whether this row is expanded or not. This helps coordinate data
* attributes so that `TableExpandRow` and `TableExpandedRow` work together
*/
isExpanded: PropTypes.bool,
/**
* Specify if the row is selected
*/
isSelected: PropTypes.bool,
/**
* Hook for when a listener initiates a request to expand the given row
*/
onExpand: PropTypes.func.isRequired
};
TableExpandRow.displayName = 'TableExpandRow';
exports.default = TableExpandRow;