UNPKG

@carbon/react

Version:

React components for the Carbon Design System

92 lines (90 loc) 3.33 kB
/** * Copyright IBM Corp. 2016, 2026 * * 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 { usePrefix } from "../../internal/usePrefix.js"; import { isComponentElement } from "../../internal/utils.js"; import { AILabel } from "../AILabel/index.js"; import TableCell from "./TableCell.js"; import TableDecoratorRow from "./TableDecoratorRow.js"; import TableSlugRow from "./TableSlugRow.js"; import classNames from "classnames"; import { Children, forwardRef } from "react"; import PropTypes from "prop-types"; import { jsx, jsxs } from "react/jsx-runtime"; import { ChevronRight } from "@carbon/icons-react"; //#region src/components/DataTable/TableExpandRow.tsx /** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const TableExpandRow = forwardRef(({ ["aria-controls"]: ariaControls, ["aria-label"]: ariaLabel, ariaLabel: deprecatedAriaLabel, className: rowClassName, children, isExpanded, onExpand, expandIconDescription, isSelected, expandHeader = "expand", ...rest }, ref) => { const prefix = usePrefix(); let rowHasAILabel; const decorator = Children.toArray(children).map((child) => { if (isComponentElement(child, TableSlugRow)) { if (child.props.slug) rowHasAILabel = true; return child; } else if (isComponentElement(child, TableDecoratorRow)) { if (isComponentElement(child.props.decorator, AILabel)) rowHasAILabel = true; return child; } }); const normalizedChildren = Children.toArray(children).map((child) => { if (!isComponentElement(child, TableSlugRow) && !isComponentElement(child, TableDecoratorRow)) return child; }); const className = classNames({ [`${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" : void 0; return /* @__PURE__ */ jsxs("tr", { ...rest, ref, className, "data-parent-row": true, children: [ decorator, /* @__PURE__ */ jsx(TableCell, { className: `${prefix}--table-expand`, "data-previous-value": previousValue, headers: expandHeader, children: /* @__PURE__ */ jsx("button", { type: "button", className: `${prefix}--table-expand__button`, onClick: onExpand, title: expandIconDescription, "aria-label": deprecatedAriaLabel || ariaLabel, "aria-expanded": isExpanded, "aria-controls": ariaControls, children: /* @__PURE__ */ jsx(ChevronRight, { className: `${prefix}--table-expand__svg`, "aria-label": expandIconDescription }) }) }), normalizedChildren ] }); }); TableExpandRow.propTypes = { "aria-controls": PropTypes.string, "aria-label": PropTypes.string, ariaLabel: PropTypes.string, children: PropTypes.node, className: PropTypes.string, expandHeader: PropTypes.string, expandIconDescription: PropTypes.string, isExpanded: PropTypes.bool, isSelected: PropTypes.bool, onExpand: PropTypes.func.isRequired }; TableExpandRow.displayName = "TableExpandRow"; //#endregion export { TableExpandRow as default };