index.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  3. import classNames from 'classnames';
  4. import { useEvent, warning } from 'rc-util';
  5. import * as React from 'react';
  6. import { INTERNAL_HOOKS } from "../constant";
  7. import { makeImmutable } from "../context/TableContext";
  8. import Table, { DEFAULT_PREFIX } from "../Table";
  9. import Grid from "./BodyGrid";
  10. import { StaticContext } from "./context";
  11. import getValue from "rc-util/es/utils/get";
  12. var renderBody = function renderBody(rawData, props) {
  13. var ref = props.ref,
  14. onScroll = props.onScroll;
  15. return /*#__PURE__*/React.createElement(Grid, {
  16. ref: ref,
  17. data: rawData,
  18. onScroll: onScroll
  19. });
  20. };
  21. function VirtualTable(props, ref) {
  22. var data = props.data,
  23. columns = props.columns,
  24. scroll = props.scroll,
  25. sticky = props.sticky,
  26. _props$prefixCls = props.prefixCls,
  27. prefixCls = _props$prefixCls === void 0 ? DEFAULT_PREFIX : _props$prefixCls,
  28. className = props.className,
  29. listItemHeight = props.listItemHeight,
  30. components = props.components,
  31. onScroll = props.onScroll;
  32. var _ref = scroll || {},
  33. scrollX = _ref.x,
  34. scrollY = _ref.y;
  35. // Fill scrollX
  36. if (typeof scrollX !== 'number') {
  37. if (process.env.NODE_ENV !== 'production') {
  38. warning(!scrollX, '`scroll.x` in virtual table must be number.');
  39. }
  40. scrollX = 1;
  41. }
  42. // Fill scrollY
  43. if (typeof scrollY !== 'number') {
  44. scrollY = 500;
  45. if (process.env.NODE_ENV !== 'production') {
  46. warning(false, '`scroll.y` in virtual table must be number.');
  47. }
  48. }
  49. var getComponent = useEvent(function (path, defaultComponent) {
  50. return getValue(components, path) || defaultComponent;
  51. });
  52. // Memo this
  53. var onInternalScroll = useEvent(onScroll);
  54. // ========================= Context ==========================
  55. var context = React.useMemo(function () {
  56. return {
  57. sticky: sticky,
  58. scrollY: scrollY,
  59. listItemHeight: listItemHeight,
  60. getComponent: getComponent,
  61. onScroll: onInternalScroll
  62. };
  63. }, [sticky, scrollY, listItemHeight, getComponent, onInternalScroll]);
  64. // ========================== Render ==========================
  65. return /*#__PURE__*/React.createElement(StaticContext.Provider, {
  66. value: context
  67. }, /*#__PURE__*/React.createElement(Table, _extends({}, props, {
  68. className: classNames(className, "".concat(prefixCls, "-virtual")),
  69. scroll: _objectSpread(_objectSpread({}, scroll), {}, {
  70. x: scrollX
  71. }),
  72. components: _objectSpread(_objectSpread({}, components), {}, {
  73. // fix https://github.com/ant-design/ant-design/issues/48991
  74. body: data !== null && data !== void 0 && data.length ? renderBody : undefined
  75. }),
  76. columns: columns,
  77. internalHooks: INTERNAL_HOOKS,
  78. tailor: true,
  79. ref: ref
  80. })));
  81. }
  82. var RefVirtualTable = /*#__PURE__*/React.forwardRef(VirtualTable);
  83. if (process.env.NODE_ENV !== 'production') {
  84. RefVirtualTable.displayName = 'VirtualTable';
  85. }
  86. export function genVirtualTable(shouldTriggerRender) {
  87. return makeImmutable(RefVirtualTable, shouldTriggerRender);
  88. }
  89. export default genVirtualTable();