Write.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <script setup lang="tsx">
  2. import { Form, FormSchema } from '@/components/Form'
  3. import { useForm } from '@/hooks/web/useForm'
  4. import { PropType, reactive, watch, ref, unref } from 'vue'
  5. import { useValidator } from '@/hooks/web/useValidator'
  6. import { useI18n } from '@/hooks/web/useI18n'
  7. import { ElTree, ElCheckboxGroup, ElCheckbox } from 'element-plus'
  8. import { getMenuListApi } from '@/api/menu'
  9. import { filter } from '@/utils/tree'
  10. const { t } = useI18n()
  11. const { required } = useValidator()
  12. const props = defineProps({
  13. currentRow: {
  14. type: Object as PropType<any>,
  15. default: () => null
  16. }
  17. })
  18. const treeRef = ref<typeof ElTree>()
  19. const formSchema = ref<FormSchema[]>([
  20. {
  21. field: 'roleName',
  22. label: t('role.roleName'),
  23. component: 'Input'
  24. },
  25. {
  26. field: 'role',
  27. label: t('role.role'),
  28. component: 'Input'
  29. },
  30. {
  31. field: 'status',
  32. label: t('menu.status'),
  33. component: 'Select',
  34. componentProps: {
  35. options: [
  36. {
  37. label: t('userDemo.disable'),
  38. value: 0
  39. },
  40. {
  41. label: t('userDemo.enable'),
  42. value: 1
  43. }
  44. ]
  45. }
  46. },
  47. {
  48. field: 'menu',
  49. label: t('role.menu'),
  50. colProps: {
  51. span: 24
  52. },
  53. formItemProps: {
  54. slots: {
  55. default: (formData: any) => {
  56. console.log(formData)
  57. return (
  58. <>
  59. <div class="flex w-full">
  60. <div class="flex-1">
  61. <ElTree
  62. ref={treeRef}
  63. show-checkbox
  64. node-key="id"
  65. highlight-current
  66. data={treeData.value}
  67. onNode-click={nodeClick}
  68. >
  69. {{
  70. default: (data) => {
  71. return <span>{data.data.meta.title}</span>
  72. }
  73. }}
  74. </ElTree>
  75. </div>
  76. <div class="flex-1">
  77. {unref(currentTreeData) && unref(currentTreeData)?.meta?.permission ? (
  78. <ElCheckboxGroup v-model={unref(currentTreeData).meta.currentPermission}>
  79. {unref(currentTreeData)?.meta?.permission.map((v: string) => {
  80. return <ElCheckbox label={v} />
  81. })}
  82. </ElCheckboxGroup>
  83. ) : null}
  84. </div>
  85. </div>
  86. </>
  87. )
  88. }
  89. }
  90. }
  91. }
  92. ])
  93. const currentTreeData = ref()
  94. const nodeClick = (treeData: any) => {
  95. currentTreeData.value = treeData
  96. }
  97. const rules = reactive({
  98. roleName: [required()],
  99. role: [required()],
  100. status: [required()]
  101. })
  102. const { formRegister, formMethods } = useForm()
  103. const { setValues, getFormData, getElFormExpose } = formMethods
  104. const treeData = ref([])
  105. const getMenuList = async () => {
  106. const res = await getMenuListApi()
  107. if (res) {
  108. treeData.value = res.data.list
  109. }
  110. }
  111. getMenuList()
  112. const submit = async () => {
  113. const elForm = await getElFormExpose()
  114. const valid = await elForm?.validate().catch((err) => {
  115. console.log(err)
  116. })
  117. if (valid) {
  118. const formData = await getFormData()
  119. const checkedKeys = [
  120. ...(unref(treeRef)?.getCheckedKeys() || []),
  121. ...(unref(treeRef)?.getHalfCheckedKeys() || [])
  122. ]
  123. const data = filter(unref(treeData), (item: any) => {
  124. return checkedKeys.includes(item.id)
  125. })
  126. formData.menu = data || []
  127. return formData
  128. }
  129. }
  130. watch(
  131. () => props.currentRow,
  132. (currentRow) => {
  133. if (!currentRow) return
  134. setValues(currentRow)
  135. },
  136. {
  137. deep: true,
  138. immediate: true
  139. }
  140. )
  141. defineExpose({
  142. submit
  143. })
  144. </script>
  145. <template>
  146. <Form :rules="rules" @register="formRegister" :schema="formSchema" />
  147. </template>