♻️(component): [space} 切换至 tsx

This commit is contained in:
sight 2022-08-29 16:23:30 +08:00
parent eee74b05f0
commit 8ea3ae78b0
6 changed files with 142 additions and 121 deletions

View File

@ -51,6 +51,7 @@
"@types/node": "^16.11.9", "@types/node": "^16.11.9",
"@types/uuid": "^8.3.4", "@types/uuid": "^8.3.4",
"@vitejs/plugin-vue": "^2.3.3", "@vitejs/plugin-vue": "^2.3.3",
"@vitejs/plugin-vue-jsx": "^1.3.10",
"@vue/compiler-sfc": "^3.2.37", "@vue/compiler-sfc": "^3.2.37",
"@vue/server-renderer": "^3.2.37", "@vue/server-renderer": "^3.2.37",
"less": "^4.1.3", "less": "^4.1.3",

View File

@ -1,5 +1,6 @@
import { UserConfigExport } from "vite"; import { UserConfigExport } from "vite";
import vue from "@vitejs/plugin-vue"; import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { resolve } from "path"; import { resolve } from "path";
export default (): UserConfigExport => { export default (): UserConfigExport => {
@ -13,7 +14,7 @@ export default (): UserConfigExport => {
}, },
], ],
}, },
plugins: [vue()], plugins: [vue(), vueJsx],
build: { build: {
cssCodeSplit: false, cssCodeSplit: false,
outDir: "lib", outDir: "lib",

View File

@ -1,5 +1,6 @@
import { UserConfigExport } from "vite"; import { UserConfigExport } from "vite";
import vue from "@vitejs/plugin-vue"; import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { resolve } from "path"; import { resolve } from "path";
import * as fs from "fs"; import * as fs from "fs";
@ -39,6 +40,8 @@ const matchModule: string[] = [
"scroll", "scroll",
"radio", "radio",
"empty", "empty",
"dropdownMenu",
"dropdownMenuItem",
]; ];
export default (): UserConfigExport => { export default (): UserConfigExport => {
@ -60,7 +63,7 @@ export default (): UserConfigExport => {
}, },
postcss: {}, postcss: {},
}, },
plugins: [vue()], plugins: [vue(), vueJsx()],
build: { build: {
cssCodeSplit: true, cssCodeSplit: true,
emptyOutDir: true, emptyOutDir: true,

View File

@ -1,5 +1,6 @@
import { UserConfigExport } from "vite"; import { UserConfigExport } from "vite";
import vue from "@vitejs/plugin-vue"; import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { resolve } from "path"; import { resolve } from "path";
export default (): UserConfigExport => { export default (): UserConfigExport => {
@ -13,7 +14,7 @@ export default (): UserConfigExport => {
}, },
], ],
}, },
plugins: [vue()], plugins: [vue(), vueJsx],
build: { build: {
cssCodeSplit: false, cssCodeSplit: false,
outDir: "umd", outDir: "umd",

View File

@ -1,53 +1,60 @@
<script lang="ts"> <script lang="tsx">
export default {
name: "LaySpace",
};
</script>
<script lang="ts" setup>
import "./index.less"; import "./index.less";
import { import {
computed, computed,
h,
useSlots,
Comment, Comment,
VNode, VNode,
Fragment, Fragment,
isVNode, isVNode,
createTextVNode, createTextVNode,
VNodeArrayChildren,
StyleValue, StyleValue,
defineComponent,
PropType,
VNodeNormalizedChildren,
} from "vue"; } from "vue";
import RenderFunction from "../_components/renderFunction";
export type SpaceSize = "lg" | "md" | "sm" | "xs" | number | string; export type SpaceSize = "lg" | "md" | "sm" | "xs" | number | string;
export interface LaySpaceProps { export default defineComponent({
name: "LaySpace",
props: {
/* 对齐方式 */ /* 对齐方式 */
align?: "start" | "end" | "center" | "baseline"; align: {
type: String as PropType<"start" | "end" | "center" | "baseline">,
},
/* 间距方向 */ /* 间距方向 */
direction?: "horizontal" | "vertical"; direction: {
type: String as PropType<"horizontal" | "vertical">,
default: "horizontal",
},
/* 子元素是否填充父容器 */ /* 子元素是否填充父容器 */
fill?: boolean; fill: {
type: Boolean,
default: false,
},
/* 间距大小 */ /* 间距大小 */
size?: SpaceSize | [SpaceSize, SpaceSize]; size: {
type: [Number, String, Array] as PropType<
SpaceSize | [SpaceSize, SpaceSize]
>,
default: "sm",
},
/* 是否自动折行 */ /* 是否自动折行 */
wrap?: boolean; wrap: {
} type: Boolean,
default: false,
const props = withDefaults(defineProps<LaySpaceProps>(), { },
direction: "horizontal", },
size: "sm",
});
const slots = useSlots();
setup(props, { slots }) {
const computAlign = computed( const computAlign = computed(
() => props.align ?? (props.direction === "horizontal" ? "center" : "") () => props.align ?? (props.direction === "horizontal" ? "center" : "")
); );
const extractChildren = () => { const extractChildren = () => {
const result: VNode[] = []; const result: VNode[] = [];
const children = slots.default && (slots?.default() as VNodeArrayChildren); const children =
slots.default && (slots?.default() as VNodeNormalizedChildren);
const elementData = Array.isArray(children) ? [...children] : []; const elementData = Array.isArray(children) ? [...children] : [];
while (elementData.length) { while (elementData.length) {
@ -76,14 +83,14 @@ const extractChildren = () => {
const spaceClass = computed(() => [ const spaceClass = computed(() => [
"layui-space", "layui-space",
{ {
[`layui-space-align-${props.align}`]: computAlign.value, [`layui-space-align-${computAlign.value}`]: computAlign.value,
[`layui-space-${props.direction}`]: props.direction, [`layui-space-${props.direction}`]: props.direction,
[`layui-space-wrap`]: props.wrap, [`layui-space-wrap`]: props.wrap,
[`layui-space-fill`]: props.fill, [`layui-space-fill`]: props.fill,
}, },
]); ]);
const spaceStyle = computed(() => { const spaceStyle = computed<StyleValue>(() => {
const sizeMap = { xs: "4px", sm: "8px", md: "16px", lg: "24px" }; const sizeMap = { xs: "4px", sm: "8px", md: "16px", lg: "24px" };
let gap = ""; let gap = "";
@ -94,13 +101,16 @@ const spaceStyle = computed(() => {
return `${size}px`; return `${size}px`;
} }
if (typeof size === "string") { if (typeof size === "string") {
return sizeMap[props.size as keyof Omit<SpaceSize, number>] || size; return (
sizeMap[props.size as keyof Omit<SpaceSize, number>] || size
);
} }
return size; return size;
}) })
.join(" "); .join(" ");
} else if (typeof props.size === "string") { } else if (typeof props.size === "string") {
gap = sizeMap[props.size as keyof Omit<SpaceSize, string>] || props.size; gap =
sizeMap[props.size as keyof Omit<SpaceSize, string>] || props.size;
} else if (typeof props.size === "number") { } else if (typeof props.size === "number") {
gap = `${props.size}px`; gap = `${props.size}px`;
} }
@ -114,22 +124,25 @@ const itemStyle = computed<StyleValue>(() => [
props.fill ? { flexGrow: 1, minWidth: "100%" } : {}, props.fill ? { flexGrow: 1, minWidth: "100%" } : {},
]); ]);
return () => {
const children = extractChildren(); const children = extractChildren();
const renderSpaceItems = () => return (
children.map((child, index) => { <div class={spaceClass.value} style={spaceStyle.value}>
return h( {children.map((child, index) => {
"div", return (
{ <div
class: "layui-space-item", key={`item-${index}`}
style: itemStyle.value, class="layui-space-item"
}, style={itemStyle.value}
h(child) >
{child}
</div>
); );
})}
</div>
);
};
},
}); });
</script> </script>
<template>
<div :class="spaceClass" :style="spaceStyle">
<RenderFunction :renderFunc="renderSpaceItems" />
</div>
</template>

View File

@ -1,4 +1,5 @@
import vue from "@vitejs/plugin-vue"; import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import Markdown from "vite-plugin-md"; import Markdown from "vite-plugin-md";
import container from "markdown-it-container"; import container from "markdown-it-container";
import preWrapper from "./pre-wrapper"; import preWrapper from "./pre-wrapper";
@ -17,6 +18,7 @@ const plugins = [
vue({ vue({
include: [/\.vue$/, /\.md$/], include: [/\.vue$/, /\.md$/],
}), }),
vueJsx(),
Markdown({ Markdown({
markdownItOptions: { markdownItOptions: {
html: true, html: true,