Write.vue 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <script setup lang="ts">
  2. import { Form } from '@/components/Form'
  3. import { useForm } from '@/hooks/web/useForm'
  4. import { PropType, reactive, watch } from 'vue'
  5. import { TableData } from '@/api/table/types'
  6. import { useValidator } from '@/hooks/web/useValidator'
  7. import { FormSchema } from '@/types/form'
  8. const { required } = useValidator()
  9. const props = defineProps({
  10. currentRow: {
  11. type: Object as PropType<Nullable<TableData>>,
  12. default: () => null
  13. },
  14. formSchema: {
  15. type: Array as PropType<FormSchema[]>,
  16. default: () => []
  17. }
  18. })
  19. const rules = reactive({
  20. title: [required()],
  21. author: [required()],
  22. importance: [required()],
  23. pageviews: [required()],
  24. display_time: [required()],
  25. content: [required()]
  26. })
  27. const { register, methods, elFormRef } = useForm({
  28. schema: props.formSchema
  29. })
  30. watch(
  31. () => props.currentRow,
  32. (currentRow) => {
  33. if (!currentRow) return
  34. const { setValues } = methods
  35. setValues(currentRow)
  36. },
  37. {
  38. deep: true,
  39. immediate: true
  40. }
  41. )
  42. defineExpose({
  43. elFormRef,
  44. getFormData: methods.getFormData
  45. })
  46. </script>
  47. <template>
  48. <Form :rules="rules" @register="register" />
  49. </template>