util.js 2.4 KB

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