useWidthColumns.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _typeof = require("@babel/runtime/helpers/typeof");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = useWidthColumns;
  8. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  9. var React = _interopRequireWildcard(require("react"));
  10. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
  11. function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
  12. function parseColWidth(totalWidth) {
  13. var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  14. if (typeof width === 'number') {
  15. return width;
  16. }
  17. if (width.endsWith('%')) {
  18. return totalWidth * parseFloat(width) / 100;
  19. }
  20. return null;
  21. }
  22. /**
  23. * Fill all column with width
  24. */
  25. function useWidthColumns(flattenColumns, scrollWidth, clientWidth) {
  26. return React.useMemo(function () {
  27. // Fill width if needed
  28. if (scrollWidth && scrollWidth > 0) {
  29. var totalWidth = 0;
  30. var missWidthCount = 0;
  31. // collect not given width column
  32. flattenColumns.forEach(function (col) {
  33. var colWidth = parseColWidth(scrollWidth, col.width);
  34. if (colWidth) {
  35. totalWidth += colWidth;
  36. } else {
  37. missWidthCount += 1;
  38. }
  39. });
  40. // Fill width
  41. var maxFitWidth = Math.max(scrollWidth, clientWidth);
  42. var restWidth = Math.max(maxFitWidth - totalWidth, missWidthCount);
  43. var restCount = missWidthCount;
  44. var avgWidth = restWidth / missWidthCount;
  45. var realTotal = 0;
  46. var filledColumns = flattenColumns.map(function (col) {
  47. var clone = (0, _objectSpread2.default)({}, col);
  48. var colWidth = parseColWidth(scrollWidth, clone.width);
  49. if (colWidth) {
  50. clone.width = colWidth;
  51. } else {
  52. var colAvgWidth = Math.floor(avgWidth);
  53. clone.width = restCount === 1 ? restWidth : colAvgWidth;
  54. restWidth -= colAvgWidth;
  55. restCount -= 1;
  56. }
  57. realTotal += clone.width;
  58. return clone;
  59. });
  60. // If realTotal is less than clientWidth,
  61. // We need extend column width
  62. if (realTotal < maxFitWidth) {
  63. var scale = maxFitWidth / realTotal;
  64. restWidth = maxFitWidth;
  65. filledColumns.forEach(function (col, index) {
  66. var colWidth = Math.floor(col.width * scale);
  67. col.width = index === filledColumns.length - 1 ? restWidth : colWidth;
  68. restWidth -= colWidth;
  69. });
  70. }
  71. return [filledColumns, Math.max(realTotal, maxFitWidth)];
  72. }
  73. return [flattenColumns, scrollWidth];
  74. }, [flattenColumns, scrollWidth, clientWidth]);
  75. }