winbox-ui-next
Version:
We Design Vue 2.0: A Vue.js 3 UI Library
90 lines (89 loc) • 2.51 kB
JavaScript
import { defineComponent, computed, Comment, createVNode } from "vue";
import { isArray, isNumber } from "../_utils/is.js";
import { getAllElements } from "../_utils/vue-utils.js";
import { getPrefixCls } from "../_utils/global-config.js";
var _Space = defineComponent({
name: "Space",
props: {
align: {
type: String
},
direction: {
type: String,
default: "horizontal"
},
size: {
type: [Number, String],
default: "small"
},
wrap: {
type: Boolean
},
fill: {
type: Boolean
}
},
setup(props, {
slots
}) {
const prefixCls = getPrefixCls("space");
const mergedAlign = computed(() => {
var _a;
return (_a = props.align) != null ? _a : props.direction === "horizontal" ? "center" : "";
});
const cls = computed(() => [prefixCls, {
[`${prefixCls}-${props.direction}`]: props.direction,
[`${prefixCls}-align-${mergedAlign.value}`]: mergedAlign.value,
[`${prefixCls}-wrap`]: props.wrap,
[`${prefixCls}-fill`]: props.fill
}]);
function getMargin(size) {
if (isNumber(size)) {
return size;
}
switch (size) {
case "mini":
return 4;
case "small":
return 8;
case "medium":
return 16;
case "large":
return 24;
default:
return 8;
}
}
const getMarginStyle = (index, isLast) => {
const style = {};
const marginRight = `${getMargin(isArray(props.size) ? props.size[0] : props.size)}px`;
const marginBottom = `${getMargin(isArray(props.size) ? props.size[1] : props.size)}px`;
if (isLast) {
return props.wrap ? {
marginBottom
} : {};
}
if (props.direction === "horizontal") {
style.marginRight = marginRight;
}
if (props.direction === "vertical" || props.wrap) {
style.marginBottom = marginBottom;
}
return style;
};
return () => {
var _a;
const children = getAllElements((_a = slots.default) == null ? void 0 : _a.call(slots)).filter((item) => item.type !== Comment);
return createVNode("div", {
"class": cls.value
}, [children.map((child, index) => {
return createVNode("div", {
"key": index,
"class": `${prefixCls}-item`,
"style": getMarginStyle(index, index === children.length - 1)
}, [child]);
})]);
};
}
});
export { _Space as default };