wix-style-react
Version:
95 lines (75 loc) • 3.51 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import React from 'react';
import * as TabPropTypes from '../constants/tab-prop-types';
import { getWidth } from '../utils';
var withMaxWidth = function withMaxWidth(WrappedComponent) {
var getMaxWidth = function getMaxWidth(items, containerWidth) {
var COMPACT_SIDE_MARGIN = 18;
var marginsBetweenItems = COMPACT_SIDE_MARGIN * 2 * (items.length - 1);
return (containerWidth - marginsBetweenItems) / items.length;
};
var Wrapper = /*#__PURE__*/function (_React$Component) {
_inherits(Wrapper, _React$Component);
var _super = _createSuper(Wrapper);
function Wrapper() {
var _this;
_classCallCheck(this, Wrapper);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "state", {
itemMaxWidth: undefined
});
return _this;
}
_createClass(Wrapper, [{
key: "initMaxWidth",
value: function initMaxWidth(itemsContainer) {
var _this$props = this.props,
type = _this$props.type,
items = _this$props.items;
if (!itemsContainer || type !== 'compactSide') {
return;
}
var itemMaxWidth = getMaxWidth(items, getWidth(itemsContainer));
if (this.state.itemMaxWidth !== itemMaxWidth) {
this.setState({
itemMaxWidth: itemMaxWidth
});
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
return /*#__PURE__*/React.createElement("div", {
ref: function ref(el) {
return _this2.initMaxWidth(el);
},
style: {
width: '100%'
}
}, /*#__PURE__*/React.createElement(WrappedComponent, _extends({}, this.props, {
itemMaxWidth: this.state.itemMaxWidth
})));
}
}]);
return Wrapper;
}(React.Component);
Wrapper.propTypes = {
type: TabPropTypes.type,
items: TabPropTypes.items.isRequired
};
return Wrapper;
};
export default withMaxWidth;