Write.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <script setup lang="tsx">
  2. import { Form, FormSchema } from '@/components/Form'
  3. import { useForm } from '@/hooks/web/useForm'
  4. import { PropType, watch, ref } from 'vue'
  5. import { useValidator } from '@/hooks/web/useValidator'
  6. import { cloneDeep } from 'lodash-es'
  7. import { UpImgButtom } from '@/components/UpFile'
  8. import { getProductCategory } from '@/api/goods'
  9. const { required } = useValidator()
  10. const props = defineProps({
  11. currentRow: {
  12. type: Object as PropType<any>,
  13. default: () => null
  14. }
  15. })
  16. const formSchema = ref<FormSchema[]>([
  17. {
  18. field: 'pid',
  19. label: '父级',
  20. component: 'Select',
  21. colProps: {
  22. span: 24
  23. },
  24. value: 0,
  25. optionApi: async () => {
  26. const { data } = await getProductCategory({ pid: -1, limit: 9999, page: 1 })
  27. const list = [
  28. {
  29. label: '顶级',
  30. value: 0
  31. },
  32. ...data.map((data) => {
  33. return {
  34. value: data.id,
  35. label: data.name
  36. }
  37. })
  38. ]
  39. return list
  40. }
  41. },
  42. {
  43. field: 'name',
  44. label: '分类名称',
  45. component: 'Input',
  46. colProps: {
  47. span: 24
  48. },
  49. componentProps: {
  50. placeholder: '请输入分类名称'
  51. }
  52. },
  53. {
  54. field: 'image',
  55. label: '图标',
  56. value: [],
  57. component: 'CheckboxGroup',
  58. formItemProps: {
  59. slots: {
  60. default: (data) => {
  61. return <UpImgButtom v-model={data.image} />
  62. }
  63. }
  64. }
  65. },
  66. {
  67. field: 'sort',
  68. label: '排序',
  69. component: 'Input',
  70. colProps: {
  71. span: 24
  72. },
  73. componentProps: {
  74. type: 'number',
  75. placeholder: '请输入排序'
  76. }
  77. },
  78. {
  79. field: 'is_show',
  80. value: true,
  81. colProps: {
  82. span: 24
  83. },
  84. label: '是否显示',
  85. component: 'Switch'
  86. },
  87. {
  88. field: 'id',
  89. label: 'id',
  90. hidden: true,
  91. component: 'Input'
  92. }
  93. ])
  94. const rules = ref({
  95. name: [required('请填写分类名称')],
  96. default_price: [required('请填写一口价')]
  97. })
  98. const { formRegister, formMethods } = useForm()
  99. const { setValues, getFormData, getElFormExpose } = formMethods
  100. const submit = async () => {
  101. const elForm = await getElFormExpose()
  102. const valid = await elForm?.validate().catch((err) => {
  103. console.log(err)
  104. })
  105. if (valid) {
  106. const formData = await getFormData()
  107. return formData
  108. }
  109. }
  110. watch(
  111. () => props.currentRow,
  112. async (value) => {
  113. if (!value) return
  114. const currentRow = cloneDeep(value)
  115. setValues(currentRow)
  116. },
  117. {
  118. deep: true,
  119. immediate: true
  120. }
  121. )
  122. defineExpose({
  123. submit
  124. })
  125. </script>
  126. <template>
  127. <Form :rules="rules" @register="formRegister" :schema="formSchema" />
  128. </template>