@hitachivantara/uikit-react-core
Version:
UI Kit Core React components.
63 lines (62 loc) • 2.21 kB
JavaScript
import { HvTypography } from "../../Typography/Typography.js";
import { HvIcon } from "../../icons.js";
import { HvButton } from "../../Button/Button.js";
import { useLabels } from "../../hooks/useLabels.js";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
//#region src/Table/hooks/useHvRowExpand.tsx
var DEFAULT_LABELS = {
expandRowButtonAriaLabel: "Expand this row",
collapseRowButtonAriaLabel: "Collapse this row"
};
var CellWithExpandButton = ({ row, cell, labels: labelsProp }) => {
const labels = useLabels(DEFAULT_LABELS, labelsProp);
const rowProps = row.getToggleRowExpandedProps?.();
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(HvButton, {
icon: true,
"aria-label": row.isExpanded ? labels.collapseRowButtonAriaLabel : labels.expandRowButtonAriaLabel,
"aria-expanded": row.isExpanded,
onClick: rowProps?.onClick,
children: /* @__PURE__ */ jsx(HvIcon, {
name: "CaretDown",
size: "xs",
rotate: row.isExpanded
})
}), cell?.value && /* @__PURE__ */ jsx(HvTypography, {
variant: "label",
component: "span",
children: cell.value
})] });
};
var visibleColumnsHook = (columns, { instance }) => {
if (instance.disableCreateExpandButton) return columns;
const firstDataColumnIndex = columns.findIndex((c) => !c.id?.startsWith("_hv_"));
if (firstDataColumnIndex !== -1) {
const firstDataColumn = columns[firstDataColumnIndex];
if (firstDataColumn.Cell === CellWithExpandButton) return columns;
if (firstDataColumn.Cell == null) {
firstDataColumn.Cell = CellWithExpandButton;
firstDataColumn.variant = "expand";
return columns;
}
}
const expandColumn = {
id: "_hv_expand",
variant: "none",
width: 32,
sticky: "left",
Cell: CellWithExpandButton
};
const columnsCopy = [...columns];
columnsCopy.splice(firstDataColumnIndex !== -1 ? firstDataColumnIndex : 0, 0, expandColumn);
return columnsCopy;
};
var getRowPropsHook = (props, { row }) => {
return [props, { expanded: row.isExpanded }];
};
var useHvRowExpand = (hooks) => {
hooks.visibleColumns.push(visibleColumnsHook);
hooks.getRowProps.push(getRowPropsHook);
};
useHvRowExpand.pluginName = "useHvRowExpand";
//#endregion
export { useHvRowExpand };