Write.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <script setup lang="ts">
  2. import { Form, FormSchema } from '@/components/Form'
  3. import { useForm } from '@/hooks/web/useForm'
  4. import { PropType, reactive, watch } from 'vue'
  5. import { useValidator } from '@/hooks/web/useValidator'
  6. import { useI18n } from '@/hooks/web/useI18n'
  7. const { t } = useI18n()
  8. const { required } = useValidator()
  9. const props = defineProps({
  10. currentRow: {
  11. type: Object as PropType<any>,
  12. default: () => null
  13. },
  14. treeSelectData: Array
  15. })
  16. const formSchema = reactive<FormSchema[]>([
  17. {
  18. field: 'type',
  19. label: '类型',
  20. component: 'RadioGroup',
  21. hidden: true,
  22. value: 0,
  23. colProps: {
  24. span: 24
  25. },
  26. componentProps: {
  27. options: [
  28. {
  29. label: '目录',
  30. value: 0
  31. },
  32. {
  33. label: '菜单',
  34. value: 1
  35. },
  36. {
  37. label: '按钮',
  38. value: 2
  39. }
  40. ]
  41. }
  42. },
  43. {
  44. field: 'pid',
  45. label: '上级菜单',
  46. component: 'TreeSelect',
  47. componentProps: {
  48. checkStrictly: true,
  49. nodeKey: 'id',
  50. props: {
  51. label: 'name'
  52. },
  53. data: props.treeSelectData
  54. }
  55. },
  56. {
  57. field: 'url',
  58. label: '路由地址',
  59. component: 'Input'
  60. },
  61. {
  62. field: 'redirect',
  63. label: '重定向路由',
  64. component: 'Input'
  65. },
  66. {
  67. field: 'orderNum',
  68. label: '排序',
  69. component: 'InputNumber'
  70. },
  71. {
  72. field: 'meta.title',
  73. label: t('menu.menuName'),
  74. component: 'Input'
  75. },
  76. {
  77. field: 'component',
  78. label: t('menu.component'),
  79. component: 'Input'
  80. },
  81. {
  82. field: 'name',
  83. label: t('menu.name'),
  84. component: 'Input'
  85. },
  86. {
  87. field: 'meta.icon',
  88. label: t('menu.icon'),
  89. component: 'IconPicker'
  90. },
  91. {
  92. field: 'status',
  93. label: t('menu.status'),
  94. component: 'Select',
  95. componentProps: {
  96. options: [
  97. {
  98. label: t('userDemo.disable'),
  99. value: 0
  100. },
  101. {
  102. label: t('userDemo.enable'),
  103. value: 1
  104. }
  105. ]
  106. }
  107. },
  108. {
  109. field: 'permissions',
  110. label: '授权标识',
  111. component: 'Input'
  112. },
  113. {
  114. field: 'meta.activeMenu',
  115. label: t('menu.activeMenu'),
  116. component: 'Input'
  117. },
  118. // {
  119. // field: 'permission',
  120. // label: t('menu.permission'),
  121. // component: 'CheckboxGroup',
  122. // componentProps: {
  123. // options: [
  124. // {
  125. // label: 'add',
  126. // value: 'add'
  127. // },
  128. // {
  129. // label: 'edit',
  130. // value: 'edit'
  131. // },
  132. // {
  133. // label: 'delete',
  134. // value: 'delete'
  135. // }
  136. // ]
  137. // }
  138. // },
  139. {
  140. field: 'meta.hidden',
  141. label: t('menu.hidden'),
  142. component: 'Switch'
  143. },
  144. {
  145. field: 'meta.alwaysShow',
  146. label: t('menu.alwaysShow'),
  147. component: 'Switch'
  148. },
  149. {
  150. field: 'meta.noCache',
  151. label: t('menu.noCache'),
  152. component: 'Switch'
  153. },
  154. {
  155. field: 'meta.breadcrumb',
  156. label: t('menu.breadcrumb'),
  157. component: 'Switch'
  158. },
  159. {
  160. field: 'meta.affix',
  161. label: t('menu.affix'),
  162. component: 'Switch'
  163. },
  164. {
  165. field: 'meta.noTagsView',
  166. label: t('menu.noTagsView'),
  167. component: 'Switch'
  168. },
  169. {
  170. field: 'canTo',
  171. label: t('menu.canTo'),
  172. component: 'Switch'
  173. }
  174. ])
  175. const rules = reactive({
  176. name: [required()],
  177. url: [required()]
  178. })
  179. const { formRegister, formMethods } = useForm()
  180. const { setValues, getFormData, getElFormExpose } = formMethods
  181. const submit = async () => {
  182. const elForm = await getElFormExpose()
  183. const valid = await elForm?.validate().catch((err) => {
  184. console.log(err)
  185. })
  186. if (valid) {
  187. const formData = await getFormData()
  188. return formData
  189. }
  190. }
  191. watch(
  192. () => props.currentRow,
  193. (currentRow) => {
  194. if (!currentRow) return
  195. let form = {
  196. ...currentRow
  197. }
  198. setValues(form)
  199. },
  200. {
  201. deep: true,
  202. immediate: true
  203. }
  204. )
  205. defineExpose({
  206. submit
  207. })
  208. </script>
  209. <template>
  210. <Form :rules="rules" @register="formRegister" :schema="formSchema" />
  211. </template>