ant-design/components/grid/row.tsx

112 lines
3.1 KiB
TypeScript
Raw Normal View History

2017-11-01 18:33:41 +08:00
// matchMedia polyfill for
// https://github.com/WickyNilliams/enquire.js/issues/82
if (typeof window !== 'undefined') {
const matchMediaPolyfill = (mediaQuery: string): MediaQueryList => {
return {
media: mediaQuery,
matches: false,
addListener() {
},
removeListener() {
},
};
};
window.matchMedia = window.matchMedia || matchMediaPolyfill;
}
2016-09-21 11:54:53 +08:00
import React from 'react';
import { Children, cloneElement } from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
2017-11-01 18:51:55 +08:00
const enquire = require('enquire.js');
export interface RowProps {
2016-08-22 17:26:14 +08:00
className?: string;
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;
prefixCls?: string;
}
2017-11-01 18:33:41 +08:00
const responsiveMap = {
xs: '(max-width: 575px)',
sm: 'only screen and (min-width: 576px) and (max-width: 767px)',
md: 'only screen and (min-width: 767px) and (max-width: 991px)',
lg: 'only screen and (min-width: 992px) and (max-width: 1199px)',
xl: 'only screen and (min-width: 1200px) and (max-width: 1599px)',
xxl: '(min-width: 1600px)',
};
export default class Row extends React.Component<RowProps, {}> {
static defaultProps = {
gutter: 0,
2016-07-13 11:14:24 +08:00
};
static propTypes = {
type: PropTypes.string,
align: PropTypes.string,
justify: PropTypes.string,
className: PropTypes.string,
children: PropTypes.node,
gutter: PropTypes.number,
prefixCls: PropTypes.string,
2016-07-13 11:14:24 +08:00
};
2017-11-01 18:33:41 +08:00
state = {
screen: 'xxl',
};
componentDidMount() {
Object.keys(responsiveMap)
.map((screen) => enquire.register(
responsiveMap[screen], () => this.setState({ screen }),
));
}
componentWillUnmount() {
Object.keys(responsiveMap)
.map(screen => enquire.unregister(responsiveMap[screen]));
}
getGutter() {
const { gutter } = this.props;
if (typeof gutter === 'object') {
return gutter[this.state.screen];
}
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();
const classes = classNames({
[prefixCls]: !type,
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${type}-${justify}`]: type && justify,
[`${prefixCls}-${type}-${align}`]: type && align,
}, className);
const rowStyle = (gutter as number) > 0 ? {
marginLeft: (gutter as number) / -2,
marginRight: (gutter as number) / -2,
...style,
} : 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) {
return cloneElement(col, {
style: {
paddingLeft: (gutter as number) / 2,
paddingRight: (gutter as number) / 2,
...col.props.style,
},
});
}
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>;
}
}