1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
| <script lang="ts" setup>
| import { Page } from '@vben/common-ui';
|
| import { NButton, NCard, useMessage } from 'naive-ui';
|
| import { useVbenForm } from '#/adapter/form';
| import { getAllMenusApi } from '#/api';
|
| const message = useMessage();
| const [Form, formApi] = useVbenForm({
| commonConfig: {
| // 所有表单项
| componentProps: {
| class: 'w-full',
| },
| },
| layout: 'horizontal',
| // 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
| wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
| handleSubmit: (values) => {
| message.success(`表单数据:${JSON.stringify(values)}`);
| },
| schema: [
| {
| // 组件需要在 #/adapter.ts内注册,并加上类型
| component: 'ApiSelect',
| // 对应组件的参数
| componentProps: {
| // 菜单接口转options格式
| afterFetch: (data: { name: string; path: string }[]) => {
| return data.map((item: any) => ({
| label: item.name,
| value: item.path,
| }));
| },
| // 菜单接口
| api: getAllMenusApi,
| },
| // 字段名
| fieldName: 'api',
| // 界面显示的label
| label: 'ApiSelect',
| },
| {
| component: 'ApiTreeSelect',
| // 对应组件的参数
| componentProps: {
| // 菜单接口
| api: getAllMenusApi,
| childrenField: 'children',
| // 菜单接口转options格式
| labelField: 'name',
| valueField: 'path',
| },
| // 字段名
| fieldName: 'apiTree',
| // 界面显示的label
| label: 'ApiTreeSelect',
| },
| {
| component: 'Input',
| fieldName: 'string',
| label: 'String',
| },
| {
| component: 'InputNumber',
| fieldName: 'number',
| label: 'Number',
| },
| {
| component: 'RadioGroup',
| fieldName: 'radio',
| label: 'Radio',
| componentProps: {
| options: [
| { value: 'A', label: 'A' },
| { value: 'B', label: 'B' },
| { value: 'C', label: 'C' },
| { value: 'D', label: 'D' },
| { value: 'E', label: 'E' },
| ],
| },
| },
| {
| component: 'RadioGroup',
| fieldName: 'radioButton',
| label: 'RadioButton',
| componentProps: {
| isButton: true,
| class: 'flex flex-wrap', // 如果选项过多,可以添加class来自动折叠
| options: [
| { value: 'A', label: '选项A' },
| { value: 'B', label: '选项B' },
| { value: 'C', label: '选项C' },
| { value: 'D', label: '选项D' },
| { value: 'E', label: '选项E' },
| { value: 'F', label: '选项F' },
| ],
| },
| },
| {
| component: 'CheckboxGroup',
| fieldName: 'checkbox',
| label: 'Checkbox',
| componentProps: {
| options: [
| { value: 'A', label: '选项A' },
| { value: 'B', label: '选项B' },
| { value: 'C', label: '选项C' },
| ],
| },
| },
| {
| component: 'DatePicker',
| fieldName: 'date',
| label: 'Date',
| },
| ],
| });
| function setFormValues() {
| formApi.setValues({
| string: 'string',
| number: 123,
| radio: 'B',
| radioButton: 'C',
| checkbox: ['A', 'C'],
| date: Date.now(),
| });
| }
| </script>
| <template>
| <Page
| description="表单适配器重新包装了CheckboxGroup和RadioGroup,可以通过options属性传递选项数据(选项数据将作为子组件的属性)"
| title="表单演示"
| >
| <NCard title="基础表单">
| <template #header-extra>
| <NButton type="primary" @click="setFormValues">设置表单值</NButton>
| </template>
| <Form />
| </NCard>
| </Page>
| </template>
|
|