spaceFlex.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.SpaceFlex = void 0;
  15. const utils_1 = require("./utils");
  16. /**
  17. * @todo Propagate more options to children.
  18. */
  19. const SpaceFlex = () => {
  20. return (options) => {
  21. const { children } = options;
  22. if (!Array.isArray(children))
  23. return [];
  24. const { direction = 'row', ratio = children.map(() => 1), padding = 0, data: flexData, } = options;
  25. const [mainStart, mainSize, crossSize, crossStart] = direction === 'col'
  26. ? ['y', 'height', 'width', 'x']
  27. : ['x', 'width', 'height', 'y'];
  28. const sum = ratio.reduce((total, value) => total + value);
  29. const totalSize = options[mainSize] - padding * (children.length - 1);
  30. const sizes = ratio.map((value) => totalSize * (value / sum));
  31. const newChildren = [];
  32. let next = options[mainStart] || 0;
  33. for (let i = 0; i < sizes.length; i += 1) {
  34. const _a = children[i], { data } = _a, rest = __rest(_a, ["data"]);
  35. const newData = (0, utils_1.mergeData)(data, flexData);
  36. newChildren.push(Object.assign({ [mainStart]: next, [mainSize]: sizes[i], [crossStart]: options[crossStart] || 0, [crossSize]: options[crossSize], data: newData }, rest));
  37. next += sizes[i] + padding;
  38. }
  39. return newChildren;
  40. };
  41. };
  42. exports.SpaceFlex = SpaceFlex;
  43. exports.SpaceFlex.props = {};
  44. //# sourceMappingURL=spaceFlex.js.map