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

75 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-08-23 08:45:55 +00:00
import { inject, App, CSSProperties, SetupContext } from 'vue';
2020-08-15 06:43:14 +00:00
import { filterEmpty } from '../_util/props-util';
import { defaultConfigProvider, SizeType } from '../config-provider';
2020-08-12 10:22:21 +00:00
const spaceSize = {
small: 8,
middle: 16,
large: 24,
};
2020-08-23 08:45:55 +00:00
export interface SpaceProps {
prefixCls?: string;
className?: string;
style?: CSSProperties;
size?: SizeType | number;
direction?: 'horizontal' | 'vertical';
// No `stretch` since many components do not support that.
align?: 'start' | 'end' | 'center' | 'baseline';
}
2020-08-12 10:22:21 +00:00
2020-08-23 08:45:55 +00:00
const Space = (props: SpaceProps, { slots }: SetupContext) => {
const configProvider = inject('configProvider', defaultConfigProvider);
const { align, size = 'small', direction = 'horizontal', prefixCls: customizePrefixCls } = props;
2020-08-12 10:22:21 +00:00
2020-08-23 08:45:55 +00:00
const { getPrefixCls } = configProvider;
const prefixCls = getPrefixCls('space', customizePrefixCls);
const items = filterEmpty(slots.default?.());
const len = items.length;
2020-08-12 10:22:21 +00:00
2020-08-23 08:45:55 +00:00
if (len === 0) {
return null;
}
2020-08-12 10:22:21 +00:00
2020-08-23 08:45:55 +00:00
const mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
2020-08-12 10:22:21 +00:00
2020-08-23 08:45:55 +00:00
const someSpaceClass = {
[prefixCls]: true,
[`${prefixCls}-${direction}`]: true,
[`${prefixCls}-align-${mergedAlign}`]: mergedAlign,
};
2020-08-12 10:22:21 +00:00
2020-08-23 08:45:55 +00:00
const itemClassName = `${prefixCls}-item`;
const marginDirection = 'marginRight'; // directionConfig === 'rtl' ? 'marginLeft' : 'marginRight';
2020-08-12 10:22:21 +00:00
2020-08-12 14:08:06 +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>
))}
</div>
);
2020-08-12 10:22:21 +00:00
};
Space.displayName = 'ASpace';
2020-08-12 10:22:21 +00:00
/* istanbul ignore next */
2020-08-23 08:45:55 +00:00
Space.install = function(app: App) {
app.component(Space.displayName, Space);
2020-08-12 10:22:21 +00:00
};
2020-08-23 08:45:55 +00:00
2020-08-19 07:23:50 +00:00
export default Space;