antd
Version:
An enterprise-class UI design language and React-based implementation
168 lines (157 loc) • 6.58 kB
JavaScript
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _extends from 'babel-runtime/helpers/extends';
import _typeof from 'babel-runtime/helpers/typeof';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
}return t;
};
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
var enquire = void 0;
if (typeof window !== 'undefined') {
var matchMediaPolyfill = function matchMediaPolyfill(mediaQuery) {
return {
media: mediaQuery,
matches: false,
addListener: function addListener() {},
removeListener: function removeListener() {}
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
enquire = require('enquire.js');
}
import * as React from 'react';
import { Children, cloneElement } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
var responsiveArray = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
var responsiveMap = {
xs: '(max-width: 575px)',
sm: '(min-width: 576px)',
md: '(min-width: 768px)',
lg: '(min-width: 992px)',
xl: '(min-width: 1200px)',
xxl: '(min-width: 1600px)'
};
var Row = function (_React$Component) {
_inherits(Row, _React$Component);
function Row() {
_classCallCheck(this, Row);
var _this = _possibleConstructorReturn(this, (Row.__proto__ || Object.getPrototypeOf(Row)).apply(this, arguments));
_this.state = {
screens: {}
};
return _this;
}
_createClass(Row, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
Object.keys(responsiveMap).map(function (screen) {
return enquire.register(responsiveMap[screen], {
match: function match() {
if (_typeof(_this2.props.gutter) !== 'object') {
return;
}
_this2.setState(function (prevState) {
return {
screens: _extends({}, prevState.screens, _defineProperty({}, screen, true))
};
});
},
unmatch: function unmatch() {
if (_typeof(_this2.props.gutter) !== 'object') {
return;
}
_this2.setState(function (prevState) {
return {
screens: _extends({}, prevState.screens, _defineProperty({}, screen, false))
};
});
},
// Keep a empty destory to avoid triggering unmatch when unregister
destroy: function destroy() {}
});
});
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
Object.keys(responsiveMap).map(function (screen) {
return enquire.unregister(responsiveMap[screen]);
});
}
}, {
key: 'getGutter',
value: function getGutter() {
var gutter = this.props.gutter;
if ((typeof gutter === 'undefined' ? 'undefined' : _typeof(gutter)) === 'object') {
for (var i = 0; i <= responsiveArray.length; i++) {
var breakpoint = responsiveArray[i];
if (this.state.screens[breakpoint] && gutter[breakpoint] !== undefined) {
return gutter[breakpoint];
}
}
}
return gutter;
}
}, {
key: 'render',
value: function render() {
var _classNames;
var _a = this.props,
type = _a.type,
justify = _a.justify,
align = _a.align,
className = _a.className,
style = _a.style,
children = _a.children,
_a$prefixCls = _a.prefixCls,
prefixCls = _a$prefixCls === undefined ? 'ant-row' : _a$prefixCls,
others = __rest(_a, ["type", "justify", "align", "className", "style", "children", "prefixCls"]);
var gutter = this.getGutter();
var classes = classNames((_classNames = {}, _defineProperty(_classNames, prefixCls, !type), _defineProperty(_classNames, prefixCls + '-' + type, type), _defineProperty(_classNames, prefixCls + '-' + type + '-' + justify, type && justify), _defineProperty(_classNames, prefixCls + '-' + type + '-' + align, type && align), _classNames), className);
var rowStyle = gutter > 0 ? _extends({ marginLeft: gutter / -2, marginRight: gutter / -2 }, style) : style;
var cols = Children.map(children, function (col) {
if (!col) {
return null;
}
if (col.props && gutter > 0) {
return cloneElement(col, {
style: _extends({ paddingLeft: gutter / 2, paddingRight: gutter / 2 }, col.props.style)
});
}
return col;
});
var otherProps = _extends({}, others);
delete otherProps.gutter;
return React.createElement(
'div',
_extends({}, otherProps, { className: classes, style: rowStyle }),
cols
);
}
}]);
return Row;
}(React.Component);
export default Row;
Row.defaultProps = {
gutter: 0
};
Row.propTypes = {
type: PropTypes.string,
align: PropTypes.string,
justify: PropTypes.string,
className: PropTypes.string,
children: PropTypes.node,
gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
prefixCls: PropTypes.string
};