ant-design-vue/components/grid/Col.jsx

99 lines
2.7 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
2019-03-15 15:12:28 +08:00
import { ConfigConsumerProps } from '../config-provider';
2018-03-19 10:16:27 +08:00
2019-01-12 11:33:27 +08:00
const stringOrNumber = PropTypes.oneOfType([PropTypes.string, PropTypes.number]);
2018-03-08 13:55:49 +08:00
export const ColSize = PropTypes.shape({
span: stringOrNumber,
order: stringOrNumber,
offset: stringOrNumber,
push: stringOrNumber,
pull: stringOrNumber,
2019-01-12 11:33:27 +08:00
}).loose;
2018-03-08 13:55:49 +08:00
2019-09-10 21:54:22 +08:00
const objectOrNumber = PropTypes.oneOfType([PropTypes.string, PropTypes.number, ColSize]);
2018-03-08 13:55:49 +08:00
export const ColProps = {
2018-12-09 10:58:50 +08:00
span: stringOrNumber,
order: stringOrNumber,
offset: stringOrNumber,
push: stringOrNumber,
pull: stringOrNumber,
xs: objectOrNumber,
sm: objectOrNumber,
md: objectOrNumber,
lg: objectOrNumber,
xl: objectOrNumber,
xxl: objectOrNumber,
2018-03-08 13:55:49 +08:00
prefixCls: PropTypes.string,
2019-01-12 11:33:27 +08:00
};
2018-03-08 13:55:49 +08:00
export default {
2018-04-08 21:17:20 +08:00
name: 'ACol',
2019-02-01 17:23:00 +08:00
props: ColProps,
2018-12-09 10:58:50 +08:00
inject: {
2019-09-11 22:35:25 +08:00
configProvider: { default: () => ConfigConsumerProps },
2018-12-09 10:58:50 +08:00
rowContext: {
2019-01-28 21:09:13 +08:00
default: () => null,
2018-12-09 10:58:50 +08:00
},
},
2019-01-12 11:33:27 +08:00
render() {
const {
span,
order,
offset,
push,
pull,
2019-03-15 15:12:28 +08:00
prefixCls: customizePrefixCls,
2019-01-12 11:33:27 +08:00
$slots,
$listeners,
rowContext,
} = this;
2019-09-11 22:35:25 +08:00
const getPrefixCls = this.configProvider.getPrefixCls;
2019-03-15 15:12:28 +08:00
const prefixCls = getPrefixCls('col', customizePrefixCls);
2018-03-08 13:55:49 +08:00
let sizeClassObj = {};
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].forEach(size => {
2019-01-12 11:33:27 +08:00
let sizeProps = {};
2018-03-08 13:55:49 +08:00
if (typeof this[size] === 'number') {
2019-01-12 11:33:27 +08:00
sizeProps.span = this[size];
2018-03-08 13:55:49 +08:00
} else if (typeof this[size] === 'object') {
2019-01-12 11:33:27 +08:00
sizeProps = this[size] || {};
}
2018-03-08 13:55:49 +08:00
sizeClassObj = {
...sizeClassObj,
[`${prefixCls}-${size}-${sizeProps.span}`]: sizeProps.span !== undefined,
[`${prefixCls}-${size}-order-${sizeProps.order}`]: sizeProps.order || sizeProps.order === 0,
2019-01-12 11:33:27 +08:00
[`${prefixCls}-${size}-offset-${sizeProps.offset}`]:
sizeProps.offset || sizeProps.offset === 0,
2018-03-08 13:55:49 +08:00
[`${prefixCls}-${size}-push-${sizeProps.push}`]: sizeProps.push || sizeProps.push === 0,
[`${prefixCls}-${size}-pull-${sizeProps.pull}`]: sizeProps.pull || sizeProps.pull === 0,
2019-01-12 11:33:27 +08:00
};
});
2018-03-08 13:55:49 +08:00
const classes = {
[`${prefixCls}-${span}`]: span !== undefined,
[`${prefixCls}-order-${order}`]: order,
[`${prefixCls}-offset-${offset}`]: offset,
[`${prefixCls}-push-${push}`]: push,
[`${prefixCls}-pull-${pull}`]: pull,
...sizeClassObj,
2019-01-12 11:33:27 +08:00
};
2018-03-08 13:55:49 +08:00
const divProps = {
on: $listeners,
class: classes,
2018-12-09 10:58:50 +08:00
style: {},
2019-01-12 11:33:27 +08:00
};
2018-12-09 10:58:50 +08:00
if (rowContext) {
2019-01-12 11:33:27 +08:00
const gutter = rowContext.getGutter();
2018-12-09 10:58:50 +08:00
if (gutter > 0) {
divProps.style = {
paddingLeft: `${gutter / 2}px`,
paddingRight: `${gutter / 2}px`,
2019-01-12 11:33:27 +08:00
};
2018-12-09 10:58:50 +08:00
}
2018-03-08 13:55:49 +08:00
}
2019-01-12 11:33:27 +08:00
return <div {...divProps}>{$slots.default}</div>;
2018-03-08 13:55:49 +08:00
},
2019-01-12 11:33:27 +08:00
};