util.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  3. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  4. var _excluded = ["default"];
  5. import { camelize } from 'vue';
  6. import { flattenChildren } from '../_util/props-util';
  7. export function getColumnKey(column, defaultKey) {
  8. if ('key' in column && column.key !== undefined && column.key !== null) {
  9. return column.key;
  10. }
  11. if (column.dataIndex) {
  12. return Array.isArray(column.dataIndex) ? column.dataIndex.join('.') : column.dataIndex;
  13. }
  14. return defaultKey;
  15. }
  16. export function getColumnPos(index, pos) {
  17. return pos ? "".concat(pos, "-").concat(index) : "".concat(index);
  18. }
  19. export function renderColumnTitle(title, props) {
  20. if (typeof title === 'function') {
  21. return title(props);
  22. }
  23. return title;
  24. }
  25. export function convertChildrenToColumns() {
  26. var elements = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  27. var flattenElements = flattenChildren(elements);
  28. var columns = [];
  29. flattenElements.forEach(function (element) {
  30. var _element$props, _element$props2, _element$type;
  31. if (!element) {
  32. return;
  33. }
  34. var key = element.key;
  35. var style = ((_element$props = element.props) === null || _element$props === void 0 ? void 0 : _element$props.style) || {};
  36. var cls = ((_element$props2 = element.props) === null || _element$props2 === void 0 ? void 0 : _element$props2.class) || '';
  37. var props = element.props || {};
  38. for (var _i = 0, _Object$entries = Object.entries(props); _i < _Object$entries.length; _i++) {
  39. var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
  40. k = _Object$entries$_i[0],
  41. v = _Object$entries$_i[1];
  42. props[camelize(k)] = v;
  43. }
  44. var _ref = element.children || {},
  45. children = _ref.default,
  46. restSlots = _objectWithoutProperties(_ref, _excluded);
  47. var column = _objectSpread(_objectSpread(_objectSpread({}, restSlots), props), {}, {
  48. style: style,
  49. class: cls
  50. });
  51. if (key) {
  52. column.key = key;
  53. }
  54. if ((_element$type = element.type) !== null && _element$type !== void 0 && _element$type.__ANT_TABLE_COLUMN_GROUP) {
  55. column.children = convertChildrenToColumns(typeof children === 'function' ? children() : children);
  56. } else {
  57. var _element$children;
  58. var customRender = (_element$children = element.children) === null || _element$children === void 0 ? void 0 : _element$children.default;
  59. column.customRender = column.customRender || customRender;
  60. }
  61. columns.push(column);
  62. });
  63. return columns;
  64. }