@ui5/webcomponents-react
Version:
React Wrapper for UI5 Web Components and additional components
195 lines • 8.1 kB
JavaScript
import { clsx } from 'clsx';
import { useEffect, useMemo, useRef, createElement as _createElement } from 'react';
import { RenderColumnTypes } from '../types/index.js';
import { getSubRowsByString } from '../util/index.js';
import { EmptyRow } from './EmptyRow.js';
import { RowSubComponent as SubComponent } from './RowSubComponent.js';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
function getDirectionStyles(isRtl, virtualColumn) {
return isRtl ? {
transform: `translateX(-${virtualColumn.start}px)`,
insertInlineStart: 0
} : {
transform: `translateX(${virtualColumn.start}px)`,
insertInlineStart: 0
};
}
export const VirtualTableBody = props => {
const {
alternateRowColor,
classes,
prepareRow,
rows,
isTreeTable,
internalRowHeight,
visibleColumns,
renderRowSubComponent,
popInRowHeight,
markNavigatedRow,
isRtl,
alwaysShowSubComponent,
dispatch,
subComponentsHeight,
columnVirtualizer,
manualGroupBy,
subRowsKey,
scrollContainerRef,
triggerScroll,
rowVirtualizer
} = props;
const rowHeight = popInRowHeight !== internalRowHeight ? popInRowHeight : internalRowHeight;
const lastNonEmptyRow = useRef(null);
useEffect(() => {
if (triggerScroll && triggerScroll.direction === 'vertical') {
if (triggerScroll.type === 'offset') {
rowVirtualizer.scrollToOffset(...triggerScroll.args);
} else {
rowVirtualizer.scrollToIndex(...triggerScroll.args);
}
}
}, [triggerScroll]);
const popInColumn = useMemo(() => visibleColumns.filter(item => item.id !== '__ui5wcr__internal_highlight_column' && item.id !== '__ui5wcr__internal_selection_column' && item.id !== '__ui5wcr__internal_navigation_column')[0], [visibleColumns]);
return /*#__PURE__*/_jsx("div", {
ref: scrollContainerRef,
"data-component-name": "AnalyticalTableBodyScrollableContainer",
style: {
position: 'relative',
height: `${rowVirtualizer.getTotalSize()}px`,
width: `${columnVirtualizer.getTotalSize()}px`
},
children: rowVirtualizer.getVirtualItems().map((virtualRow, visibleRowIndex) => {
const row = rows[virtualRow.index];
const rowIndexWithHeader = virtualRow.index + 1;
if (!row || row.groupByVal === 'undefined') {
const alternate = alternateRowColor && virtualRow.index % 2 !== 0;
if (!lastNonEmptyRow.current?.cells) {
return /*#__PURE__*/_jsx(EmptyRow, {
virtualRow: virtualRow,
className: clsx(classes.tr, alternate && classes.alternateRowColor)
}, `empty_row_${virtualRow.index}`);
}
const cells = lastNonEmptyRow.current.cells;
return /*#__PURE__*/_jsx(EmptyRow, {
virtualRow: virtualRow,
className: clsx(classes.tr, alternate && classes.alternateRowColor),
children: columnVirtualizer.getVirtualItems().map(item => {
const cell = cells[item.index];
const cellProps = cell.getCellProps();
const {
'aria-colindex': _0,
'aria-selected': _1,
'aria-label': _2,
tabIndex: _3,
...emptyRowCellProps
} = cellProps;
return /*#__PURE__*/_createElement("div", {
...emptyRowCellProps,
key: `${visibleRowIndex}-${emptyRowCellProps.key}`,
"data-empty-row-cell": "true",
tabIndex: -1,
"aria-hidden": "true",
style: {
...emptyRowCellProps.style,
cursor: 'unset',
position: 'absolute',
width: `${item.size}px`,
...getDirectionStyles(isRtl, item)
}
});
})
}, `empty_row_${virtualRow.index}`);
} else {
lastNonEmptyRow.current = row;
}
prepareRow(row);
const {
key,
...rowProps
} = row.getRowProps({
'aria-rowindex': virtualRow.index + 2,
'data-virtual-row-index': virtualRow.index
});
const isNavigatedCell = typeof markNavigatedRow === 'function' ? markNavigatedRow(row) : false;
const RowSubComponent = typeof renderRowSubComponent === 'function' ? renderRowSubComponent(row) : undefined;
let updatedHeight = rowHeight;
if (renderRowSubComponent && (rows[virtualRow.index]?.isExpanded || alwaysShowSubComponent) && subComponentsHeight?.[virtualRow.index]?.rowId === rows[virtualRow.index]?.id) {
updatedHeight += subComponentsHeight?.[virtualRow.index]?.subComponentHeight ?? 0;
}
const measureRef = isTreeTable && renderRowSubComponent && (row.isExpanded || alwaysShowSubComponent) ? node => {
rowVirtualizer.measureElement(node);
} : rowVirtualizer.measureElement;
return /*#__PURE__*/_jsxs("div", {
...rowProps,
ref: measureRef,
style: {
...(rowProps.style ?? {}),
transform: `translateY(${virtualRow.start}px)`,
position: 'absolute',
boxSizing: 'border-box',
'--_ui5wcr_AnalyticalTable_RowHeight': `${updatedHeight}px`
},
children: [typeof renderRowSubComponent === 'function' && /*#__PURE__*/_jsx(SubComponent, {
subComponentsHeight: subComponentsHeight,
virtualRow: virtualRow,
dispatch: dispatch,
row: row,
rowHeight: rowHeight,
rows: rows,
alwaysShowSubComponent: alwaysShowSubComponent,
rowIndex: visibleRowIndex + 1,
classNames: classes,
renderSubComp: RowSubComponent && (row.isExpanded || alwaysShowSubComponent),
children: RowSubComponent
}), columnVirtualizer.getVirtualItems().map((virtualColumn, visibleColumnIndex) => {
const cell = row.cells[virtualColumn.index];
if (!cell) {
return null;
}
const {
key,
...cellProps
} = cell.getCellProps();
const allCellProps = {
...cellProps,
['data-visible-column-index']: visibleColumnIndex,
['data-column-index']: virtualColumn.index,
['data-visible-row-index']: visibleRowIndex + 1,
['data-row-index']: rowIndexWithHeader,
style: {
...cellProps.style,
position: 'absolute',
width: `${virtualColumn.size}px`,
top: 0,
height: `${rowHeight}px`,
...getDirectionStyles(isRtl, virtualColumn)
}
};
let contentToRender;
if (cell.column.id === '__ui5wcr__internal_highlight_column' || cell.column.id === '__ui5wcr__internal_selection_column' || cell.column.id === '__ui5wcr__internal_navigation_column') {
contentToRender = RenderColumnTypes.Cell;
} else if (isTreeTable || !alwaysShowSubComponent && RowSubComponent) {
contentToRender = RenderColumnTypes.Expandable;
} else if (cell.isGrouped || manualGroupBy && cell.column.isGrouped && getSubRowsByString(subRowsKey, row.original) != null && cell.value !== undefined) {
contentToRender = RenderColumnTypes.Grouped;
} else if (cell.isAggregated) {
contentToRender = RenderColumnTypes.Aggregated;
} else if (cell.isPlaceholder) {
contentToRender = RenderColumnTypes.RepeatedValue;
} else {
contentToRender = RenderColumnTypes.Cell;
}
return /*#__PURE__*/_jsx("div", {
...allCellProps,
"data-selection-cell": cell.column.id === '__ui5wcr__internal_selection_column',
children: popInRowHeight !== internalRowHeight && popInColumn.id === cell.column.id ? cell.render(RenderColumnTypes.PopIn, {
contentToRender,
internalRowHeight
}) : cell.render(contentToRender, isNavigatedCell === true ? {
isNavigatedCell
} : {})
}, key);
})]
}, key);
})
});
};