useRow.js 2.3 KB

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