es-grid-template
Version:
es-grid-template
84 lines (83 loc) • 3.17 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import TableBodyRow from "./TableBodyRow";
// import type { Virtualizer } from "@tanstack/react-virtual";
import { useVirtualizer } from "@tanstack/react-virtual";
// import type { Dispatch, SetStateAction } from "react";
import React, { Fragment } from "react";
import { TableContext } from "../useContext";
import { Empty } from "rc-master-ui";
const TableBody = ({
columnVirtualizer,
table,
tableContainerRef,
virtualPaddingLeft,
virtualPaddingRight,
fixedLeftColumns,
fixedRightColumns,
tableId,
showEmptyText,
...rest
}) => {
const {
rows
} = table.getRowModel();
const {
editingKey,
prefix,
locale
} = React.useContext(TableContext);
//dynamic row height virtualization - alternatively you could use a simpler fixed row height strategy without the need for `measureElement`
const rowVirtualizer = useVirtualizer({
count: rows.length,
estimateSize: () => 36,
//estimate row height for accurate scrollbar dragging
getScrollElement: () => tableContainerRef.current,
//measure dynamic row height, except in firefox because it measures table border height incorrectly
measureElement: typeof window !== 'undefined' && navigator.userAgent.indexOf('Firefox') === -1 ? element => element?.getBoundingClientRect().height : undefined,
overscan: 1
});
const virtualRows = rowVirtualizer.getVirtualItems();
return /*#__PURE__*/React.createElement("div", {
className: `${prefix}-grid-tbody`,
style: {
display: 'grid',
// height: `${rowVirtualizer.getTotalSize()}px`, //tells scrollbar how big the table is
height: showEmptyText && rows.length === 0 ? `140px` : `${rowVirtualizer.getTotalSize()}px`,
//tells scrollbar how big the table is
position: 'relative' //needed for absolute positioning of rows
}
}, showEmptyText && rows.length === 0 ? /*#__PURE__*/React.createElement("tr", {
style: {
// transform: `translateY(${virtualRow.start}px)`, //this should always be a `style` as it changes on scroll
}
}, /*#__PURE__*/React.createElement("td", {
// colSpan={30}
style: {
position: "sticky",
left: 0,
width: tableContainerRef.current?.clientWidth,
overflow: 'hidden'
}
}, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Empty, {
image: Empty.PRESENTED_IMAGE_SIMPLE,
description: locale?.table?.emptyText
})))) : /*#__PURE__*/React.createElement(Fragment, null, virtualRows.map(virtualRow => {
const row = rows[virtualRow.index];
const isEditing = row.id === editingKey;
return /*#__PURE__*/React.createElement(TableBodyRow, _extends({}, rest, {
table: table,
tableId: tableId,
key: row.id,
row: row,
columnVirtualizer: columnVirtualizer,
rowVirtualizer: rowVirtualizer,
virtualPaddingLeft: virtualPaddingLeft,
virtualPaddingRight: virtualPaddingRight,
virtualRow: virtualRow,
fixedLeftColumns: fixedLeftColumns,
fixedRightColumns: fixedRightColumns,
isEditing: isEditing
}));
})));
};
export default TableBody;