ant-design/components/grid/row.tsx

62 lines
1.8 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';
import PropTypes from 'prop-types';
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, {}> {
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
};
2015-10-27 23:52:17 +08:00
render() {
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}`]: 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
});
2016-02-23 17:34:46 +08:00
return <div {...others} className={classes} style={rowStyle}>{cols}</div>;
}
}