UNPKG

element-plus

Version:

A Component Library for Vue 3

214 lines (206 loc) 6.08 kB
import { withInstall } from 'element-plus/es/utils/with-install'; import { defineComponent, computed, openBlock, createElementBlock, normalizeClass, renderSlot, ref, watchEffect, isVNode, createVNode, createTextVNode } from 'vue'; import { isString } from '@vue/shared'; import { isFragment, PatchFlags, isValidElementNode } from 'element-plus/es/utils/vnode'; import { isNumber, isArray } from 'element-plus/es/utils/util'; import { buildProps, definePropType, componentSize } from 'element-plus/es/utils/props'; const spaceItem = buildProps({ prefixCls: { type: String, default: "el-space" } }); var script = defineComponent({ props: spaceItem, setup(props) { const classes = computed(() => [`${props.prefixCls}__item`]); return { classes }; } }); function render(_ctx, _cache, $props, $setup, $data, $options) { return openBlock(), createElementBlock("div", { class: normalizeClass(_ctx.classes) }, [ renderSlot(_ctx.$slots, "default") ], 2); } script.render = render; script.__file = "packages/components/space/src/item.vue"; const SIZE_MAP = { mini: 4, small: 8, medium: 12, large: 16 }; function useSpace(props) { const classes = computed(() => [ "el-space", `el-space--${props.direction}`, props.class ]); const horizontalSize = ref(0); const verticalSize = ref(0); const containerStyle = computed(() => { const wrapKls = props.wrap || props.fill ? { flexWrap: "wrap", marginBottom: `-${verticalSize.value}px` } : {}; const alignment = { alignItems: props.alignment }; return [wrapKls, alignment, props.style]; }); const itemStyle = computed(() => { const itemBaseStyle = { paddingBottom: `${verticalSize.value}px`, marginRight: `${horizontalSize.value}px` }; const fillStyle = props.fill ? { flexGrow: 1, minWidth: `${props.fillRatio}%` } : {}; return [itemBaseStyle, fillStyle]; }); watchEffect(() => { const { size = "small", wrap, direction: dir, fill } = props; if (Array.isArray(size)) { const [h = 0, v = 0] = size; horizontalSize.value = h; verticalSize.value = v; } else { let val; if (isNumber(size)) { val = size; } else { val = SIZE_MAP[size] || SIZE_MAP.small; } if ((wrap || fill) && dir === "horizontal") { horizontalSize.value = verticalSize.value = val; } else { if (dir === "horizontal") { horizontalSize.value = val; verticalSize.value = 0; } else { verticalSize.value = val; horizontalSize.value = 0; } } } }); return { classes, containerStyle, itemStyle }; } const spaceProps = buildProps({ direction: { type: String, values: ["horizontal", "vertical"], default: "horizontal" }, class: { type: definePropType([ String, Object, Array ]), default: "" }, style: { type: definePropType([String, Array, Object]), default: "" }, alignment: { type: definePropType(String), default: "center" }, prefixCls: { type: String }, spacer: { type: definePropType([Object, String, Number, Array]), default: null, validator: (val) => isVNode(val) || isNumber(val) || isString(val) }, wrap: { type: Boolean, default: false }, fill: { type: Boolean, default: false }, fillRatio: { type: Number, default: 100 }, size: { type: [String, Array, Number], values: componentSize, validator: (val) => { return isNumber(val) || isArray(val) && val.length === 2 && val.every((i) => isNumber(i)); } } }); var Space = defineComponent({ name: "ElSpace", props: spaceProps, setup(props, { slots }) { const { classes, containerStyle, itemStyle } = useSpace(props); return () => { var _a; const { spacer, prefixCls, direction } = props; const children = renderSlot(slots, "default", { key: 0 }, () => []); if (((_a = children.children) != null ? _a : []).length === 0) return null; if (isArray(children.children)) { let extractedChildren = []; children.children.forEach((child, loopKey) => { if (isFragment(child)) { if (isArray(child.children)) { child.children.forEach((nested, key) => { extractedChildren.push(createVNode(script, { style: itemStyle.value, prefixCls, key: `nested-${key}` }, { default: () => [nested] }, PatchFlags.PROPS | PatchFlags.STYLE, ["style", "prefixCls"])); }); } } else if (isValidElementNode(child)) { extractedChildren.push(createVNode(script, { style: itemStyle.value, prefixCls, key: `LoopKey${loopKey}` }, { default: () => [child] }, PatchFlags.PROPS | PatchFlags.STYLE, ["style", "prefixCls"])); } }); if (spacer) { const len = extractedChildren.length - 1; extractedChildren = extractedChildren.reduce((acc, child, idx) => { const children2 = [...acc, child]; if (idx !== len) { children2.push(createVNode("span", { style: [ itemStyle.value, direction === "vertical" ? "width: 100%" : null ], key: idx }, [ isVNode(spacer) ? spacer : createTextVNode(spacer, PatchFlags.TEXT) ], PatchFlags.STYLE)); } return children2; }, []); } return createVNode("div", { class: classes.value, style: containerStyle.value }, extractedChildren, PatchFlags.STYLE | PatchFlags.CLASS); } return children.children; }; } }); const ElSpace = withInstall(Space); export { ElSpace, ElSpace as default, spaceProps, useSpace };