basic.vue 4.2 KB

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