ant-design/components/grid/col.tsx

124 lines
3.4 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
import classNames from 'classnames';
import RowContext from './RowContext';
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
// https://github.com/ant-design/ant-design/issues/14324
type ColSpanType = number | string;
2016-09-13 15:31:29 +08:00
export interface ColSize {
span?: ColSpanType;
order?: ColSpanType;
offset?: ColSpanType;
push?: ColSpanType;
pull?: ColSpanType;
}
export interface ColProps extends React.HTMLAttributes<HTMLDivElement> {
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;
prefixCls?: string;
}
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 = {
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,
xl: objectOrNumber,
xxl: objectOrNumber,
2016-10-24 12:04:26 +08:00
};
2016-07-13 11:14:24 +08:00
renderCol = ({ getPrefixCls }: ConfigConsumerProps) => {
2017-11-21 20:34:06 +08:00
const props: any = this.props;
const {
prefixCls: customizePrefixCls,
2018-12-07 20:02:01 +08:00
span,
order,
offset,
push,
pull,
className,
children,
...others
} = props;
const prefixCls = getPrefixCls('col', customizePrefixCls);
2016-10-24 12:04:26 +08:00
let sizeClassObj = {};
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].forEach(size => {
2016-10-24 12:04:26 +08:00
let sizeProps: ColSize = {};
if (typeof props[size] === 'number') {
sizeProps.span = props[size];
} else if (typeof props[size] === 'object') {
sizeProps = props[size] || {};
}
2016-07-04 15:19:35 +08:00
2016-10-24 12:04:26 +08:00
delete others[size];
sizeClassObj = {
...sizeClassObj,
2016-10-24 12:04:26 +08:00
[`${prefixCls}-${size}-${sizeProps.span}`]: sizeProps.span !== undefined,
[`${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,
[`${prefixCls}-${size}-push-${sizeProps.push}`]: sizeProps.push || sizeProps.push === 0,
[`${prefixCls}-${size}-pull-${sizeProps.pull}`]: sizeProps.pull || sizeProps.pull === 0,
};
2016-10-24 12:04:26 +08:00
});
2018-12-07 20:02:01 +08:00
const classes = classNames(
{
[`${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
return (
<RowContext.Consumer>
{({ gutter }) => {
let style = others.style;
if (gutter! > 0) {
style = {
paddingLeft: gutter! / 2,
paddingRight: gutter! / 2,
...style,
};
}
2018-12-07 20:02:01 +08:00
return (
<div {...others} style={style} className={classes}>
{children}
</div>
);
}}
</RowContext.Consumer>
2018-12-07 20:02:01 +08:00
);
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderCol}</ConfigConsumer>;
}
2016-10-24 12:04:26 +08:00
}