@procore/core-react
Version:
React library of Procore Design Guidelines
68 lines (67 loc) • 3.04 kB
JavaScript
var _excluded = ["boxProps", "children", "direction", "size", "space"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
import React from 'react';
import { Box } from '../Box/Box';
import { StyledFlex } from './FlexList.styles';
function getSingleSize() {
var size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
return size.split(' ')[0];
}
function getItemMargin(direction, size) {
return direction.includes('row') ? "0 ".concat(size) : "".concat(size, " 0");
}
function getMargin(direction, space) {
var size = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'md';
if (space === undefined) {
return {
margin: getItemMargin(direction, size)
};
}
if (space && direction.includes('row')) {
return {
marginRight: space
};
}
if (space && direction.includes('column')) {
return {
marginBottom: space
};
}
return {};
}
/**
* `FlexList` combines `Flex` and `Box`, **each child element is wrapped in a `Box`**
* having a margin (leading and trailing space removed). This isolates the
* children React components from the `display: flex` on direct children in CSS.
*/
export var FlexList = /*#__PURE__*/React.forwardRef(function FlexList(_ref, ref) {
var boxProps = _ref.boxProps,
children = _ref.children,
_ref$direction = _ref.direction,
direction = _ref$direction === void 0 ? 'row' : _ref$direction,
_ref$size = _ref.size,
size = _ref$size === void 0 ? 'md' : _ref$size,
space = _ref.space,
props = _objectWithoutProperties(_ref, _excluded);
var singleSize = React.useMemo(function () {
return getSingleSize(size);
}, [size]);
var margin = React.useMemo(function () {
return getMargin(direction, space, singleSize);
}, [direction, space, singleSize]);
return /*#__PURE__*/React.createElement(StyledFlex, _extends({}, props, {
ref: ref,
direction: direction,
$direction: direction
}), React.Children.toArray(children).filter(function (child) {
return child;
}).map(function (child, i) {
return child && /*#__PURE__*/React.createElement(Box, _extends({}, boxProps, margin, {
key: child.key || i
}), child);
}));
});
FlexList.displayName = 'FlexList';
//# sourceMappingURL=FlexList.js.map