useColumns.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
  10. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  11. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  12. var _warning = require("../../vc-util/warning");
  13. var _legacyUtil = require("../utils/legacyUtil");
  14. var _constant = require("../constant");
  15. var _excluded = ["fixed"];
  16. function flatColumns(columns) {
  17. return columns.reduce(function (list, column) {
  18. var fixed = column.fixed;
  19. // Convert `fixed='true'` to `fixed='left'` instead
  20. var parsedFixed = fixed === true ? 'left' : fixed;
  21. var subColumns = column.children;
  22. if (subColumns && subColumns.length > 0) {
  23. return [].concat((0, _toConsumableArray2.default)(list), (0, _toConsumableArray2.default)(flatColumns(subColumns).map(function (subColum) {
  24. return (0, _objectSpread2.default)({
  25. fixed: parsedFixed
  26. }, subColum);
  27. })));
  28. }
  29. return [].concat((0, _toConsumableArray2.default)(list), [(0, _objectSpread2.default)((0, _objectSpread2.default)({}, column), {}, {
  30. fixed: parsedFixed
  31. })]);
  32. }, []);
  33. }
  34. function warningFixed(flattenColumns) {
  35. var allFixLeft = true;
  36. for (var i = 0; i < flattenColumns.length; i += 1) {
  37. var col = flattenColumns[i];
  38. if (allFixLeft && col.fixed !== 'left') {
  39. allFixLeft = false;
  40. } else if (!allFixLeft && col.fixed === 'left') {
  41. (0, _warning.warning)(false, "Index ".concat(i - 1, " of `columns` missing `fixed='left'` prop."));
  42. break;
  43. }
  44. }
  45. var allFixRight = true;
  46. for (var _i = flattenColumns.length - 1; _i >= 0; _i -= 1) {
  47. var _col = flattenColumns[_i];
  48. if (allFixRight && _col.fixed !== 'right') {
  49. allFixRight = false;
  50. } else if (!allFixRight && _col.fixed === 'right') {
  51. (0, _warning.warning)(false, "Index ".concat(_i + 1, " of `columns` missing `fixed='right'` prop."));
  52. break;
  53. }
  54. }
  55. }
  56. function revertForRtl(columns) {
  57. return columns.map(function (column) {
  58. var fixed = column.fixed,
  59. restProps = (0, _objectWithoutProperties2.default)(column, _excluded);
  60. // Convert `fixed='left'` to `fixed='right'` instead
  61. var parsedFixed = fixed;
  62. if (fixed === 'left') {
  63. parsedFixed = 'right';
  64. } else if (fixed === 'right') {
  65. parsedFixed = 'left';
  66. }
  67. return (0, _objectSpread2.default)({
  68. fixed: parsedFixed
  69. }, restProps);
  70. });
  71. }
  72. /**
  73. * Parse `columns` & `children` into `columns`.
  74. */
  75. function useColumns(_ref, transformColumns) {
  76. var prefixCls = _ref.prefixCls,
  77. baseColumns = _ref.columns,
  78. expandable = _ref.expandable,
  79. expandedKeys = _ref.expandedKeys,
  80. getRowKey = _ref.getRowKey,
  81. onTriggerExpand = _ref.onTriggerExpand,
  82. expandIcon = _ref.expandIcon,
  83. rowExpandable = _ref.rowExpandable,
  84. expandIconColumnIndex = _ref.expandIconColumnIndex,
  85. direction = _ref.direction,
  86. expandRowByClick = _ref.expandRowByClick,
  87. expandColumnWidth = _ref.expandColumnWidth,
  88. expandFixed = _ref.expandFixed;
  89. // Add expand column
  90. var withExpandColumns = (0, _vue.computed)(function () {
  91. if (expandable.value) {
  92. var _expandColumn;
  93. var cloneColumns = baseColumns.value.slice();
  94. // >>> Warning if use `expandIconColumnIndex`
  95. if (process.env.NODE_ENV !== 'production' && expandIconColumnIndex.value >= 0) {
  96. (0, _warning.warning)(false, '`expandIconColumnIndex` is deprecated. Please use `Table.EXPAND_COLUMN` in `columns` instead.');
  97. }
  98. // >>> Insert expand column if not exist
  99. if (!cloneColumns.includes(_constant.EXPAND_COLUMN)) {
  100. var expandColIndex = expandIconColumnIndex.value || 0;
  101. if (expandColIndex >= 0) {
  102. cloneColumns.splice(expandColIndex, 0, _constant.EXPAND_COLUMN);
  103. }
  104. }
  105. // >>> Deduplicate additional expand column
  106. if (process.env.NODE_ENV !== 'production' && cloneColumns.filter(function (c) {
  107. return c === _constant.EXPAND_COLUMN;
  108. }).length > 1) {
  109. (0, _warning.warning)(false, 'There exist more than one `EXPAND_COLUMN` in `columns`.');
  110. }
  111. var expandColumnIndex = cloneColumns.indexOf(_constant.EXPAND_COLUMN);
  112. cloneColumns = cloneColumns.filter(function (column, index) {
  113. return column !== _constant.EXPAND_COLUMN || index === expandColumnIndex;
  114. });
  115. // >>> Check if expand column need to fixed
  116. var prevColumn = baseColumns.value[expandColumnIndex];
  117. var fixedColumn;
  118. if ((expandFixed.value === 'left' || expandFixed.value) && !expandIconColumnIndex.value) {
  119. fixedColumn = 'left';
  120. } else if ((expandFixed.value === 'right' || expandFixed.value) && expandIconColumnIndex.value === baseColumns.value.length) {
  121. fixedColumn = 'right';
  122. } else {
  123. fixedColumn = prevColumn ? prevColumn.fixed : null;
  124. }
  125. var expandedKeysValue = expandedKeys.value;
  126. var rowExpandableValue = rowExpandable.value;
  127. var expandIconValue = expandIcon.value;
  128. var prefixClsValue = prefixCls.value;
  129. var expandRowByClickValue = expandRowByClick.value;
  130. // >>> Create expandable column
  131. var expandColumn = (_expandColumn = {}, (0, _defineProperty2.default)(_expandColumn, _legacyUtil.INTERNAL_COL_DEFINE, {
  132. class: "".concat(prefixCls.value, "-expand-icon-col"),
  133. columnType: 'EXPAND_COLUMN'
  134. }), (0, _defineProperty2.default)(_expandColumn, "title", ''), (0, _defineProperty2.default)(_expandColumn, "fixed", fixedColumn), (0, _defineProperty2.default)(_expandColumn, "class", "".concat(prefixCls.value, "-row-expand-icon-cell")), (0, _defineProperty2.default)(_expandColumn, "width", expandColumnWidth.value), (0, _defineProperty2.default)(_expandColumn, "customRender", function customRender(_ref2) {
  135. var record = _ref2.record,
  136. index = _ref2.index;
  137. var rowKey = getRowKey.value(record, index);
  138. var expanded = expandedKeysValue.has(rowKey);
  139. var recordExpandable = rowExpandableValue ? rowExpandableValue(record) : true;
  140. var icon = expandIconValue({
  141. prefixCls: prefixClsValue,
  142. expanded: expanded,
  143. expandable: recordExpandable,
  144. record: record,
  145. onExpand: onTriggerExpand
  146. });
  147. if (expandRowByClickValue) {
  148. return (0, _vue.createVNode)("span", {
  149. "onClick": function onClick(e) {
  150. return e.stopPropagation();
  151. }
  152. }, [icon]);
  153. }
  154. return icon;
  155. }), _expandColumn);
  156. return cloneColumns.map(function (col) {
  157. return col === _constant.EXPAND_COLUMN ? expandColumn : col;
  158. });
  159. }
  160. if (process.env.NODE_ENV !== 'production' && baseColumns.value.includes(_constant.EXPAND_COLUMN)) {
  161. (0, _warning.warning)(false, '`expandable` is not config but there exist `EXPAND_COLUMN` in `columns`.');
  162. }
  163. return baseColumns.value.filter(function (col) {
  164. return col !== _constant.EXPAND_COLUMN;
  165. });
  166. });
  167. var mergedColumns = (0, _vue.computed)(function () {
  168. var finalColumns = withExpandColumns.value;
  169. if (transformColumns.value) {
  170. finalColumns = transformColumns.value(finalColumns);
  171. }
  172. // Always provides at least one column for table display
  173. if (!finalColumns.length) {
  174. finalColumns = [{
  175. customRender: function customRender() {
  176. return null;
  177. }
  178. }];
  179. }
  180. return finalColumns;
  181. });
  182. var flattenColumns = (0, _vue.computed)(function () {
  183. if (direction.value === 'rtl') {
  184. return revertForRtl(flatColumns(mergedColumns.value));
  185. }
  186. return flatColumns(mergedColumns.value);
  187. });
  188. // Only check out of production since it's waste for each render
  189. if (process.env.NODE_ENV !== 'production') {
  190. (0, _vue.watchEffect)(function () {
  191. setTimeout(function () {
  192. warningFixed(flattenColumns.value);
  193. });
  194. });
  195. }
  196. return [mergedColumns, flattenColumns];
  197. }
  198. var _default = useColumns;
  199. exports.default = _default;