2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2020-01-02 21:21:57 +08:00
|
|
|
import omit from 'omit.js';
|
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';
|
2020-01-02 21:21:57 +08:00
|
|
|
import { tuple } from '../_util/type';
|
|
|
|
import { BreakpointMap } from '../_util/responsiveObserve';
|
2016-08-22 17:26:14 +08:00
|
|
|
|
2019-01-23 00:36:15 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/14324
|
|
|
|
type ColSpanType = number | string;
|
|
|
|
|
2019-11-20 11:13:24 +08:00
|
|
|
type FlexType = number | 'none' | 'auto' | 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;
|
2019-11-20 11:13:24 +08:00
|
|
|
flex?: FlexType;
|
2016-08-19 17:11:06 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 11:13:24 +08:00
|
|
|
function parseFlex(flex: FlexType): string {
|
|
|
|
if (typeof flex === 'number') {
|
|
|
|
return `${flex} ${flex} auto`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/^\d+(\.\d+)?(px|em|rem|%)$/.test(flex)) {
|
|
|
|
return `0 0 ${flex}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return flex;
|
|
|
|
}
|
2016-07-13 11:14:24 +08:00
|
|
|
|
2020-01-02 21:21:57 +08:00
|
|
|
const RESPONSIVE_LIST = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
|
|
|
|
|
|
|
|
const ResponsiveTypes = tuple('xxl', 'xl', 'lg', 'md', 'sm', 'xs');
|
|
|
|
export type ResponsiveType = typeof ResponsiveTypes[number];
|
|
|
|
|
|
|
|
export function getScreenClassNames(
|
|
|
|
prefixCls: string,
|
|
|
|
screens: BreakpointMap,
|
|
|
|
{ span, order, offset, push, pull, ...restProps }: ColProps,
|
|
|
|
) {
|
|
|
|
let mergedScreenObj: ColSize = {
|
|
|
|
span,
|
|
|
|
order,
|
|
|
|
offset,
|
|
|
|
push,
|
|
|
|
pull,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let i = 0; i < RESPONSIVE_LIST.length; i += 1) {
|
|
|
|
const screen = RESPONSIVE_LIST[i] as ResponsiveType;
|
|
|
|
const screenObj = restProps[screen];
|
|
|
|
if (screens[screen] && screenObj !== undefined) {
|
|
|
|
mergedScreenObj = typeof screenObj === 'object' ? screenObj : { span: screenObj };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return classNames({
|
|
|
|
[`${prefixCls}-base-${mergedScreenObj.span}`]: mergedScreenObj.span !== undefined,
|
|
|
|
[`${prefixCls}-base-order-${mergedScreenObj.order}`]: mergedScreenObj.order !== undefined,
|
|
|
|
[`${prefixCls}-base-offset-${mergedScreenObj.offset}`]: mergedScreenObj.offset !== undefined,
|
|
|
|
[`${prefixCls}-base-push-${mergedScreenObj.push}`]: mergedScreenObj.push !== undefined,
|
|
|
|
[`${prefixCls}-base-pull-${mergedScreenObj.pull}`]: mergedScreenObj.pull !== undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-20 11:13:24 +08:00
|
|
|
export default class Col extends React.Component<ColProps, {}> {
|
2020-01-02 19:10:16 +08:00
|
|
|
renderCol = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { props } = this;
|
2020-01-02 21:21:57 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, children, flex, style, ...others } = props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('col', customizePrefixCls);
|
2020-01-02 21:21:57 +08:00
|
|
|
const restProps = omit(others, [...RESPONSIVE_LIST, 'span', 'order', 'offset', 'push', 'pull']);
|
2016-08-22 17:26:14 +08:00
|
|
|
|
2018-09-21 19:30:39 +08:00
|
|
|
return (
|
|
|
|
<RowContext.Consumer>
|
2020-01-02 21:21:57 +08:00
|
|
|
{({ gutter, screens = {} }) => {
|
2019-11-20 11:13:24 +08:00
|
|
|
let mergedStyle: React.CSSProperties = { ...style };
|
2020-01-02 21:21:57 +08:00
|
|
|
const screenClassNames = getScreenClassNames(prefixCls, screens, this.props);
|
|
|
|
|
2019-09-24 21:44:20 +08:00
|
|
|
if (gutter) {
|
2019-11-20 11:13:24 +08:00
|
|
|
mergedStyle = {
|
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,
|
|
|
|
}
|
|
|
|
: {}),
|
2019-11-20 11:13:24 +08:00
|
|
|
...mergedStyle,
|
2018-09-21 19:30:39 +08:00
|
|
|
};
|
|
|
|
}
|
2019-11-20 11:13:24 +08:00
|
|
|
if (flex) {
|
|
|
|
mergedStyle.flex = parseFlex(flex);
|
|
|
|
}
|
2019-09-24 21:44:20 +08:00
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
return (
|
2020-01-02 21:21:57 +08:00
|
|
|
<div
|
|
|
|
{...restProps}
|
|
|
|
style={mergedStyle}
|
|
|
|
className={classNames(prefixCls, className, screenClassNames, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
})}
|
|
|
|
>
|
2018-12-07 20:02:01 +08:00
|
|
|
{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
|
|
|
}
|