ant-design-vue/components/grid/Col.tsx
tangjinzhou 9e0df41a55
refactor: Anchor、Alert、Avatar、Badge、BackTop、Col、Form、Layout、Menu、Space、Spin、Switch、Row、Result、Rate (#4171)
* chore: remove  resize-observer-polyfill

* refactor: align

* refactor(v3/avatar): refactor using composition api  (#4052)

* refactor(avatar): refactor using composition api

* refactor: update props define

* fix: avatar src scale not update

* refactor: resizeObserver

* refactor: divider

* refactor: localeProvider

* refactor(v3/back-top): use composition api (#4060)

* refactor: backtop

* refactor: empty

* refactor: transButton

* feat(v3/avatar): add avatar group (#4062)

* feat(avatar): add avatar group

* refactor: update

* refactor: update

Co-authored-by: tangjinzhou <415800467@qq.com>

* refactor: avatar

* refactor: avatar

* style: rename useProvide

* refactor:  menu (#4110)

* fix: menu

* refactor: menu

* refactor: remove rc-menu

* fix: menu rtl error

* style: lint

* refactor(Anchor): use composition api (#4054)

* refactor: anchor

* refactor: anchor

* refactor: anchor

* feat: update

* fix: icon class lose

* refactor(v3/badge): use composition api (#4076)

* refactor: badge

* fix: badge inheritAttrs

* refactor: grid

* refactor: layout

* fix: menu not close

* refactor: space

* refactor: result

* refactor: affix

* refactor: comment

* refactor: form

* feat: spin add rtl

* feat: export spin type

* refactor: pageHeader

* refactor: page-header

* refactor: skeleton

* refactor: typography

* refactor(v3/rate): use composition api

* fix: add useRef hook

* refactor: form

* fix: menu not update

* refactor: form

* refactor: form

* fix: slide animate not work

* fix: menu mode error

* fix: menu icon

* refactor: rate

* perf: remove rate

* feat: add vc-overflow

* refactor: menu

* fix: remove flex check (#4165)

* fix: dist locale file lose #3684

* release 2.2.0-beta.1

* dcos: update changelog

* chore: update type

* docs: update changelog

Co-authored-by: John <John60676@qq.com>
Co-authored-by: 言肆 <18x@loacg.com>
Co-authored-by: zkwolf <chenhao5866@gmail.com>
2021-06-07 17:35:03 +08:00

139 lines
4.2 KiB
Vue

import { defineComponent, CSSProperties, ExtractPropTypes, computed } from 'vue';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import useConfigInject from '../_util/hooks/useConfigInject';
import { useInjectRow } from './context';
type ColSpanType = number | string;
type FlexType = number | 'none' | 'auto' | string;
export interface ColSize {
span?: ColSpanType;
order?: ColSpanType;
offset?: ColSpanType;
push?: ColSpanType;
pull?: ColSpanType;
}
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;
}
const stringOrNumber = PropTypes.oneOfType([PropTypes.string, PropTypes.number]);
export const colSize = PropTypes.shape({
span: stringOrNumber,
order: stringOrNumber,
offset: stringOrNumber,
push: stringOrNumber,
pull: stringOrNumber,
}).loose;
const objectOrNumber = PropTypes.oneOfType([PropTypes.string, PropTypes.number, colSize]);
const colProps = {
span: stringOrNumber,
order: stringOrNumber,
offset: stringOrNumber,
push: stringOrNumber,
pull: stringOrNumber,
xs: objectOrNumber,
sm: objectOrNumber,
md: objectOrNumber,
lg: objectOrNumber,
xl: objectOrNumber,
xxl: objectOrNumber,
prefixCls: PropTypes.string,
flex: stringOrNumber,
};
export type ColProps = Partial<ExtractPropTypes<typeof colProps>>;
export default defineComponent({
name: 'ACol',
props: colProps,
setup(props, { slots }) {
const { gutter, supportFlexGap, wrap } = useInjectRow();
const { prefixCls, direction } = useConfigInject('col', props);
const classes = computed(() => {
const { span, order, offset, push, pull } = props;
const pre = prefixCls.value;
let sizeClassObj = {};
['xs', 'sm', 'md', 'lg', 'xl', 'xxl'].forEach(size => {
let sizeProps: ColSize = {};
const propSize = props[size];
if (typeof propSize === 'number') {
sizeProps.span = propSize;
} else if (typeof propSize === 'object') {
sizeProps = propSize || {};
}
sizeClassObj = {
...sizeClassObj,
[`${pre}-${size}-${sizeProps.span}`]: sizeProps.span !== undefined,
[`${pre}-${size}-order-${sizeProps.order}`]: sizeProps.order || sizeProps.order === 0,
[`${pre}-${size}-offset-${sizeProps.offset}`]: sizeProps.offset || sizeProps.offset === 0,
[`${pre}-${size}-push-${sizeProps.push}`]: sizeProps.push || sizeProps.push === 0,
[`${pre}-${size}-pull-${sizeProps.pull}`]: sizeProps.pull || sizeProps.pull === 0,
[`${pre}-rtl`]: direction.value === 'rtl',
};
});
return classNames(
pre,
{
[`${pre}-${span}`]: span !== undefined,
[`${pre}-order-${order}`]: order,
[`${pre}-offset-${offset}`]: offset,
[`${pre}-push-${push}`]: push,
[`${pre}-pull-${pull}`]: pull,
},
sizeClassObj,
);
});
const mergedStyle = computed(() => {
const { flex } = props;
const gutterVal = gutter.value;
const style: CSSProperties = {};
// Horizontal gutter use padding
if (gutterVal && gutterVal[0] > 0) {
const horizontalGutter = `${gutterVal[0] / 2}px`;
style.paddingLeft = horizontalGutter;
style.paddingRight = horizontalGutter;
}
// Vertical gutter use padding when gap not support
if (gutterVal && gutterVal[1] > 0 && !supportFlexGap.value) {
const verticalGutter = `${gutterVal[1] / 2}px`;
style.paddingTop = verticalGutter;
style.paddingBottom = verticalGutter;
}
if (flex) {
style.flex = parseFlex(flex);
// Hack for Firefox to avoid size issue
// https://github.com/ant-design/ant-design/pull/20023#issuecomment-564389553
if (flex === 'auto' && wrap.value === false && !style.minWidth) {
style.minWidth = 0;
}
}
return style;
});
return () => {
return (
<div class={classes.value} style={mergedStyle.value}>
{slots.default?.()}
</div>
);
};
},
});