Skip to content

Commit eba3720

Browse files
authored
feat: improve form demo (#5582)
1 parent c9ccd2b commit eba3720

File tree

6 files changed

+126
-89
lines changed

6 files changed

+126
-89
lines changed

playground/src/locales/langs/en-US/examples.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"form": {
1313
"title": "Form",
1414
"basic": "Basic Form",
15+
"layout": "Custom Layout",
1516
"query": "Query Form",
1617
"rules": "Form Rules",
1718
"dynamic": "Dynamic Form",

playground/src/locales/langs/zh-CN/examples.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"form": {
1616
"title": "表单",
1717
"basic": "基础表单",
18+
"layout": "自定义布局",
1819
"query": "查询表单",
1920
"rules": "表单校验",
2021
"dynamic": "动态表单",

playground/src/router/routes/modules/examples.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ const routes: RouteRecordRaw[] = [
5353
title: $t('examples.form.dynamic'),
5454
},
5555
},
56+
{
57+
name: 'FormLayoutExample',
58+
path: '/examples/form/custom-layout',
59+
component: () => import('#/views/examples/form/custom-layout.vue'),
60+
meta: {
61+
title: $t('examples.form.layout'),
62+
},
63+
},
5664
{
5765
name: 'FormCustomExample',
5866
path: '/examples/form/custom',

playground/src/views/examples/form/basic.vue

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,14 @@ import { h, ref } from 'vue';
44
import { Page } from '@vben/common-ui';
55
66
import { useDebounceFn } from '@vueuse/core';
7-
import {
8-
Button,
9-
Card,
10-
message,
11-
Spin,
12-
TabPane,
13-
Tabs,
14-
Tag,
15-
} from 'ant-design-vue';
7+
import { Button, Card, message, Spin, Tag } from 'ant-design-vue';
168
import dayjs from 'dayjs';
179
1810
import { useVbenForm, z } from '#/adapter/form';
1911
import { getAllMenusApi } from '#/api';
2012
2113
import DocButton from '../doc-button.vue';
2214
23-
const activeTab = ref('basic');
2415
const keyword = ref('');
2516
const fetching = ref(false);
2617
// 模拟远程获取数据
@@ -339,75 +330,6 @@ const [BaseForm, baseFormApi] = useVbenForm({
339330
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
340331
});
341332
342-
const [CustomLayoutForm] = useVbenForm({
343-
// 所有表单项共用,可单独在表单内覆盖
344-
commonConfig: {
345-
// 所有表单项
346-
componentProps: {
347-
class: 'w-full',
348-
},
349-
},
350-
layout: 'horizontal',
351-
schema: [
352-
{
353-
component: 'Select',
354-
fieldName: 'field1',
355-
label: '字符串',
356-
},
357-
{
358-
component: 'TreeSelect',
359-
fieldName: 'field2',
360-
label: '字符串',
361-
},
362-
{
363-
component: 'Mentions',
364-
fieldName: 'field3',
365-
label: '字符串',
366-
},
367-
{
368-
component: 'Input',
369-
fieldName: 'field4',
370-
label: '字符串',
371-
},
372-
{
373-
component: 'InputNumber',
374-
fieldName: 'field5',
375-
// 从第三列开始 相当于中间空了一列
376-
formItemClass: 'col-start-3',
377-
label: '前面空了一列',
378-
},
379-
{
380-
component: 'Textarea',
381-
fieldName: 'field6',
382-
// 占满三列空间 基线对齐
383-
formItemClass: 'col-span-3 items-baseline',
384-
label: '占满三列',
385-
},
386-
{
387-
component: 'Input',
388-
fieldName: 'field7',
389-
// 占满2列空间 从第二列开始 相当于前面空了一列
390-
formItemClass: 'col-span-2 col-start-2',
391-
label: '占满2列',
392-
},
393-
{
394-
component: 'Input',
395-
fieldName: 'field8',
396-
// 左右留空
397-
formItemClass: 'col-start-2',
398-
label: '左右留空',
399-
},
400-
{
401-
component: 'InputPassword',
402-
fieldName: 'field9',
403-
formItemClass: 'col-start-1',
404-
label: '字符串',
405-
},
406-
],
407-
// 一共三列
408-
wrapperClass: 'grid-cols-3',
409-
});
410-
411333
function onSubmit(values: Record<string, any>) {
412334
message.success({
413335
content: `form values: ${JSON.stringify(values)}`,
@@ -443,7 +365,6 @@ function handleSetFormValue() {
443365
<Page
444366
content-class="flex flex-col gap-4"
445367
description="表单组件基础示例,请注意,该页面用到的参数代码会添加一些简单注释,方便理解,请仔细查看。"
446-
header-class="pb-0"
447368
title="表单组件"
448369
>
449370
<template #description>
@@ -452,22 +373,15 @@ function handleSetFormValue() {
452373
表单组件基础示例,请注意,该页面用到的参数代码会添加一些简单注释,方便理解,请仔细查看。
453374
</p>
454375
</div>
455-
<Tabs v-model:active-key="activeTab" :tab-bar-style="{ marginBottom: 0 }">
456-
<TabPane key="basic" tab="基础示例" />
457-
<TabPane key="layout" tab="自定义布局" />
458-
</Tabs>
459376
</template>
460377
<template #extra>
461378
<DocButton class="mb-2" path="/components/common-ui/vben-form" />
462379
</template>
463-
<Card v-show="activeTab === 'basic'" title="基础示例">
380+
<Card title="基础示例">
464381
<template #extra>
465382
<Button type="primary" @click="handleSetFormValue">设置表单值</Button>
466383
</template>
467384
<BaseForm />
468385
</Card>
469-
<Card v-show="activeTab === 'layout'" title="使用tailwind自定义布局">
470-
<CustomLayoutForm />
471-
</Card>
472386
</Page>
473387
</template>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<script lang="ts" setup>
2+
import { h } from 'vue';
3+
4+
import { Page } from '@vben/common-ui';
5+
6+
import { Card } from 'ant-design-vue';
7+
8+
import { useVbenForm } from '#/adapter/form';
9+
10+
import DocButton from '../doc-button.vue';
11+
12+
const [CustomLayoutForm] = useVbenForm({
13+
// 所有表单项共用,可单独在表单内覆盖
14+
commonConfig: {
15+
// 所有表单项
16+
componentProps: {
17+
class: 'w-full',
18+
},
19+
},
20+
layout: 'horizontal',
21+
schema: [
22+
{
23+
component: 'Select',
24+
fieldName: 'field1',
25+
label: '字符串',
26+
},
27+
{
28+
component: 'TreeSelect',
29+
fieldName: 'field2',
30+
label: '字符串',
31+
},
32+
{
33+
component: 'Mentions',
34+
fieldName: 'field3',
35+
label: '字符串',
36+
},
37+
{
38+
component: 'Input',
39+
fieldName: 'field4',
40+
label: '字符串',
41+
},
42+
{
43+
component: 'InputNumber',
44+
fieldName: 'field5',
45+
// 从第三列开始 相当于中间空了一列
46+
formItemClass: 'col-start-3',
47+
label: '前面空了一列',
48+
},
49+
{
50+
component: 'Divider',
51+
fieldName: '_divider',
52+
formItemClass: 'col-span-3',
53+
hideLabel: true,
54+
renderComponentContent: () => {
55+
return {
56+
default: () => h('div', '分割线'),
57+
};
58+
},
59+
},
60+
{
61+
component: 'Textarea',
62+
fieldName: 'field6',
63+
// 占满三列空间 基线对齐
64+
formItemClass: 'col-span-3 items-baseline',
65+
label: '占满三列',
66+
},
67+
{
68+
component: 'Input',
69+
fieldName: 'field7',
70+
// 占满2列空间 从第二列开始 相当于前面空了一列
71+
formItemClass: 'col-span-2 col-start-2',
72+
label: '占满2列',
73+
},
74+
{
75+
component: 'Input',
76+
fieldName: 'field8',
77+
// 左右留空
78+
formItemClass: 'col-start-2',
79+
label: '左右留空',
80+
},
81+
{
82+
component: 'InputPassword',
83+
fieldName: 'field9',
84+
formItemClass: 'col-start-1',
85+
label: '字符串',
86+
},
87+
],
88+
// 一共三列
89+
wrapperClass: 'grid-cols-3',
90+
});
91+
</script>
92+
93+
<template>
94+
<Page
95+
content-class="flex flex-col gap-4"
96+
description="使用tailwind自定义表单项的布局"
97+
title="表单自定义布局"
98+
>
99+
<template #description>
100+
<div class="text-muted-foreground">
101+
<p>使用tailwind自定义表单项的布局,使用Divider分割表单。</p>
102+
</div>
103+
</template>
104+
<template #extra>
105+
<DocButton class="mb-2" path="/components/common-ui/vben-form" />
106+
</template>
107+
<Card title="使用tailwind自定义布局">
108+
<CustomLayoutForm />
109+
</Card>
110+
</Page>
111+
</template>

playground/src/views/examples/form/rules.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ const [Form, formApi] = useVbenForm({
150150
default: () => ['我已阅读并同意'],
151151
};
152152
},
153-
rules: 'selectRequired',
153+
rules: z.boolean().refine((value) => value, {
154+
message: '请勾选',
155+
}),
154156
},
155157
{
156158
component: 'DatePicker',

0 commit comments

Comments
 (0)