2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-09-05 10:17:56 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import type { LiteralUnion } from '../_util/type';
|
2020-10-14 16:35:29 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-21 22:14:15 +08:00
|
|
|
import RowContext from './RowContext';
|
2022-03-31 12:45:41 +08:00
|
|
|
import { useColStyle } from './style';
|
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;
|
|
|
|
|
2023-03-26 23:37:46 +08:00
|
|
|
type FlexType = number | LiteralUnion<'none' | 'auto'>;
|
2019-11-20 11:13:24 +08:00
|
|
|
|
2016-09-13 15:31:29 +08:00
|
|
|
export interface ColSize {
|
2020-09-04 10:50:36 +08:00
|
|
|
flex?: FlexType;
|
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> {
|
2020-09-04 10:50:36 +08:00
|
|
|
flex?: FlexType;
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2020-11-13 19:37:23 +08:00
|
|
|
const sizes = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'] as const;
|
2020-07-10 17:38:28 +08:00
|
|
|
const Col = React.forwardRef<HTMLDivElement, ColProps>((props, ref) => {
|
2020-10-14 16:35:29 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2023-09-05 10:17:56 +08:00
|
|
|
const { gutter, wrap } = React.useContext(RowContext);
|
2020-10-14 16:35:29 +08:00
|
|
|
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
span,
|
|
|
|
order,
|
|
|
|
offset,
|
|
|
|
push,
|
|
|
|
pull,
|
|
|
|
className,
|
|
|
|
children,
|
|
|
|
flex,
|
|
|
|
style,
|
|
|
|
...others
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('col', customizePrefixCls);
|
2023-11-17 16:25:47 +08:00
|
|
|
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useColStyle(prefixCls);
|
2020-10-14 16:35:29 +08:00
|
|
|
|
|
|
|
let sizeClassObj = {};
|
2022-11-19 13:47:33 +08:00
|
|
|
sizes.forEach((size) => {
|
2020-10-14 16:35:29 +08:00
|
|
|
let sizeProps: ColSize = {};
|
|
|
|
const propSize = props[size];
|
|
|
|
if (typeof propSize === 'number') {
|
|
|
|
sizeProps.span = propSize;
|
|
|
|
} else if (typeof propSize === 'object') {
|
|
|
|
sizeProps = propSize || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-04-25 10:32:56 +08:00
|
|
|
[`${prefixCls}-${size}-flex-${sizeProps.flex}`]: sizeProps.flex || sizeProps.flex === 'auto',
|
2020-10-14 16:35:29 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const classes = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${span}`]: span !== undefined,
|
|
|
|
[`${prefixCls}-order-${order}`]: order,
|
|
|
|
[`${prefixCls}-offset-${offset}`]: offset,
|
|
|
|
[`${prefixCls}-push-${push}`]: push,
|
|
|
|
[`${prefixCls}-pull-${pull}`]: pull,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
sizeClassObj,
|
2022-03-31 12:45:41 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2020-10-14 16:35:29 +08:00
|
|
|
);
|
|
|
|
|
2021-02-03 18:06:51 +08:00
|
|
|
const mergedStyle: React.CSSProperties = {};
|
|
|
|
// Horizontal gutter use padding
|
|
|
|
if (gutter && gutter[0] > 0) {
|
|
|
|
const horizontalGutter = gutter[0] / 2;
|
|
|
|
mergedStyle.paddingLeft = horizontalGutter;
|
|
|
|
mergedStyle.paddingRight = horizontalGutter;
|
|
|
|
}
|
|
|
|
|
2020-10-14 16:35:29 +08:00
|
|
|
if (flex) {
|
|
|
|
mergedStyle.flex = parseFlex(flex);
|
2020-10-30 22:13:23 +08:00
|
|
|
|
|
|
|
// Hack for Firefox to avoid size issue
|
|
|
|
// https://github.com/ant-design/ant-design/pull/20023#issuecomment-564389553
|
2021-09-15 17:13:51 +08:00
|
|
|
if (wrap === false && !mergedStyle.minWidth) {
|
2020-10-30 22:13:23 +08:00
|
|
|
mergedStyle.minWidth = 0;
|
|
|
|
}
|
2020-10-14 16:35:29 +08:00
|
|
|
}
|
2019-09-24 21:44:20 +08:00
|
|
|
|
2023-11-17 16:25:47 +08:00
|
|
|
return wrapCSSVar(
|
2022-05-19 10:05:40 +08:00
|
|
|
<div {...others} style={{ ...mergedStyle, ...style }} className={classes} ref={ref}>
|
2020-10-14 16:35:29 +08:00
|
|
|
{children}
|
2022-03-31 12:45:41 +08:00
|
|
|
</div>,
|
2020-10-14 16:35:29 +08:00
|
|
|
);
|
2020-07-10 17:38:28 +08:00
|
|
|
});
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Col.displayName = 'Col';
|
|
|
|
}
|
2020-07-10 17:38:28 +08:00
|
|
|
|
|
|
|
export default Col;
|