"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useWidthColumns; var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var React = _interopRequireWildcard(require("react")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function parseColWidth(totalWidth) { var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; if (typeof width === 'number') { return width; } if (width.endsWith('%')) { return totalWidth * parseFloat(width) / 100; } return null; } /** * Fill all column with width */ function useWidthColumns(flattenColumns, scrollWidth, clientWidth) { return React.useMemo(function () { // Fill width if needed if (scrollWidth && scrollWidth > 0) { var totalWidth = 0; var missWidthCount = 0; // collect not given width column flattenColumns.forEach(function (col) { var colWidth = parseColWidth(scrollWidth, col.width); if (colWidth) { totalWidth += colWidth; } else { missWidthCount += 1; } }); // Fill width var restWidth = Math.max(scrollWidth - totalWidth, missWidthCount); var restCount = missWidthCount; var avgWidth = restWidth / missWidthCount; var realTotal = 0; var filledColumns = flattenColumns.map(function (col) { var clone = (0, _objectSpread2.default)({}, col); var colWidth = parseColWidth(scrollWidth, clone.width); if (colWidth) { clone.width = colWidth; } else { var colAvgWidth = Math.floor(avgWidth); clone.width = restCount === 1 ? restWidth : colAvgWidth; restWidth -= colAvgWidth; restCount -= 1; } realTotal += clone.width; return clone; }); var maxFitWidth = Math.max(scrollWidth, clientWidth); // If realTotal is less than clientWidth, // We need extend column width if (realTotal < maxFitWidth) { var scale = maxFitWidth / realTotal; restWidth = maxFitWidth; filledColumns.forEach(function (col, index) { var colWidth = Math.floor(col.width * scale); col.width = index === filledColumns.length - 1 ? restWidth : colWidth; restWidth -= colWidth; }); } return [filledColumns, Math.max(realTotal, maxFitWidth)]; } return [flattenColumns, scrollWidth]; }, [flattenColumns, scrollWidth, clientWidth]); }