UNPKG

3.82 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 = useCellRender;
9var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
11var _useMemo = _interopRequireDefault(require("rc-util/lib/hooks/useMemo"));
12var _isEqual = _interopRequireDefault(require("rc-util/lib/isEqual"));
13var _get = _interopRequireDefault(require("rc-util/lib/utils/get"));
14var _warning = _interopRequireDefault(require("rc-util/lib/warning"));
15var React = _interopRequireWildcard(require("react"));
16var _PerfContext = _interopRequireDefault(require("../context/PerfContext"));
17var _valueUtil = require("../utils/valueUtil");
18var _TableContext = require("../context/TableContext");
19function _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); }
20function _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; }
21function isRenderCell(data) {
22 return data && (0, _typeof2.default)(data) === 'object' && !Array.isArray(data) && ! /*#__PURE__*/React.isValidElement(data);
23}
24function useCellRender(record, dataIndex, renderIndex, children, render, shouldCellUpdate) {
25 // TODO: Remove this after next major version
26 var perfRecord = React.useContext(_PerfContext.default);
27 var mark = (0, _TableContext.useImmutableMark)();
28
29 // ======================== Render ========================
30 var retData = (0, _useMemo.default)(function () {
31 if ((0, _valueUtil.validateValue)(children)) {
32 return [children];
33 }
34 var path = dataIndex === null || dataIndex === undefined || dataIndex === '' ? [] : Array.isArray(dataIndex) ? dataIndex : [dataIndex];
35 var value = (0, _get.default)(record, path);
36
37 // Customize render node
38 var returnChildNode = value;
39 var returnCellProps = undefined;
40 if (render) {
41 var renderData = render(value, record, renderIndex);
42 if (isRenderCell(renderData)) {
43 if (process.env.NODE_ENV !== 'production') {
44 (0, _warning.default)(false, '`columns.render` return cell props is deprecated with perf issue, please use `onCell` instead.');
45 }
46 returnChildNode = renderData.children;
47 returnCellProps = renderData.props;
48 perfRecord.renderWithProps = true;
49 } else {
50 returnChildNode = renderData;
51 }
52 }
53 return [returnChildNode, returnCellProps];
54 }, [
55 // Force update deps
56 mark,
57 // Normal deps
58 record, children, dataIndex, render, renderIndex], function (prev, next) {
59 if (shouldCellUpdate) {
60 var _prev = (0, _slicedToArray2.default)(prev, 2),
61 prevRecord = _prev[1];
62 var _next = (0, _slicedToArray2.default)(next, 2),
63 nextRecord = _next[1];
64 return shouldCellUpdate(nextRecord, prevRecord);
65 }
66
67 // Legacy mode should always update
68 if (perfRecord.renderWithProps) {
69 return true;
70 }
71 return !(0, _isEqual.default)(prev, next, true);
72 });
73 return retData;
74}
\No newline at end of file