2017-11-01 18:33:41 +08:00
|
|
|
// matchMedia polyfill for
|
|
|
|
// https://github.com/WickyNilliams/enquire.js/issues/82
|
2017-11-01 19:08:16 +08:00
|
|
|
let enquire;
|
2017-11-01 18:33:41 +08:00
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => {
|
|
|
|
return {
|
|
|
|
media: mediaQuery,
|
|
|
|
matches: false,
|
|
|
|
addListener() {
|
|
|
|
},
|
|
|
|
removeListener() {
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
window.matchMedia = window.matchMedia || matchMediaPolyfill;
|
2017-11-01 19:08:16 +08:00
|
|
|
enquire = require('enquire.js');
|
2017-11-01 18:33:41 +08:00
|
|
|
}
|
|
|
|
|
2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
2016-08-19 17:11:06 +08:00
|
|
|
import { Children, cloneElement } from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2017-04-12 04:49:08 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2016-08-19 17:11:06 +08:00
|
|
|
|
|
|
|
export interface RowProps {
|
2016-08-22 17:26:14 +08:00
|
|
|
className?: string;
|
2016-08-19 17:11:06 +08:00
|
|
|
gutter?: number;
|
|
|
|
type?: 'flex';
|
|
|
|
align?: 'top' | 'middle' | 'bottom';
|
|
|
|
justify?: 'start' | 'end' | 'center' | 'space-around' | 'space-between';
|
2016-08-22 17:26:14 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2017-11-01 20:31:26 +08:00
|
|
|
const responsiveArray = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
|
|
|
|
|
2017-11-01 18:33:41 +08:00
|
|
|
const responsiveMap = {
|
|
|
|
xs: '(max-width: 575px)',
|
2017-11-01 20:31:26 +08:00
|
|
|
sm: '(min-width: 576px)',
|
|
|
|
md: '(min-width: 768px)',
|
|
|
|
lg: '(min-width: 992px)',
|
|
|
|
xl: '(min-width: 1200px)',
|
2017-11-01 18:33:41 +08:00
|
|
|
xxl: '(min-width: 1600px)',
|
|
|
|
};
|
|
|
|
|
2017-11-01 20:31:26 +08:00
|
|
|
export default class Row extends React.Component<RowProps, {
|
|
|
|
screens: {},
|
|
|
|
}> {
|
2016-03-28 23:21:47 +08:00
|
|
|
static defaultProps = {
|
|
|
|
gutter: 0,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2016-03-28 23:21:47 +08:00
|
|
|
static propTypes = {
|
2017-04-12 04:49:08 +08:00
|
|
|
type: PropTypes.string,
|
|
|
|
align: PropTypes.string,
|
|
|
|
justify: PropTypes.string,
|
|
|
|
className: PropTypes.string,
|
|
|
|
children: PropTypes.node,
|
2017-11-01 21:40:47 +08:00
|
|
|
gutter: PropTypes.oneOfType([PropTypes.object, PropTypes.number]),
|
2017-04-12 04:49:08 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2016-07-13 11:14:24 +08:00
|
|
|
};
|
2017-11-01 18:33:41 +08:00
|
|
|
state = {
|
2017-11-01 20:31:26 +08:00
|
|
|
screens: {},
|
2017-11-01 18:33:41 +08:00
|
|
|
};
|
|
|
|
componentDidMount() {
|
|
|
|
Object.keys(responsiveMap)
|
2017-11-01 20:31:26 +08:00
|
|
|
.map((screen) => enquire.register(responsiveMap[screen], {
|
|
|
|
match: () => {
|
|
|
|
if (typeof this.props.gutter !== 'object') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState((prevState) => ({
|
|
|
|
screens: {
|
|
|
|
...prevState.screens,
|
|
|
|
[screen]: true,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
unmatch: () => {
|
|
|
|
if (typeof this.props.gutter !== 'object') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState((prevState) => ({
|
|
|
|
screens: {
|
|
|
|
...prevState.screens,
|
|
|
|
[screen]: false,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
},
|
2017-11-01 18:33:41 +08:00
|
|
|
));
|
|
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
|
|
Object.keys(responsiveMap)
|
|
|
|
.map(screen => enquire.unregister(responsiveMap[screen]));
|
|
|
|
}
|
|
|
|
getGutter() {
|
|
|
|
const { gutter } = this.props;
|
|
|
|
if (typeof gutter === 'object') {
|
2017-11-01 20:31:26 +08:00
|
|
|
for (let i = 0; i <= responsiveArray.length; i++) {
|
|
|
|
if (this.state.screens[responsiveArray[i]] && gutter[responsiveArray[i]] !== undefined) {
|
|
|
|
return gutter[responsiveArray[i]];
|
|
|
|
}
|
|
|
|
}
|
2017-11-01 18:33:41 +08:00
|
|
|
}
|
|
|
|
return gutter;
|
|
|
|
}
|
2015-10-27 23:52:17 +08:00
|
|
|
render() {
|
2017-11-01 15:46:06 +08:00
|
|
|
const {
|
2017-11-01 18:33:41 +08:00
|
|
|
type, justify, align, className, style, children,
|
2017-11-01 15:46:06 +08:00
|
|
|
prefixCls = 'ant-row', ...others,
|
|
|
|
} = this.props;
|
2017-11-01 18:33:41 +08:00
|
|
|
const gutter = this.getGutter();
|
2015-11-24 20:03:57 +08:00
|
|
|
const classes = classNames({
|
2016-09-14 16:18:33 +08:00
|
|
|
[prefixCls]: !type,
|
|
|
|
[`${prefixCls}-${type}`]: type,
|
2017-02-08 09:31:09 +08:00
|
|
|
[`${prefixCls}-${type}-${justify}`]: type && justify,
|
|
|
|
[`${prefixCls}-${type}-${align}`]: type && align,
|
2016-11-30 10:20:23 +08:00
|
|
|
}, className);
|
2017-07-03 16:57:11 +08:00
|
|
|
const rowStyle = (gutter as number) > 0 ? {
|
2017-03-06 15:48:03 +08:00
|
|
|
marginLeft: (gutter as number) / -2,
|
|
|
|
marginRight: (gutter as number) / -2,
|
2017-07-03 16:57:11 +08:00
|
|
|
...style,
|
2017-10-08 22:14:31 +08:00
|
|
|
} : style;
|
|
|
|
const cols = Children.map(children, (col: React.ReactElement<HTMLDivElement>) => {
|
2016-07-13 11:14:24 +08:00
|
|
|
if (!col) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-16 23:03:06 +08:00
|
|
|
if (col.props && (gutter as number) > 0) {
|
2016-08-03 20:30:17 +08:00
|
|
|
return cloneElement(col, {
|
2017-07-03 16:57:11 +08:00
|
|
|
style: {
|
2017-03-06 15:48:03 +08:00
|
|
|
paddingLeft: (gutter as number) / 2,
|
|
|
|
paddingRight: (gutter as number) / 2,
|
2017-07-03 16:57:11 +08:00
|
|
|
...col.props.style,
|
|
|
|
},
|
2016-08-03 20:30:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return col;
|
2016-03-10 09:35:09 +08:00
|
|
|
});
|
2017-11-01 18:33:41 +08:00
|
|
|
const otherProps = { ...others };
|
|
|
|
delete otherProps.gutter;
|
|
|
|
return <div {...otherProps} className={classes} style={rowStyle}>{cols}</div>;
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|
|
|
|
}
|