spaceFlex.js 1.8 KB

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