basic.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <script lang="ts" setup>
  2. import { h } from 'vue';
  3. import { Page } from '@vben/common-ui';
  4. import { ElButton, ElCard, ElCheckbox, ElMessage } from 'element-plus';
  5. import { useVbenForm } from '#/adapter/form';
  6. import { getAllMenusApi } from '#/api';
  7. const [Form, formApi] = useVbenForm({
  8. commonConfig: {
  9. // 所有表单项
  10. componentProps: {
  11. class: 'w-full',
  12. },
  13. },
  14. layout: 'horizontal',
  15. // 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
  16. wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
  17. handleSubmit: (values) => {
  18. ElMessage.success(`表单数据:${JSON.stringify(values)}`);
  19. },
  20. schema: [
  21. {
  22. // 组件需要在 #/adapter.ts内注册,并加上类型
  23. component: 'ApiSelect',
  24. // 对应组件的参数
  25. componentProps: {
  26. // 菜单接口转options格式
  27. afterFetch: (data: { name: string; path: string }[]) => {
  28. return data.map((item: any) => ({
  29. label: item.name,
  30. value: item.path,
  31. }));
  32. },
  33. // 菜单接口
  34. api: getAllMenusApi,
  35. },
  36. // 字段名
  37. fieldName: 'api',
  38. // 界面显示的label
  39. label: 'ApiSelect',
  40. },
  41. {
  42. component: 'ApiTreeSelect',
  43. // 对应组件的参数
  44. componentProps: {
  45. // 菜单接口
  46. api: getAllMenusApi,
  47. childrenField: 'children',
  48. // 菜单接口转options格式
  49. labelField: 'name',
  50. valueField: 'path',
  51. },
  52. // 字段名
  53. fieldName: 'apiTree',
  54. // 界面显示的label
  55. label: 'ApiTreeSelect',
  56. },
  57. {
  58. component: 'Input',
  59. fieldName: 'string',
  60. label: 'String',
  61. },
  62. {
  63. component: 'InputNumber',
  64. fieldName: 'number',
  65. label: 'Number',
  66. },
  67. {
  68. component: 'RadioGroup',
  69. fieldName: 'radio',
  70. label: 'Radio',
  71. componentProps: {
  72. options: [
  73. { value: 'A', label: 'A' },
  74. { value: 'B', label: 'B' },
  75. { value: 'C', label: 'C' },
  76. { value: 'D', label: 'D' },
  77. { value: 'E', label: 'E' },
  78. ],
  79. },
  80. },
  81. {
  82. component: 'RadioGroup',
  83. fieldName: 'radioButton',
  84. label: 'RadioButton',
  85. componentProps: {
  86. isButton: true,
  87. options: ['A', 'B', 'C', 'D', 'E', 'F'].map((v) => ({
  88. value: v,
  89. label: `选项${v}`,
  90. })),
  91. },
  92. },
  93. {
  94. component: 'CheckboxGroup',
  95. fieldName: 'checkbox',
  96. label: 'Checkbox',
  97. componentProps: {
  98. options: ['A', 'B', 'C'].map((v) => ({ value: v, label: `选项${v}` })),
  99. },
  100. },
  101. {
  102. component: 'CheckboxGroup',
  103. fieldName: 'checkbox1',
  104. label: 'Checkbox1',
  105. renderComponentContent: () => {
  106. return {
  107. default: () => {
  108. return ['A', 'B', 'C', 'D'].map((v) =>
  109. h(ElCheckbox, { label: v, value: v }),
  110. );
  111. },
  112. };
  113. },
  114. },
  115. {
  116. component: 'CheckboxGroup',
  117. fieldName: 'checkbotton',
  118. label: 'CheckBotton',
  119. componentProps: {
  120. isButton: true,
  121. options: [
  122. { value: 'A', label: '选项A' },
  123. { value: 'B', label: '选项B' },
  124. { value: 'C', label: '选项C' },
  125. ],
  126. },
  127. },
  128. {
  129. component: 'DatePicker',
  130. fieldName: 'date',
  131. label: 'Date',
  132. },
  133. {
  134. component: 'Select',
  135. fieldName: 'select',
  136. label: 'Select',
  137. componentProps: {
  138. options: [
  139. { value: 'A', label: '选项A' },
  140. { value: 'B', label: '选项B' },
  141. { value: 'C', label: '选项C' },
  142. ],
  143. },
  144. },
  145. ],
  146. });
  147. function setFormValues() {
  148. formApi.setValues({
  149. string: 'string',
  150. number: 123,
  151. radio: 'B',
  152. radioButton: 'C',
  153. checkbox: ['A', 'C'],
  154. checkbotton: ['B', 'C'],
  155. checkbox1: ['A', 'B'],
  156. date: new Date(),
  157. select: 'B',
  158. });
  159. }
  160. </script>
  161. <template>
  162. <Page
  163. description="我们重新包装了CheckboxGroup、RadioGroup、Select,可以通过options属性传入选项属性数组以自动生成选项"
  164. title="表单演示"
  165. >
  166. <ElCard>
  167. <template #header>
  168. <div class="flex items-center">
  169. <span class="flex-auto">基础表单演示</span>
  170. <ElButton type="primary" @click="setFormValues">设置表单值</ElButton>
  171. </div>
  172. </template>
  173. <Form />
  174. </ElCard>
  175. </Page>
  176. </template>