ant-design-vue/components/space/index.tsx

79 lines
2.2 KiB
Vue
Raw Normal View History

2020-11-01 07:03:33 +00:00
import { inject, defineComponent, PropType } from 'vue';
2020-10-17 04:13:55 +00:00
import PropTypes from '../_util/vue-types';
2020-08-15 06:43:14 +00:00
import { filterEmpty } from '../_util/props-util';
import { defaultConfigProvider, SizeType } from '../config-provider';
2020-11-01 07:03:33 +00:00
import { tuple, withInstall } from '../_util/type';
2020-08-12 10:22:21 +00:00
const spaceSize = {
small: 8,
middle: 16,
large: 24,
};
2020-10-17 04:13:55 +00:00
const Space = defineComponent({
name: 'ASpace',
props: {
prefixCls: PropTypes.string,
size: {
type: [String, Number] as PropType<number | SizeType>,
},
direction: PropTypes.oneOf(tuple('horizontal', 'vertical')),
align: PropTypes.oneOf(tuple('start', 'end', 'center', 'baseline')),
},
setup(props, { slots }) {
const configProvider = inject('configProvider', defaultConfigProvider);
2020-08-12 10:22:21 +00:00
2020-10-17 04:13:55 +00:00
return () => {
2020-10-18 13:49:37 +00:00
const {
align,
size = 'small',
direction = 'horizontal',
prefixCls: customizePrefixCls,
} = props;
const { getPrefixCls } = configProvider;
2020-10-17 04:13:55 +00:00
const prefixCls = getPrefixCls('space', customizePrefixCls);
const items = filterEmpty(slots.default?.());
const len = items.length;
2020-08-12 10:22:21 +00:00
2020-10-17 04:13:55 +00:00
if (len === 0) {
return null;
}
2020-08-12 10:22:21 +00:00
2020-10-17 04:13:55 +00:00
const mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
2020-08-12 10:22:21 +00:00
2020-10-17 04:13:55 +00:00
const someSpaceClass = {
[prefixCls]: true,
[`${prefixCls}-${direction}`]: true,
[`${prefixCls}-align-${mergedAlign}`]: mergedAlign,
};
2020-08-12 10:22:21 +00:00
2020-10-17 04:13:55 +00:00
const itemClassName = `${prefixCls}-item`;
const marginDirection = 'marginRight'; // directionConfig === 'rtl' ? 'marginLeft' : 'marginRight';
2020-08-12 10:22:21 +00:00
2020-10-17 04:13:55 +00:00
return (
<div class={someSpaceClass}>
{items.map((child, i) => (
<div
class={itemClassName}
key={`${itemClassName}-${i}`}
style={
i === len - 1
? {}
: {
[direction === 'vertical' ? 'marginBottom' : marginDirection]:
typeof size === 'string' ? `${spaceSize[size]}px` : `${size}px`,
}
}
>
{child}
</div>
))}
2020-08-12 14:08:06 +00:00
</div>
2020-10-17 04:13:55 +00:00
);
};
},
});
2020-08-12 10:22:21 +00:00
2020-11-01 07:03:33 +00:00
export default withInstall(Space);