2016-02-25 16:48:03 +08:00
|
|
|
import React, { PropTypes } from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2015-10-27 23:52:17 +08:00
|
|
|
|
2016-02-25 16:48:03 +08:00
|
|
|
const stringOrNumber = PropTypes.oneOfType([PropTypes.string, PropTypes.number]);
|
2016-03-07 21:03:17 +08:00
|
|
|
const objectOrNumber = PropTypes.oneOfType([PropTypes.object, PropTypes.number]);
|
2016-02-25 16:48:03 +08:00
|
|
|
|
2016-04-13 17:38:53 +08:00
|
|
|
export default function Col(props) {
|
|
|
|
const { span, order, offset, push, pull, className, children, ...others } = props;
|
|
|
|
let sizeClassObj = {};
|
|
|
|
['xs', 'sm', 'md', 'lg'].forEach(size => {
|
|
|
|
let sizeProps = {};
|
|
|
|
if (typeof props[size] === 'number') {
|
|
|
|
sizeProps.span = props[size];
|
|
|
|
} else if (typeof props[size] === 'object') {
|
|
|
|
sizeProps = props[size] || {};
|
|
|
|
}
|
|
|
|
sizeClassObj = {
|
2016-03-07 21:03:17 +08:00
|
|
|
...sizeClassObj,
|
2016-04-13 17:38:53 +08:00
|
|
|
[`col-${size}-${sizeProps.span}`]: sizeProps.span !== undefined,
|
|
|
|
[`col-${size}-order-${sizeProps.order}`]: sizeProps.order,
|
|
|
|
[`col-${size}-offset-${sizeProps.offset}`]: sizeProps.offset,
|
|
|
|
[`col-${size}-push-${sizeProps.push}`]: sizeProps.push,
|
|
|
|
[`col-${size}-pull-${sizeProps.pull}`]: sizeProps.pull,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const classes = classNames({
|
|
|
|
[`col-${span}`]: span !== undefined,
|
|
|
|
[`col-order-${order}`]: order,
|
|
|
|
[`col-offset-${offset}`]: offset,
|
|
|
|
[`col-push-${push}`]: push,
|
|
|
|
[`col-pull-${pull}`]: pull,
|
|
|
|
[className]: !!className,
|
|
|
|
...sizeClassObj,
|
|
|
|
});
|
2016-03-07 21:03:17 +08:00
|
|
|
|
2016-04-13 17:38:53 +08:00
|
|
|
return <div {...others} className={classes}>{children}</div>;
|
2016-03-28 23:21:47 +08:00
|
|
|
}
|
2016-04-13 17:38:53 +08:00
|
|
|
|
|
|
|
Col.propTypes = {
|
|
|
|
span: stringOrNumber,
|
|
|
|
order: stringOrNumber,
|
|
|
|
offset: stringOrNumber,
|
|
|
|
push: stringOrNumber,
|
|
|
|
pull: stringOrNumber,
|
|
|
|
className: PropTypes.string,
|
|
|
|
children: PropTypes.node,
|
|
|
|
xs: objectOrNumber,
|
|
|
|
sm: objectOrNumber,
|
|
|
|
md: objectOrNumber,
|
|
|
|
lg: objectOrNumber,
|
|
|
|
};
|