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

99 lines
2.7 KiB
Vue
Raw Normal View History

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