UNPKG

10.1 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4var _typeof3 = require("@babel/runtime/helpers/typeof");
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
10var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11var _context = require("@rc-component/context");
12var _rcVirtualList = _interopRequireDefault(require("rc-virtual-list"));
13var React = _interopRequireWildcard(require("react"));
14var _TableContext = _interopRequireWildcard(require("../context/TableContext"));
15var _useFlattenRecords = _interopRequireDefault(require("../hooks/useFlattenRecords"));
16var _BodyLine = _interopRequireDefault(require("./BodyLine"));
17var _context2 = require("./context");
18function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
19function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof3(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
20var Grid = /*#__PURE__*/React.forwardRef(function (props, ref) {
21 var data = props.data,
22 onScroll = props.onScroll;
23 var _useContext = (0, _context.useContext)(_TableContext.default, ['flattenColumns', 'onColumnResize', 'getRowKey', 'prefixCls', 'expandedKeys', 'childrenColumnName', 'scrollX']),
24 flattenColumns = _useContext.flattenColumns,
25 onColumnResize = _useContext.onColumnResize,
26 getRowKey = _useContext.getRowKey,
27 expandedKeys = _useContext.expandedKeys,
28 prefixCls = _useContext.prefixCls,
29 childrenColumnName = _useContext.childrenColumnName,
30 scrollX = _useContext.scrollX;
31 var _useContext2 = (0, _context.useContext)(_context2.StaticContext),
32 sticky = _useContext2.sticky,
33 scrollY = _useContext2.scrollY,
34 listItemHeight = _useContext2.listItemHeight,
35 getComponent = _useContext2.getComponent,
36 onTablePropScroll = _useContext2.onScroll;
37
38 // =========================== Ref ============================
39 var listRef = React.useRef();
40
41 // =========================== Data ===========================
42 var flattenData = (0, _useFlattenRecords.default)(data, childrenColumnName, expandedKeys, getRowKey);
43
44 // ========================== Column ==========================
45 var columnsWidth = React.useMemo(function () {
46 var total = 0;
47 return flattenColumns.map(function (_ref) {
48 var width = _ref.width,
49 key = _ref.key;
50 total += width;
51 return [key, width, total];
52 });
53 }, [flattenColumns]);
54 var columnsOffset = React.useMemo(function () {
55 return columnsWidth.map(function (colWidth) {
56 return colWidth[2];
57 });
58 }, [columnsWidth]);
59 React.useEffect(function () {
60 columnsWidth.forEach(function (_ref2) {
61 var _ref3 = (0, _slicedToArray2.default)(_ref2, 2),
62 key = _ref3[0],
63 width = _ref3[1];
64 onColumnResize(key, width);
65 });
66 }, [columnsWidth]);
67
68 // =========================== Ref ============================
69 React.useImperativeHandle(ref, function () {
70 var _listRef$current2;
71 var obj = {
72 scrollTo: function scrollTo(config) {
73 var _listRef$current;
74 (_listRef$current = listRef.current) === null || _listRef$current === void 0 || _listRef$current.scrollTo(config);
75 },
76 nativeElement: (_listRef$current2 = listRef.current) === null || _listRef$current2 === void 0 ? void 0 : _listRef$current2.nativeElement
77 };
78 Object.defineProperty(obj, 'scrollLeft', {
79 get: function get() {
80 var _listRef$current3;
81 return ((_listRef$current3 = listRef.current) === null || _listRef$current3 === void 0 ? void 0 : _listRef$current3.getScrollInfo().x) || 0;
82 },
83 set: function set(value) {
84 var _listRef$current4;
85 (_listRef$current4 = listRef.current) === null || _listRef$current4 === void 0 || _listRef$current4.scrollTo({
86 left: value
87 });
88 }
89 });
90 return obj;
91 });
92
93 // ======================= Col/Row Span =======================
94 var getRowSpan = function getRowSpan(column, index) {
95 var _flattenData$index;
96 var record = (_flattenData$index = flattenData[index]) === null || _flattenData$index === void 0 ? void 0 : _flattenData$index.record;
97 var onCell = column.onCell;
98 if (onCell) {
99 var _cellProps$rowSpan;
100 var cellProps = onCell(record, index);
101 return (_cellProps$rowSpan = cellProps === null || cellProps === void 0 ? void 0 : cellProps.rowSpan) !== null && _cellProps$rowSpan !== void 0 ? _cellProps$rowSpan : 1;
102 }
103 return 1;
104 };
105 var extraRender = function extraRender(info) {
106 var start = info.start,
107 end = info.end,
108 getSize = info.getSize,
109 offsetY = info.offsetY;
110
111 // Do nothing if no data
112 if (end < 0) {
113 return null;
114 }
115
116 // Find first rowSpan column
117 var firstRowSpanColumns = flattenColumns.filter(
118 // rowSpan is 0
119 function (column) {
120 return getRowSpan(column, start) === 0;
121 });
122 var startIndex = start;
123 var _loop = function _loop(i) {
124 firstRowSpanColumns = firstRowSpanColumns.filter(function (column) {
125 return getRowSpan(column, i) === 0;
126 });
127 if (!firstRowSpanColumns.length) {
128 startIndex = i;
129 return 1; // break
130 }
131 };
132 for (var i = start; i >= 0; i -= 1) {
133 if (_loop(i)) break;
134 }
135
136 // Find last rowSpan column
137 var lastRowSpanColumns = flattenColumns.filter(
138 // rowSpan is not 1
139 function (column) {
140 return getRowSpan(column, end) !== 1;
141 });
142 var endIndex = end;
143 var _loop2 = function _loop2(_i) {
144 lastRowSpanColumns = lastRowSpanColumns.filter(function (column) {
145 return getRowSpan(column, _i) !== 1;
146 });
147 if (!lastRowSpanColumns.length) {
148 endIndex = Math.max(_i - 1, end);
149 return 1; // break
150 }
151 };
152 for (var _i = end; _i < flattenData.length; _i += 1) {
153 if (_loop2(_i)) break;
154 }
155
156 // Collect the line who has rowSpan
157 var spanLines = [];
158 var _loop3 = function _loop3(_i2) {
159 var item = flattenData[_i2];
160
161 // This code will never reach, just incase
162 if (!item) {
163 return 1; // continue
164 }
165 if (flattenColumns.some(function (column) {
166 return getRowSpan(column, _i2) > 1;
167 })) {
168 spanLines.push(_i2);
169 }
170 };
171 for (var _i2 = startIndex; _i2 <= endIndex; _i2 += 1) {
172 if (_loop3(_i2)) continue;
173 }
174
175 // Patch extra line on the page
176 var nodes = spanLines.map(function (index) {
177 var item = flattenData[index];
178 var rowKey = getRowKey(item.record, index);
179 var getHeight = function getHeight(rowSpan) {
180 var endItemIndex = index + rowSpan - 1;
181 var endItemKey = getRowKey(flattenData[endItemIndex].record, endItemIndex);
182 var sizeInfo = getSize(rowKey, endItemKey);
183 return sizeInfo.bottom - sizeInfo.top;
184 };
185 var sizeInfo = getSize(rowKey);
186 return /*#__PURE__*/React.createElement(_BodyLine.default, {
187 key: index,
188 data: item,
189 rowKey: rowKey,
190 index: index,
191 style: {
192 top: -offsetY + sizeInfo.top
193 },
194 extra: true,
195 getHeight: getHeight
196 });
197 });
198 return nodes;
199 };
200
201 // ========================= Context ==========================
202 var gridContext = React.useMemo(function () {
203 return {
204 columnsOffset: columnsOffset
205 };
206 }, [columnsOffset]);
207
208 // ========================== Render ==========================
209 var tblPrefixCls = "".concat(prefixCls, "-tbody");
210
211 // default 'div' in rc-virtual-list
212 var wrapperComponent = getComponent(['body', 'wrapper']);
213
214 // ========================== Sticky Scroll Bar ==========================
215 var horizontalScrollBarStyle = {};
216 if (sticky) {
217 horizontalScrollBarStyle.position = 'sticky';
218 horizontalScrollBarStyle.bottom = 0;
219 if ((0, _typeof2.default)(sticky) === 'object' && sticky.offsetScroll) {
220 horizontalScrollBarStyle.bottom = sticky.offsetScroll;
221 }
222 }
223 return /*#__PURE__*/React.createElement(_context2.GridContext.Provider, {
224 value: gridContext
225 }, /*#__PURE__*/React.createElement(_rcVirtualList.default, {
226 fullHeight: false,
227 ref: listRef,
228 prefixCls: "".concat(tblPrefixCls, "-virtual"),
229 styles: {
230 horizontalScrollBar: horizontalScrollBarStyle
231 },
232 className: tblPrefixCls,
233 height: scrollY,
234 itemHeight: listItemHeight || 24,
235 data: flattenData,
236 itemKey: function itemKey(item) {
237 return getRowKey(item.record);
238 },
239 component: wrapperComponent,
240 scrollWidth: scrollX,
241 onVirtualScroll: function onVirtualScroll(_ref4) {
242 var _listRef$current5;
243 var x = _ref4.x;
244 onScroll({
245 currentTarget: (_listRef$current5 = listRef.current) === null || _listRef$current5 === void 0 ? void 0 : _listRef$current5.nativeElement,
246 scrollLeft: x
247 });
248 },
249 onScroll: onTablePropScroll,
250 extraRender: extraRender
251 }, function (item, index, itemProps) {
252 var rowKey = getRowKey(item.record, index);
253 return /*#__PURE__*/React.createElement(_BodyLine.default, {
254 data: item,
255 rowKey: rowKey,
256 index: index,
257 style: itemProps.style
258 });
259 }));
260});
261var ResponseGrid = (0, _TableContext.responseImmutable)(Grid);
262if (process.env.NODE_ENV !== 'production') {
263 ResponseGrid.displayName = 'ResponseGrid';
264}
265var _default = exports.default = ResponseGrid;
\No newline at end of file