Write.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 { ImgData } from '@/api/manage/types'
  6. import { useValidator } from '@/hooks/web/useValidator'
  7. const { required } = useValidator()
  8. const props = defineProps({
  9. currentRow: {
  10. type: Object as PropType<ImgData>,
  11. default: () => undefined
  12. },
  13. formSchema: {
  14. type: Array as PropType<FormSchema[]>,
  15. default: () => []
  16. }
  17. })
  18. const rules = reactive({
  19. bannerUrl: [required()]
  20. })
  21. const { formRegister, formMethods } = useForm()
  22. const { setValues, getFormData, getElFormExpose, setSchema } = formMethods
  23. const submit = async () => {
  24. const elForm = await getElFormExpose()
  25. const valid = await elForm?.validate().catch((err) => {
  26. console.log(err)
  27. })
  28. if (valid) {
  29. const formData = await getFormData()
  30. return formData
  31. }
  32. }
  33. const resetForm = async () => {
  34. const elForm = await getElFormExpose()
  35. elForm?.resetFields()
  36. }
  37. watch(
  38. () => props.currentRow,
  39. (currentRow) => {
  40. if (!currentRow) return
  41. setValues(currentRow)
  42. },
  43. {
  44. deep: true,
  45. immediate: true
  46. }
  47. )
  48. defineExpose({
  49. submit,
  50. setSchema,
  51. setValues,
  52. resetForm
  53. })
  54. </script>
  55. <template>
  56. <Form :rules="rules" @register="formRegister" :schema="formSchema" />
  57. </template>