choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
210 lines (176 loc) • 5.97 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
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";
var _responsiveMap;
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import { PureComponent } from 'react';
import isObject from 'lodash/isObject';
import { isArrayLike } from 'mobx';
import { matchMediaPolifill } from '../_util/mediaQueryListPolyfill';
import { Breakpoint } from './enum';
var enquire;
if (typeof window !== 'undefined') {
window.matchMedia = window.matchMedia || matchMediaPolifill; // eslint-disable-next-line global-require
enquire = require('enquire.js');
}
var responsiveMap = (_responsiveMap = {}, _defineProperty(_responsiveMap, Breakpoint.xxl, '(min-width: 1600px)'), _defineProperty(_responsiveMap, Breakpoint.xl, '(min-width: 1200px)'), _defineProperty(_responsiveMap, Breakpoint.lg, '(min-width: 992px)'), _defineProperty(_responsiveMap, Breakpoint.md, '(min-width: 768px)'), _defineProperty(_responsiveMap, Breakpoint.sm, '(min-width: 576px)'), _defineProperty(_responsiveMap, Breakpoint.xs, '(max-width: 575px)'), _responsiveMap);
var responsiveArray = Object.keys(responsiveMap);
var Responsive =
/*#__PURE__*/
function (_PureComponent) {
_inherits(Responsive, _PureComponent);
var _super = _createSuper(Responsive);
function Responsive() {
var _this;
_classCallCheck(this, Responsive);
_this = _super.apply(this, arguments);
_this.state = {
breakpoints: {}
};
return _this;
}
_createClass(Responsive, [{
key: "isDisabled",
value: function isDisabled(props) {
var disabled = props.disabled,
items = props.items;
if (!disabled && items) {
return !items.some(isObject);
}
return true;
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
if (!this.isDisabled(this.props)) {
this.register();
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
var onChange = this.props.onChange;
if (onChange) {
onChange(this.getValues());
}
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if (this.isDisabled(this.props) && !this.isDisabled(nextProps)) {
this.register();
}
if (!this.isDisabled(this.props) && this.isDisabled(nextProps)) {
this.unregister();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (!this.isDisabled(this.props)) {
this.unregister();
}
}
}, {
key: "register",
value: function register() {
var _this2 = this;
if (enquire) {
responsiveArray.map(function (breakpoint) {
return enquire.register(responsiveMap[breakpoint], {
match: function match() {
_this2.setState(function (prevState) {
return {
breakpoints: _objectSpread({}, prevState.breakpoints, _defineProperty({}, breakpoint, true))
};
});
},
unmatch: function unmatch() {
_this2.setState(function (prevState) {
return {
breakpoints: _objectSpread({}, prevState.breakpoints, _defineProperty({}, breakpoint, false))
};
});
},
// Keep a empty destory to avoid triggering unmatch when unregister
destroy: function destroy() {}
});
});
}
}
}, {
key: "unregister",
value: function unregister() {
Object.keys(responsiveMap).map(function (breakpoint) {
return enquire.unregister(responsiveMap[breakpoint]);
});
}
}, {
key: "processValue",
value: function processValue(value) {
var breakpoints = this.state.breakpoints;
if (isArrayLike(value)) {
return value.map(this.processValue, this);
}
if (isObject(value)) {
for (var i = 0; i < responsiveArray.length; i++) {
var breakpoint = responsiveArray[i];
if (breakpoints[breakpoint] && value[breakpoint] !== undefined) {
return value[breakpoint];
}
}
return undefined;
}
return value;
}
}, {
key: "getValues",
value: function getValues() {
var items = this.props.items;
if (items) {
return items.map(this.processValue, this);
}
return [];
}
}, {
key: "render",
value: function render() {
var children = this.props.children;
if (typeof children === 'function') {
return children(this.getValues());
}
return children;
}
}]);
return Responsive;
}(PureComponent);
export { Responsive as default };
Responsive.displayName = 'Responsive';
//# sourceMappingURL=Responsive.js.map