2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2018-09-21 19:30:39 +08:00
|
|
|
import RowContext from './RowContext';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2016-08-22 17:26:14 +08:00
|
|
|
|
2016-03-07 21:03:17 +08:00
|
|
|
const objectOrNumber = PropTypes.oneOfType([PropTypes.object, PropTypes.number]);
|
2016-07-13 11:14:24 +08:00
|
|
|
|
2019-01-23 00:36:15 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/14324
|
|
|
|
type ColSpanType = number | string;
|
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface ColSize {
|
2019-01-23 00:36:15 +08:00
|
|
|
span?: ColSpanType;
|
|
|
|
order?: ColSpanType;
|
|
|
|
offset?: ColSpanType;
|
|
|
|
push?: ColSpanType;
|
|
|
|
pull?: ColSpanType;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2018-03-06 15:51:06 +08:00
|
|
|
export interface ColProps extends React.HTMLAttributes<HTMLDivElement> {
|
2019-01-23 00:36:15 +08:00
|
|
|
span?: ColSpanType;
|
|
|
|
order?: ColSpanType;
|
|
|
|
offset?: ColSpanType;
|
|
|
|
push?: ColSpanType;
|
|
|
|
pull?: ColSpanType;
|
|
|
|
xs?: ColSpanType | ColSize;
|
|
|
|
sm?: ColSpanType | ColSize;
|
|
|
|
md?: ColSpanType | ColSize;
|
|
|
|
lg?: ColSpanType | ColSize;
|
|
|
|
xl?: ColSpanType | ColSize;
|
|
|
|
xxl?: ColSpanType | ColSize;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:06 +08:00
|
|
|
export default class Col extends React.Component<ColProps, {}> {
|
2016-10-24 12:04:26 +08:00
|
|
|
static propTypes = {
|
2018-12-22 21:21:42 +08:00
|
|
|
span: PropTypes.number,
|
|
|
|
order: PropTypes.number,
|
|
|
|
offset: PropTypes.number,
|
|
|
|
push: PropTypes.number,
|
|
|
|
pull: PropTypes.number,
|
2016-10-24 12:04:26 +08:00
|
|
|
className: PropTypes.string,
|
|
|
|
children: PropTypes.node,
|
|
|
|
xs: objectOrNumber,
|
|
|
|
sm: objectOrNumber,
|
|
|
|
md: objectOrNumber,
|
|
|
|
lg: objectOrNumber,
|
2017-02-13 15:44:58 +08:00
|
|
|
xl: objectOrNumber,
|
2018-01-21 01:03:00 +08:00
|
|
|
xxl: objectOrNumber,
|
2016-10-24 12:04:26 +08:00
|
|
|
};
|
2016-07-13 11:14:24 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderCol = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { props } = this;
|
2018-12-05 19:12:18 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
span,
|
|
|
|
order,
|
|
|
|
offset,
|
|
|
|
push,
|
|
|
|
pull,
|
|
|
|
className,
|
|
|
|
children,
|
2018-12-05 19:12:18 +08:00
|
|
|
...others
|
|
|
|
} = props;
|
|
|
|
const prefixCls = getPrefixCls('col', customizePrefixCls);
|
2016-10-24 12:04:26 +08:00
|
|
|
let sizeClassObj = {};
|
2017-08-16 20:19:41 +08:00
|
|
|
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].forEach(size => {
|
2016-10-24 12:04:26 +08:00
|
|
|
let sizeProps: ColSize = {};
|
2019-08-05 18:38:10 +08:00
|
|
|
const propSize = (props as any)[size];
|
|
|
|
if (typeof propSize === 'number') {
|
|
|
|
sizeProps.span = propSize;
|
|
|
|
} else if (typeof propSize === 'object') {
|
|
|
|
sizeProps = propSize || {};
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|
2016-07-04 15:19:35 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
delete (others as any)[size];
|
2016-04-13 17:38:53 +08:00
|
|
|
|
2017-07-03 16:57:11 +08:00
|
|
|
sizeClassObj = {
|
|
|
|
...sizeClassObj,
|
2016-10-24 12:04:26 +08:00
|
|
|
[`${prefixCls}-${size}-${sizeProps.span}`]: sizeProps.span !== undefined,
|
2016-12-19 16:18:39 +08:00
|
|
|
[`${prefixCls}-${size}-order-${sizeProps.order}`]: sizeProps.order || sizeProps.order === 0,
|
2018-12-07 20:02:01 +08:00
|
|
|
[`${prefixCls}-${size}-offset-${sizeProps.offset}`]:
|
|
|
|
sizeProps.offset || sizeProps.offset === 0,
|
2016-12-19 16:18:39 +08:00
|
|
|
[`${prefixCls}-${size}-push-${sizeProps.push}`]: sizeProps.push || sizeProps.push === 0,
|
|
|
|
[`${prefixCls}-${size}-pull-${sizeProps.pull}`]: sizeProps.pull || sizeProps.pull === 0,
|
2017-07-03 16:57:11 +08:00
|
|
|
};
|
2016-10-24 12:04:26 +08:00
|
|
|
});
|
2018-12-07 20:02:01 +08:00
|
|
|
const classes = classNames(
|
2019-03-31 20:25:31 +08:00
|
|
|
prefixCls,
|
2018-12-07 20:02:01 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-${span}`]: span !== undefined,
|
|
|
|
[`${prefixCls}-order-${order}`]: order,
|
|
|
|
[`${prefixCls}-offset-${offset}`]: offset,
|
|
|
|
[`${prefixCls}-push-${push}`]: push,
|
|
|
|
[`${prefixCls}-pull-${pull}`]: pull,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
sizeClassObj,
|
|
|
|
);
|
2016-08-22 17:26:14 +08:00
|
|
|
|
2018-09-21 19:30:39 +08:00
|
|
|
return (
|
|
|
|
<RowContext.Consumer>
|
|
|
|
{({ gutter }) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
let { style } = others;
|
2019-09-24 21:44:20 +08:00
|
|
|
|
|
|
|
if (gutter) {
|
2018-09-21 19:30:39 +08:00
|
|
|
style = {
|
2019-09-24 21:44:20 +08:00
|
|
|
...(gutter[0]! > 0
|
|
|
|
? {
|
|
|
|
paddingLeft: gutter[0]! / 2,
|
|
|
|
paddingRight: gutter[0]! / 2,
|
|
|
|
}
|
|
|
|
: {}),
|
|
|
|
...(gutter[1]! > 0
|
|
|
|
? {
|
|
|
|
paddingTop: gutter[1]! / 2,
|
|
|
|
paddingBottom: gutter[1]! / 2,
|
|
|
|
}
|
|
|
|
: {}),
|
2018-09-21 19:30:39 +08:00
|
|
|
...style,
|
|
|
|
};
|
|
|
|
}
|
2019-09-24 21:44:20 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
return (
|
|
|
|
<div {...others} style={style} className={classes}>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
2018-09-21 19:30:39 +08:00
|
|
|
}}
|
|
|
|
</RowContext.Consumer>
|
2018-12-07 20:02:01 +08:00
|
|
|
);
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderCol}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-10-24 12:04:26 +08:00
|
|
|
}
|