ant-design/components/grid/col.tsx
afc163 30fe9918d8
Merge branch 'master' into feature
* master:
  📝 Add bodyStyle and style of Drawer in documentation, #13850
  🐛 Fix Drawer style not working
  🐛 fix markStyle affset style
  Update Santa.jsx
  update changelog
  bump 3.11.6
  revert Christmas egg
  [type] make type of components compatible with ComponentType<P>
  bump 3.11.5
   site: make document side bar sticky
2018-12-25 16:28:50 +08:00

121 lines
3.3 KiB
TypeScript

import * as React from 'react';
import * as PropTypes from 'prop-types';
import classNames from 'classnames';
import RowContext from './RowContext';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
const objectOrNumber = PropTypes.oneOfType([PropTypes.object, PropTypes.number]);
export interface ColSize {
span?: number;
order?: number;
offset?: number;
push?: number;
pull?: number;
}
export interface ColProps extends React.HTMLAttributes<HTMLDivElement> {
span?: number;
order?: number;
offset?: number;
push?: number;
pull?: number;
xs?: number | ColSize;
sm?: number | ColSize;
md?: number | ColSize;
lg?: number | ColSize;
xl?: number | ColSize;
xxl?: number | ColSize;
prefixCls?: string;
}
export default class Col extends React.Component<ColProps, {}> {
static propTypes = {
span: PropTypes.number,
order: PropTypes.number,
offset: PropTypes.number,
push: PropTypes.number,
pull: PropTypes.number,
className: PropTypes.string,
children: PropTypes.node,
xs: objectOrNumber,
sm: objectOrNumber,
md: objectOrNumber,
lg: objectOrNumber,
xl: objectOrNumber,
xxl: objectOrNumber,
};
renderCol = ({ getPrefixCls }: ConfigConsumerProps) => {
const props: any = this.props;
const {
prefixCls: customizePrefixCls,
span,
order,
offset,
push,
pull,
className,
children,
...others
} = props;
const prefixCls = getPrefixCls('col', customizePrefixCls);
let sizeClassObj = {};
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].forEach(size => {
let sizeProps: ColSize = {};
if (typeof props[size] === 'number') {
sizeProps.span = props[size];
} else if (typeof props[size] === 'object') {
sizeProps = props[size] || {};
}
delete others[size];
sizeClassObj = {
...sizeClassObj,
[`${prefixCls}-${size}-${sizeProps.span}`]: sizeProps.span !== undefined,
[`${prefixCls}-${size}-order-${sizeProps.order}`]: sizeProps.order || sizeProps.order === 0,
[`${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,
};
});
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,
);
return (
<RowContext.Consumer>
{({ gutter }) => {
let style = others.style;
if ((gutter as number) > 0) {
style = {
paddingLeft: (gutter as number) / 2,
paddingRight: (gutter as number) / 2,
...style,
};
}
return (
<div {...others} style={style} className={classes}>
{children}
</div>
);
}}
</RowContext.Consumer>
);
};
render() {
return <ConfigConsumer>{this.renderCol}</ConfigConsumer>;
}
}