ant-design/components/grid/row.tsx

58 lines
1.7 KiB
TypeScript
Raw Normal View History

2016-09-21 11:54:53 +08:00
import React from 'react';
import { Children, cloneElement } from 'react';
import classNames from 'classnames';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
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;
}
export default class Row extends React.Component<RowProps, any> {
static defaultProps = {
gutter: 0,
2016-07-13 11:14:24 +08:00
};
static propTypes = {
2015-10-27 23:52:17 +08:00
type: React.PropTypes.string,
align: React.PropTypes.string,
justify: React.PropTypes.string,
className: React.PropTypes.string,
2015-10-27 23:52:17 +08:00
children: React.PropTypes.node,
2016-02-23 17:34:46 +08:00
gutter: React.PropTypes.number,
prefixCls: React.PropTypes.string,
2016-07-13 11:14:24 +08:00
};
2015-10-27 23:52:17 +08:00
render() {
2016-12-19 15:19:15 +08:00
const { type, justify, align, className, gutter, style, children, prefixCls = 'ant-row', ...others } = this.props;
const classes = classNames({
[prefixCls]: !type,
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${type}-${justify}`]: justify,
[`${prefixCls}-${type}-${align}`]: align,
}, className);
2016-06-22 13:18:43 +08:00
const rowStyle = gutter > 0 ? assign({}, {
2016-02-23 17:34:46 +08:00
marginLeft: gutter / -2,
marginRight: gutter / -2,
2016-07-13 11:14:24 +08:00
}, style) : style;
2016-08-24 16:09:55 +08:00
const cols = Children.map(children, (col: React.ReactElement<any>) => {
2016-07-13 11:14:24 +08:00
if (!col) {
return null;
}
if (col.props) {
return cloneElement(col, {
2016-08-04 14:06:09 +08:00
style: gutter > 0 ? assign({}, {
paddingLeft: gutter / 2,
paddingRight: gutter / 2,
2016-08-04 14:06:09 +08:00
}, col.props.style) : col.props.style,
});
}
return col;
2016-03-10 09:35:09 +08:00
});
2016-02-23 17:34:46 +08:00
return <div {...others} className={classes} style={rowStyle}>{cols}</div>;
}
}