index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _vue = require("vue");
  8. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  9. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  10. var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
  11. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  12. var _classNames2 = _interopRequireDefault(require("../../_util/classNames"));
  13. var _propsUtil = require("../../_util/props-util");
  14. var _valueUtil = require("../utils/valueUtil");
  15. var _context = require("../../table/context");
  16. var _legacyUtil = require("../utils/legacyUtil");
  17. var _HoverContext = require("../context/HoverContext");
  18. var _StickyContext = require("../context/StickyContext");
  19. var _warning = require("../../vc-util/warning");
  20. var _eagerComputed = _interopRequireDefault(require("../../_util/eagerComputed"));
  21. var _excluded = ["colSpan", "rowSpan", "style", "class"];
  22. /** Check if cell is in hover range */
  23. function inHoverRange(cellStartRow, cellRowSpan, startRow, endRow) {
  24. var cellEndRow = cellStartRow + cellRowSpan - 1;
  25. return cellStartRow <= endRow && cellEndRow >= startRow;
  26. }
  27. function isRenderCell(data) {
  28. return data && (0, _typeof2.default)(data) === 'object' && !Array.isArray(data) && !(0, _vue.isVNode)(data);
  29. }
  30. var _default2 = (0, _vue.defineComponent)({
  31. name: 'Cell',
  32. props: ['prefixCls', 'record', 'index', 'renderIndex', 'dataIndex', 'customRender', 'component', 'colSpan', 'rowSpan', 'fixLeft', 'fixRight', 'firstFixLeft', 'lastFixLeft', 'firstFixRight', 'lastFixRight', 'appendNode', 'additionalProps', 'ellipsis', 'align', 'rowType', 'isSticky', 'column', 'cellType', 'transformCellText'],
  33. slots: ['appendNode'],
  34. setup: function setup(props, _ref) {
  35. var slots = _ref.slots;
  36. var contextSlots = (0, _context.useInjectSlots)();
  37. var _useInjectHover = (0, _HoverContext.useInjectHover)(),
  38. onHover = _useInjectHover.onHover,
  39. startRow = _useInjectHover.startRow,
  40. endRow = _useInjectHover.endRow;
  41. var colSpan = (0, _vue.computed)(function () {
  42. var _ref2, _props$colSpan, _props$additionalProp, _props$additionalProp2;
  43. return (_ref2 = (_props$colSpan = props.colSpan) !== null && _props$colSpan !== void 0 ? _props$colSpan : (_props$additionalProp = props.additionalProps) === null || _props$additionalProp === void 0 ? void 0 : _props$additionalProp.colSpan) !== null && _ref2 !== void 0 ? _ref2 : (_props$additionalProp2 = props.additionalProps) === null || _props$additionalProp2 === void 0 ? void 0 : _props$additionalProp2.colspan;
  44. });
  45. var rowSpan = (0, _vue.computed)(function () {
  46. var _ref3, _props$rowSpan, _props$additionalProp3, _props$additionalProp4;
  47. return (_ref3 = (_props$rowSpan = props.rowSpan) !== null && _props$rowSpan !== void 0 ? _props$rowSpan : (_props$additionalProp3 = props.additionalProps) === null || _props$additionalProp3 === void 0 ? void 0 : _props$additionalProp3.rowSpan) !== null && _ref3 !== void 0 ? _ref3 : (_props$additionalProp4 = props.additionalProps) === null || _props$additionalProp4 === void 0 ? void 0 : _props$additionalProp4.rowspan;
  48. });
  49. var hovering = (0, _eagerComputed.default)(function () {
  50. var index = props.index;
  51. return inHoverRange(index, rowSpan.value || 1, startRow.value, endRow.value);
  52. });
  53. var supportSticky = (0, _StickyContext.useInjectSticky)();
  54. // ====================== Hover =======================
  55. var _onMouseenter = function onMouseenter(event, mergedRowSpan) {
  56. var _additionalProps$onMo;
  57. var record = props.record,
  58. index = props.index,
  59. additionalProps = props.additionalProps;
  60. if (record) {
  61. onHover(index, index + mergedRowSpan - 1);
  62. }
  63. additionalProps === null || additionalProps === void 0 ? void 0 : (_additionalProps$onMo = additionalProps.onMouseenter) === null || _additionalProps$onMo === void 0 ? void 0 : _additionalProps$onMo.call(additionalProps, event);
  64. };
  65. var onMouseleave = function onMouseleave(event) {
  66. var _additionalProps$onMo2;
  67. var record = props.record,
  68. additionalProps = props.additionalProps;
  69. if (record) {
  70. onHover(-1, -1);
  71. }
  72. additionalProps === null || additionalProps === void 0 ? void 0 : (_additionalProps$onMo2 = additionalProps.onMouseleave) === null || _additionalProps$onMo2 === void 0 ? void 0 : _additionalProps$onMo2.call(additionalProps, event);
  73. };
  74. var getTitle = function getTitle(vnodes) {
  75. var vnode = (0, _propsUtil.filterEmpty)(vnodes)[0];
  76. if ((0, _vue.isVNode)(vnode)) {
  77. if (vnode.type === _vue.Text) {
  78. return vnode.children;
  79. } else {
  80. return Array.isArray(vnode.children) ? getTitle(vnode.children) : undefined;
  81. }
  82. } else {
  83. return vnode;
  84. }
  85. };
  86. return function () {
  87. var _slots$appendNode, _slots$default, _ref5, _ref6, _classNames, _slots$dragHandle;
  88. var prefixCls = props.prefixCls,
  89. record = props.record,
  90. index = props.index,
  91. renderIndex = props.renderIndex,
  92. dataIndex = props.dataIndex,
  93. customRender = props.customRender,
  94. _props$component = props.component,
  95. Component = _props$component === void 0 ? 'td' : _props$component,
  96. fixLeft = props.fixLeft,
  97. fixRight = props.fixRight,
  98. firstFixLeft = props.firstFixLeft,
  99. lastFixLeft = props.lastFixLeft,
  100. firstFixRight = props.firstFixRight,
  101. lastFixRight = props.lastFixRight,
  102. _props$appendNode = props.appendNode,
  103. appendNode = _props$appendNode === void 0 ? (_slots$appendNode = slots.appendNode) === null || _slots$appendNode === void 0 ? void 0 : _slots$appendNode.call(slots) : _props$appendNode,
  104. _props$additionalProp5 = props.additionalProps,
  105. additionalProps = _props$additionalProp5 === void 0 ? {} : _props$additionalProp5,
  106. ellipsis = props.ellipsis,
  107. align = props.align,
  108. rowType = props.rowType,
  109. isSticky = props.isSticky,
  110. _props$column = props.column,
  111. column = _props$column === void 0 ? {} : _props$column,
  112. cellType = props.cellType;
  113. var cellPrefixCls = "".concat(prefixCls, "-cell");
  114. // ==================== Child Node ====================
  115. var cellProps;
  116. var childNode;
  117. var children = (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots);
  118. if ((0, _valueUtil.validateValue)(children) || cellType === 'header') {
  119. childNode = children;
  120. } else {
  121. var _column$slots;
  122. var value = (0, _valueUtil.getPathValue)(record, dataIndex);
  123. // Customize render node
  124. childNode = value;
  125. if (customRender) {
  126. var renderData = customRender({
  127. text: value,
  128. value: value,
  129. record: record,
  130. index: index,
  131. renderIndex: renderIndex,
  132. column: column.__originColumn__
  133. });
  134. if (isRenderCell(renderData)) {
  135. if (process.env.NODE_ENV !== 'production') {
  136. (0, _warning.warning)(false, '`columns.customRender` return cell props is deprecated with perf issue, please use `customCell` instead.');
  137. }
  138. childNode = renderData.children;
  139. cellProps = renderData.props;
  140. } else {
  141. childNode = renderData;
  142. }
  143. }
  144. if (!(_legacyUtil.INTERNAL_COL_DEFINE in column) && cellType === 'body' && contextSlots.value.bodyCell && !((_column$slots = column.slots) !== null && _column$slots !== void 0 && _column$slots.customRender)) {
  145. var child = (0, _vue.renderSlot)(contextSlots.value, 'bodyCell', {
  146. text: value,
  147. value: value,
  148. record: record,
  149. index: index,
  150. column: column.__originColumn__
  151. }, function () {
  152. var fallback = childNode === undefined ? value : childNode;
  153. return [(0, _typeof2.default)(fallback) === 'object' && (0, _propsUtil.isValidElement)(fallback) || (0, _typeof2.default)(fallback) !== 'object' ? fallback : null];
  154. });
  155. childNode = (0, _propsUtil.flattenChildren)(child);
  156. }
  157. /** maybe we should @deprecated */
  158. if (props.transformCellText) {
  159. childNode = props.transformCellText({
  160. text: childNode,
  161. record: record,
  162. index: index,
  163. column: column.__originColumn__
  164. });
  165. }
  166. }
  167. // Not crash if final `childNode` is not validate VueNode
  168. if ((0, _typeof2.default)(childNode) === 'object' && !Array.isArray(childNode) && !(0, _vue.isVNode)(childNode)) {
  169. childNode = null;
  170. }
  171. if (ellipsis && (lastFixLeft || firstFixRight)) {
  172. var _childNode = function () {
  173. return childNode;
  174. }();
  175. childNode = (0, _vue.createVNode)("span", {
  176. "class": "".concat(cellPrefixCls, "-content")
  177. }, [childNode]);
  178. }
  179. if (Array.isArray(childNode) && childNode.length === 1) {
  180. childNode = childNode[0];
  181. }
  182. var _ref4 = cellProps || {},
  183. cellColSpan = _ref4.colSpan,
  184. cellRowSpan = _ref4.rowSpan,
  185. cellStyle = _ref4.style,
  186. cellClassName = _ref4.class,
  187. restCellProps = (0, _objectWithoutProperties2.default)(_ref4, _excluded);
  188. var mergedColSpan = (_ref5 = cellColSpan !== undefined ? cellColSpan : colSpan.value) !== null && _ref5 !== void 0 ? _ref5 : 1;
  189. var mergedRowSpan = (_ref6 = cellRowSpan !== undefined ? cellRowSpan : rowSpan.value) !== null && _ref6 !== void 0 ? _ref6 : 1;
  190. if (mergedColSpan === 0 || mergedRowSpan === 0) {
  191. return null;
  192. }
  193. // ====================== Fixed =======================
  194. var fixedStyle = {};
  195. var isFixLeft = typeof fixLeft === 'number' && supportSticky.value;
  196. var isFixRight = typeof fixRight === 'number' && supportSticky.value;
  197. if (isFixLeft) {
  198. fixedStyle.position = 'sticky';
  199. fixedStyle.left = "".concat(fixLeft, "px");
  200. }
  201. if (isFixRight) {
  202. fixedStyle.position = 'sticky';
  203. fixedStyle.right = "".concat(fixRight, "px");
  204. }
  205. // ====================== Align =======================
  206. var alignStyle = {};
  207. if (align) {
  208. alignStyle.textAlign = align;
  209. }
  210. // ====================== Render ======================
  211. var title;
  212. var ellipsisConfig = ellipsis === true ? {
  213. showTitle: true
  214. } : ellipsis;
  215. if (ellipsisConfig && (ellipsisConfig.showTitle || rowType === 'header')) {
  216. if (typeof childNode === 'string' || typeof childNode === 'number') {
  217. title = childNode.toString();
  218. } else if ((0, _vue.isVNode)(childNode)) {
  219. title = getTitle([childNode]);
  220. }
  221. }
  222. var componentProps = (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({
  223. title: title
  224. }, restCellProps), additionalProps), {}, {
  225. colSpan: mergedColSpan !== 1 ? mergedColSpan : null,
  226. rowSpan: mergedRowSpan !== 1 ? mergedRowSpan : null,
  227. class: (0, _classNames2.default)(cellPrefixCls, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left"), isFixLeft && supportSticky.value), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-first"), firstFixLeft && supportSticky.value), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-left-last"), lastFixLeft && supportSticky.value), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right"), isFixRight && supportSticky.value), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right-first"), firstFixRight && supportSticky.value), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-right-last"), lastFixRight && supportSticky.value), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-ellipsis"), ellipsis), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-with-append"), appendNode), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-fix-sticky"), (isFixLeft || isFixRight) && isSticky && supportSticky.value), (0, _defineProperty2.default)(_classNames, "".concat(cellPrefixCls, "-row-hover"), !cellProps && hovering.value), _classNames), additionalProps.class, cellClassName),
  228. onMouseenter: function onMouseenter(e) {
  229. _onMouseenter(e, mergedRowSpan);
  230. },
  231. onMouseleave: onMouseleave,
  232. style: (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, (0, _propsUtil.parseStyleText)(additionalProps.style)), alignStyle), fixedStyle), cellStyle)
  233. });
  234. return (0, _vue.createVNode)(Component, componentProps, {
  235. default: function _default() {
  236. return [appendNode, childNode, (_slots$dragHandle = slots.dragHandle) === null || _slots$dragHandle === void 0 ? void 0 : _slots$dragHandle.call(slots)];
  237. }
  238. });
  239. };
  240. }
  241. });
  242. exports.default = _default2;