useRow.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = require("react");
  7. var _warning = require("../../_util/warning");
  8. var __rest = void 0 && (void 0).__rest || function (s, e) {
  9. var t = {};
  10. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  11. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  12. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  13. }
  14. return t;
  15. };
  16. // Calculate the sum of span in a row
  17. function getCalcRows(rowItems, mergedColumn) {
  18. let rows = [];
  19. let tmpRow = [];
  20. let exceed = false;
  21. let count = 0;
  22. rowItems.filter(n => n).forEach(rowItem => {
  23. const {
  24. filled
  25. } = rowItem,
  26. restItem = __rest(rowItem, ["filled"]);
  27. if (filled) {
  28. tmpRow.push(restItem);
  29. rows.push(tmpRow);
  30. // reset
  31. tmpRow = [];
  32. count = 0;
  33. return;
  34. }
  35. const restSpan = mergedColumn - count;
  36. count += rowItem.span || 1;
  37. if (count >= mergedColumn) {
  38. if (count > mergedColumn) {
  39. exceed = true;
  40. tmpRow.push(Object.assign(Object.assign({}, restItem), {
  41. span: restSpan
  42. }));
  43. } else {
  44. tmpRow.push(restItem);
  45. }
  46. rows.push(tmpRow);
  47. // reset
  48. tmpRow = [];
  49. count = 0;
  50. } else {
  51. tmpRow.push(restItem);
  52. }
  53. });
  54. if (tmpRow.length > 0) {
  55. rows.push(tmpRow);
  56. }
  57. rows = rows.map(rows => {
  58. const count = rows.reduce((acc, item) => acc + (item.span || 1), 0);
  59. if (count < mergedColumn) {
  60. // If the span of the last element in the current row is less than the column, then add its span to the remaining columns
  61. const last = rows[rows.length - 1];
  62. last.span = mergedColumn - (count - (last.span || 1));
  63. return rows;
  64. }
  65. return rows;
  66. });
  67. return [rows, exceed];
  68. }
  69. const useRow = (mergedColumn, items) => {
  70. const [rows, exceed] = (0, _react.useMemo)(() => getCalcRows(items, mergedColumn), [items, mergedColumn]);
  71. if (process.env.NODE_ENV !== 'production') {
  72. const warning = (0, _warning.devUseWarning)('Descriptions');
  73. process.env.NODE_ENV !== "production" ? warning(!exceed, 'usage', 'Sum of column `span` in a line not match `column` of Descriptions.') : void 0;
  74. }
  75. return rows;
  76. };
  77. var _default = exports.default = useRow;