Write.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 { DictData } 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<DictData>,
  11. default: () => undefined
  12. },
  13. formSchema: {
  14. type: Array as PropType<FormSchema[]>,
  15. default: () => []
  16. }
  17. })
  18. const rules = reactive({
  19. code: [required()]
  20. })
  21. const { formRegister, formMethods } = useForm()
  22. const { setValues, getFormData, getElFormExpose } = 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. watch(
  34. () => props.currentRow,
  35. (currentRow) => {
  36. if (!currentRow) return
  37. setValues(currentRow)
  38. },
  39. {
  40. deep: true,
  41. immediate: true
  42. }
  43. )
  44. defineExpose({
  45. submit
  46. })
  47. </script>
  48. <template>
  49. <Form :rules="rules" @register="formRegister" :schema="formSchema" />
  50. </template>