From eba372062e9e6a9b5c3543d2ec220ca156629813 Mon Sep 17 00:00:00 2001 From: Netfan Date: Fri, 21 Feb 2025 12:07:32 +0800 Subject: [PATCH] feat: improve form demo (#5582) --- .../src/locales/langs/en-US/examples.json | 1 + .../src/locales/langs/zh-CN/examples.json | 1 + .../src/router/routes/modules/examples.ts | 8 ++ playground/src/views/examples/form/basic.vue | 90 +------------- .../src/views/examples/form/custom-layout.vue | 111 ++++++++++++++++++ playground/src/views/examples/form/rules.vue | 4 +- 6 files changed, 126 insertions(+), 89 deletions(-) create mode 100644 playground/src/views/examples/form/custom-layout.vue diff --git a/playground/src/locales/langs/en-US/examples.json b/playground/src/locales/langs/en-US/examples.json index 8131380f..91666158 100644 --- a/playground/src/locales/langs/en-US/examples.json +++ b/playground/src/locales/langs/en-US/examples.json @@ -12,6 +12,7 @@ "form": { "title": "Form", "basic": "Basic Form", + "layout": "Custom Layout", "query": "Query Form", "rules": "Form Rules", "dynamic": "Dynamic Form", diff --git a/playground/src/locales/langs/zh-CN/examples.json b/playground/src/locales/langs/zh-CN/examples.json index fea65a70..0c5fc19a 100644 --- a/playground/src/locales/langs/zh-CN/examples.json +++ b/playground/src/locales/langs/zh-CN/examples.json @@ -15,6 +15,7 @@ "form": { "title": "表单", "basic": "基础表单", + "layout": "自定义布局", "query": "查询表单", "rules": "表单校验", "dynamic": "动态表单", diff --git a/playground/src/router/routes/modules/examples.ts b/playground/src/router/routes/modules/examples.ts index cc86838e..e131bf8b 100644 --- a/playground/src/router/routes/modules/examples.ts +++ b/playground/src/router/routes/modules/examples.ts @@ -53,6 +53,14 @@ const routes: RouteRecordRaw[] = [ title: $t('examples.form.dynamic'), }, }, + { + name: 'FormLayoutExample', + path: '/examples/form/custom-layout', + component: () => import('#/views/examples/form/custom-layout.vue'), + meta: { + title: $t('examples.form.layout'), + }, + }, { name: 'FormCustomExample', path: '/examples/form/custom', diff --git a/playground/src/views/examples/form/basic.vue b/playground/src/views/examples/form/basic.vue index 03534683..5d734d43 100644 --- a/playground/src/views/examples/form/basic.vue +++ b/playground/src/views/examples/form/basic.vue @@ -4,15 +4,7 @@ import { h, ref } from 'vue'; import { Page } from '@vben/common-ui'; import { useDebounceFn } from '@vueuse/core'; -import { - Button, - Card, - message, - Spin, - TabPane, - Tabs, - Tag, -} from 'ant-design-vue'; +import { Button, Card, message, Spin, Tag } from 'ant-design-vue'; import dayjs from 'dayjs'; import { useVbenForm, z } from '#/adapter/form'; @@ -20,7 +12,6 @@ import { getAllMenusApi } from '#/api'; import DocButton from '../doc-button.vue'; -const activeTab = ref('basic'); const keyword = ref(''); const fetching = ref(false); // 模拟远程获取数据 @@ -339,75 +330,6 @@ const [BaseForm, baseFormApi] = useVbenForm({ wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3', }); -const [CustomLayoutForm] = useVbenForm({ - // 所有表单项共用,可单独在表单内覆盖 - commonConfig: { - // 所有表单项 - componentProps: { - class: 'w-full', - }, - }, - layout: 'horizontal', - schema: [ - { - component: 'Select', - fieldName: 'field1', - label: '字符串', - }, - { - component: 'TreeSelect', - fieldName: 'field2', - label: '字符串', - }, - { - component: 'Mentions', - fieldName: 'field3', - label: '字符串', - }, - { - component: 'Input', - fieldName: 'field4', - label: '字符串', - }, - { - component: 'InputNumber', - fieldName: 'field5', - // 从第三列开始 相当于中间空了一列 - formItemClass: 'col-start-3', - label: '前面空了一列', - }, - { - component: 'Textarea', - fieldName: 'field6', - // 占满三列空间 基线对齐 - formItemClass: 'col-span-3 items-baseline', - label: '占满三列', - }, - { - component: 'Input', - fieldName: 'field7', - // 占满2列空间 从第二列开始 相当于前面空了一列 - formItemClass: 'col-span-2 col-start-2', - label: '占满2列', - }, - { - component: 'Input', - fieldName: 'field8', - // 左右留空 - formItemClass: 'col-start-2', - label: '左右留空', - }, - { - component: 'InputPassword', - fieldName: 'field9', - formItemClass: 'col-start-1', - label: '字符串', - }, - ], - // 一共三列 - wrapperClass: 'grid-cols-3', -}); - function onSubmit(values: Record) { message.success({ content: `form values: ${JSON.stringify(values)}`, @@ -443,7 +365,6 @@ function handleSetFormValue() { - + - - - diff --git a/playground/src/views/examples/form/custom-layout.vue b/playground/src/views/examples/form/custom-layout.vue new file mode 100644 index 00000000..cd7146b2 --- /dev/null +++ b/playground/src/views/examples/form/custom-layout.vue @@ -0,0 +1,111 @@ + + + diff --git a/playground/src/views/examples/form/rules.vue b/playground/src/views/examples/form/rules.vue index dd95894c..7ec95493 100644 --- a/playground/src/views/examples/form/rules.vue +++ b/playground/src/views/examples/form/rules.vue @@ -150,7 +150,9 @@ const [Form, formApi] = useVbenForm({ default: () => ['我已阅读并同意'], }; }, - rules: 'selectRequired', + rules: z.boolean().refine((value) => value, { + message: '请勾选', + }), }, { component: 'DatePicker',