ant-design/components/layout/col.tsx

80 lines
2.2 KiB
TypeScript
Raw Normal View History

2016-07-07 20:25:03 +08:00
import * as React from 'react';
2016-08-22 17:26:14 +08:00
import { PropTypes } from 'react';
import classNames from 'classnames';
2016-06-22 13:18:43 +08:00
import assign from 'object-assign';
2016-08-22 17:26:14 +08:00
import splitObject from '../_util/splitObject';
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-07-13 11:14:24 +08:00
interface ColSize {
span?: number;
order?: number;
offset?: number;
push?: number;
pull?: number;
}
export interface ColProps {
2016-08-22 17:26:14 +08:00
className?: string;
span?: number;
order?: number;
offset?: number;
push?: number;
pull?: number;
xs?: ColSize;
sm?: ColSize;
md?: ColSize;
lg?: ColSize;
}
2016-08-22 17:26:14 +08:00
const Col: React.StatelessComponent<ColProps> = (props) => {
2016-07-13 11:14:24 +08:00
const [{ span, order, offset, push, pull, className, children }, others] = splitObject(props,
['span', 'order', 'offset', 'push', 'pull', 'className', 'children']);
let sizeClassObj = {};
['xs', 'sm', 'md', 'lg'].forEach(size => {
let sizeProps: ColSize = {};
if (typeof props[size] === 'number') {
sizeProps.span = props[size];
} else if (typeof props[size] === 'object') {
sizeProps = props[size] || {};
}
2016-07-13 11:14:24 +08:00
delete others[size];
2016-07-04 15:19:35 +08:00
2016-06-22 13:18:43 +08:00
sizeClassObj = assign({}, sizeClassObj, {
2016-06-06 11:40:54 +08:00
[`ant-col-${size}-${sizeProps.span}`]: sizeProps.span !== undefined,
[`ant-col-${size}-order-${sizeProps.order}`]: sizeProps.order,
[`ant-col-${size}-offset-${sizeProps.offset}`]: sizeProps.offset,
[`ant-col-${size}-push-${sizeProps.push}`]: sizeProps.push,
[`ant-col-${size}-pull-${sizeProps.pull}`]: sizeProps.pull,
2016-06-22 13:18:43 +08:00
});
});
2016-06-22 13:18:43 +08:00
const classes = classNames(assign({}, {
2016-06-06 11:40:54 +08:00
[`ant-col-${span}`]: span !== undefined,
[`ant-col-order-${order}`]: order,
[`ant-col-offset-${offset}`]: offset,
[`ant-col-push-${push}`]: push,
[`ant-col-pull-${pull}`]: pull,
[className]: !!className,
2016-07-13 11:14:24 +08:00
}, sizeClassObj));
2016-03-07 21:03:17 +08:00
return <div {...others} className={classes}>{children}</div>;
2016-08-22 17:26:14 +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,
};
2016-08-22 17:26:14 +08:00
export default Col;