UNPKG

@vtex/styleguide

Version:

> VTEX Styleguide React components ([Docs](https://vtex.github.io/styleguide))

45 lines (38 loc) 1.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useTableProportion; var _react = require("react"); function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function useTableProportion(_ref) { var columns = _ref.columns, ratio = _ref.ratio; var calculatedWidths = (0, _react.useMemo)(function () { return calculateWidths(columns, ratio); }, [columns, ratio]); var sizedColumns = (0, _react.useMemo)(function () { return columns.map(function (col, i) { return _extends({}, col, { width: calculatedWidths[i] }); }); }, [calculatedWidths, columns]); return (0, _react.useMemo)(function () { return { sizedColumns: sizedColumns }; }, [sizedColumns]); } function calculateWidths(columns, ratio) { var slicedRatio = ratio.slice(0, columns.length); var sum = function sum(acc, num) { return acc + num; }; var ratioSum = slicedRatio.reduce(sum, 0); var fullPortion = 100; var minPortion = fullPortion / ratioSum; return slicedRatio.map(function (value) { return value * minPortion + "%"; }); }