util.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.convertChildrenToColumns = convertChildrenToColumns;
  7. exports.getColumnKey = getColumnKey;
  8. exports.getColumnPos = getColumnPos;
  9. exports.renderColumnTitle = renderColumnTitle;
  10. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  11. var _propsUtil = require("../_util/props-util");
  12. var __rest = void 0 && (void 0).__rest || function (s, e) {
  13. var t = {};
  14. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  15. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  16. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  17. }
  18. return t;
  19. };
  20. function getColumnKey(column, defaultKey) {
  21. if ('key' in column && column.key !== undefined && column.key !== null) {
  22. return column.key;
  23. }
  24. if (column.dataIndex) {
  25. return Array.isArray(column.dataIndex) ? column.dataIndex.join('.') : column.dataIndex;
  26. }
  27. return defaultKey;
  28. }
  29. function getColumnPos(index, pos) {
  30. return pos ? `${pos}-${index}` : `${index}`;
  31. }
  32. function renderColumnTitle(title, props) {
  33. if (typeof title === 'function') {
  34. return title(props);
  35. }
  36. return title;
  37. }
  38. function convertChildrenToColumns() {
  39. let elements = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  40. const flattenElements = (0, _propsUtil.flattenChildren)(elements);
  41. const columns = [];
  42. flattenElements.forEach(element => {
  43. var _a, _b, _c, _d;
  44. if (!element) {
  45. return;
  46. }
  47. const key = element.key;
  48. const style = ((_a = element.props) === null || _a === void 0 ? void 0 : _a.style) || {};
  49. const cls = ((_b = element.props) === null || _b === void 0 ? void 0 : _b.class) || '';
  50. const props = element.props || {};
  51. for (const [k, v] of Object.entries(props)) {
  52. props[(0, _propsUtil.camelize)(k)] = v;
  53. }
  54. const _e = element.children || {},
  55. {
  56. default: children
  57. } = _e,
  58. restSlots = __rest(_e, ["default"]);
  59. const column = (0, _extends2.default)((0, _extends2.default)((0, _extends2.default)({}, restSlots), props), {
  60. style,
  61. class: cls
  62. });
  63. if (key) {
  64. column.key = key;
  65. }
  66. if ((_c = element.type) === null || _c === void 0 ? void 0 : _c.__ANT_TABLE_COLUMN_GROUP) {
  67. column.children = convertChildrenToColumns(typeof children === 'function' ? children() : children);
  68. } else {
  69. const customRender = (_d = element.children) === null || _d === void 0 ? void 0 : _d.default;
  70. column.customRender = column.customRender || customRender;
  71. }
  72. columns.push(column);
  73. });
  74. return columns;
  75. }