gohttpdUi/src/views/Example/Dialog/components/Write.vue

64 lines
1.3 KiB
Vue
Raw Normal View History

2022-02-11 17:17:29 +08:00
<script setup lang="ts">
2023-07-23 15:15:28 +08:00
import { Form, FormSchema } from '@/components/Form'
2022-02-11 17:17:29 +08:00
import { useForm } from '@/hooks/web/useForm'
2022-02-19 15:36:18 +08:00
import { PropType, reactive, watch } from 'vue'
2022-02-11 17:17:29 +08:00
import { TableData } from '@/api/table/types'
2022-06-25 21:56:24 +08:00
import { useValidator } from '@/hooks/web/useValidator'
const { required } = useValidator()
2022-02-11 17:17:29 +08:00
const props = defineProps({
currentRow: {
type: Object as PropType<Nullable<TableData>>,
default: () => null
},
2022-04-26 15:03:48 +08:00
formSchema: {
type: Array as PropType<FormSchema[]>,
default: () => []
2022-02-11 17:17:29 +08:00
}
2022-04-26 15:03:48 +08:00
})
2022-02-11 17:17:29 +08:00
const rules = reactive({
2022-06-25 21:56:24 +08:00
title: [required()],
author: [required()],
importance: [required()],
pageviews: [required()],
display_time: [required()],
content: [required()]
2022-02-11 17:17:29 +08:00
})
2023-07-23 15:15:28 +08:00
const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods
const submit = async () => {
const elForm = await getElFormExpose()
const valid = await elForm?.validate().catch((err) => {
console.log(err)
})
if (valid) {
const formData = getFormData()
return formData
}
}
2022-02-11 17:17:29 +08:00
2022-02-19 15:36:18 +08:00
watch(
() => props.currentRow,
(currentRow) => {
if (!currentRow) return
setValues(currentRow)
},
{
deep: true,
immediate: true
}
)
2022-02-11 17:17:29 +08:00
defineExpose({
2023-07-23 15:15:28 +08:00
submit
2022-02-11 17:17:29 +08:00
})
</script>
<template>
2023-07-23 15:15:28 +08:00
<Form :rules="rules" @register="formRegister" :schema="formSchema" />
2022-02-11 17:17:29 +08:00
</template>