feat: 函数式弹框组件添加结合Formdemo示例

This commit is contained in:
xiaoxian521 2023-05-10 01:47:43 +08:00
parent fd9c19dd00
commit 7957dc2c18
3 changed files with 162 additions and 4 deletions

View File

@ -128,7 +128,7 @@ interface DialogOptions extends DialogProps {
close: Function;
titleId: string;
titleClass: string;
}) => VNode;
}) => VNode | Component;
/** 自定义内容渲染器 */
contentRenderer?: ({
options,
@ -136,7 +136,7 @@ interface DialogOptions extends DialogProps {
}: {
options: DialogOptions;
index: number;
}) => VNode;
}) => VNode | Component;
/** 自定义按钮操作区的内容渲染器,会覆盖`footerButtons`以及默认的 `取消` 和 `确定` 按钮 */
footerRenderer?: ({
options,
@ -144,7 +144,7 @@ interface DialogOptions extends DialogProps {
}: {
options: DialogOptions;
index: number;
}) => VNode;
}) => VNode | Component;
/** 自定义底部按钮操作 */
footerButtons?: Array<ButtonProps>;
/** `Dialog` 打开后的回调 */

View File

@ -0,0 +1,45 @@
<script setup lang="ts">
import { ref } from "vue";
// props
export interface FormProps {
formInline: {
user: string;
region: string;
};
}
// props
// https://cn.vuejs.org/guide/typescript/composition-api.html#typing-component-props
const props = withDefaults(defineProps<FormProps>(), {
formInline: () => ({ user: "", region: "" })
});
// vue prop prop form.vue
// prop 使 prop
// https://cn.vuejs.org/guide/components/props.html#one-way-data-flow
const newFormInline = ref(props.formInline);
</script>
<template>
<el-form :model="newFormInline">
<el-form-item label="姓名">
<el-input
class="!w-[220px]"
v-model="newFormInline.user"
placeholder="请输入姓名"
/>
</el-form-item>
<el-form-item label="城市">
<el-select
class="!w-[220px]"
v-model="newFormInline.region"
placeholder="请选择城市"
>
<el-option label="上海" value="上海" />
<el-option label="浙江" value="浙江" />
<el-option label="深圳" value="深圳" />
</el-select>
</el-form-item>
</el-form>
</template>

View File

@ -1,6 +1,8 @@
<script setup lang="tsx">
import { h, createVNode } from "vue";
import { h, createVNode, ref } from "vue";
import { message } from "@/utils/message";
import { cloneDeep } from "@pureadmin/utils";
import forms, { type FormProps } from "./form.vue";
import { addDialog, closeDialog, closeAllDialog } from "@/components/ReDialog";
defineOptions({
@ -225,6 +227,102 @@ function onNestingClick() {
)
});
}
// Form props prop
function onFormOneClick() {
addDialog({
width: "30%",
title: "结合Form表单第一种方式",
contentRenderer: () => forms,
props: {
//
formInline: {
user: "菜虚鲲",
region: "浙江"
}
},
closeCallBack: ({ options, args }) => {
// options.props
const { formInline } = options.props as FormProps;
const text = `姓名:${formInline.user} 城市:${formInline.region}`;
if (args?.command === "cancel") {
//
message(`您点击了取消按钮,当前表单数据为 ${text}`);
} else if (args?.command === "sure") {
message(`您点击了确定按钮,当前表单数据为 ${text}`);
} else {
message(`您点击了右上角关闭按钮或者空白页,当前表单数据为 ${text}`);
}
}
});
}
// Formh https://cn.vuejs.org/api/render-function.html#h
const formInline = ref({
user: "菜虚鲲",
region: "浙江"
});
const resetFormInline = cloneDeep(formInline.value);
function onFormTwoClick() {
addDialog({
width: "30%",
title: "结合Form表单第二种方式",
contentRenderer: () => h(forms, { formInline: formInline.value }),
closeCallBack: () => {
message(
`当前表单数据为 姓名:${formInline.value.user} 城市:${formInline.value.region}`
);
//
formInline.value = cloneDeep(resetFormInline);
}
});
}
// FormcreateVNode https://cn.vuejs.org/guide/extras/render-function.html#creating-vnodes
const formThreeInline = ref({
user: "菜虚鲲",
region: "浙江"
});
const resetFormThreeInline = cloneDeep(formThreeInline.value);
function onFormThreeClick() {
addDialog({
width: "30%",
title: "结合Form表单第三种方式",
contentRenderer: () =>
createVNode(forms, { formInline: formThreeInline.value }),
closeCallBack: () => {
message(
`当前表单数据为 姓名:${formThreeInline.value.user} 城市:${formThreeInline.value.region}`
);
//
formThreeInline.value = cloneDeep(resetFormThreeInline);
}
});
}
// Form使jsx
// forms forms contentRenderer: () => formsh(forms) createVNode(createVNode)
// 使jsx `contentRenderer: () => <forms formInline={formFourInline.value} />` forms `script` forms
// tsx 使 `contentRenderer: () => <forms formInline={formFourInline.value} />` forms return forms
const formFourInline = ref({
user: "菜虚鲲",
region: "浙江"
});
const resetFormFourInline = cloneDeep(formFourInline.value);
function onFormFourClick() {
addDialog({
width: "30%",
title: "结合Form表单第四种方式",
contentRenderer: () => <forms formInline={formFourInline.value} />,
closeCallBack: () => {
message(
`当前表单数据为 姓名:${formFourInline.value.user} 城市:${formFourInline.value.region}`
);
//
formFourInline.value = cloneDeep(resetFormFourInline);
}
});
}
</script>
<template>
@ -267,5 +365,20 @@ function onNestingClick() {
<el-button @click="onCloseCallBackClick"> 关闭后的回调 </el-button>
<el-button @click="onNestingClick"> 嵌套的弹框 </el-button>
</el-space>
<el-divider />
<el-space wrap>
<el-button @click="onFormOneClick">
结合Form表单第一种方式
</el-button>
<el-button @click="onFormTwoClick">
结合Form表单第二种方式
</el-button>
<el-button @click="onFormThreeClick">
结合Form表单第三种方式
</el-button>
<el-button @click="onFormFourClick">
结合Form表单第四种方式
</el-button>
</el-space>
</el-card>
</template>