| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <script setup lang="ts">
- import { ElDialog, ElForm, ElFormItem, ElInput, ElDatePicker, ElButton, ElMessage, ElRadio } from 'element-plus'
- import type { FormInstance, FormRules } from 'element-plus'
- // 定义表单数据类型
- type IFormData = {
- text: string
- }
- // 定义组件的属性
- interface Props {
- visible: boolean
- modelValue?: IFormData | null
- mode?: 'create' | 'edit'
- }
- // 定义组件的事件
- interface Emits {
- (e: 'update:visible', value: boolean): void
- (e: 'cancel'): void
- (e: 'refresh'): void
- }
- // 接收属性和事件
- const props = withDefaults(defineProps<Props>(), {
- visible: false,
- modelValue: null,
- mode: 'create'
- })
- const emit = defineEmits<Emits>()
- // 创建本地响应式变量用于 v-model 绑定
- const dialogVisible = ref(props.visible)
- // 监听 props.visible 变化,更新本地变量
- watch(() => props.visible, (newVisible) => {
- dialogVisible.value = newVisible
- })
- // 监听本地变量变化,发出 update 事件
- watch(dialogVisible, (newVisible) => {
- emit('update:visible', newVisible)
- })
- // 表单引用
- const editFormRef = ref<FormInstance>();
- // 响应式数据 - 直接定义所有必需字段
- const formData = ref<Partial<IFormData>>({
- text: '',
- });
- // 监听 props.modelValue 变化
- watch(() => props.modelValue, (newModelValue) => {
- if (newModelValue) {
- formData.value = {
- ...formData.value,
- ...newModelValue
- };
- }
- });
- // 表单验证规则
- const formRules = ref<FormRules>({
- text: [
- { required: true, message: '输入名称', trigger: 'blur' },
- ],
- })
- // 监听可见性变化
- watch(() => props.visible, (newVisible) => {
- resetForm()
- })
- // 重置表单
- function resetForm() {
- console.log('reset')
- if (editFormRef.value) {
- editFormRef.value.resetFields()
- }
- formData.value = {
- text: '',
- }
- }
- // 处理确认
- async function handleConfirm() {
- if (!editFormRef.value) return
- try {
- await editFormRef.value.validate()
- // const { code } = await updateCallings({
- // cloneId: props.cloneId,
- // callings: formData.value.text || ''
- // })
- // if (code === 0) {
- // ElMessage.success('操作成功')
- // emit('refresh')
- // }
- // 关闭对话框
- emit('update:visible', false)
- } catch (error) {
- // 验证失败,不做处理
- }
- }
- function handleCancel() {
- emit('cancel')
- emit('update:visible', false)
- }
- function handleClose() {
- emit('update:visible', false)
- }
- </script>
- <template>
- <div>
- <ElDialog title="编辑提示词" v-model="dialogVisible" align-center @close="handleClose"
- width="400" :z-index="2000">
- <ElForm ref="editFormRef" :model="formData" :rules="formRules" label-width="120px" class="mt-4 space-y-4 w-full">
- <ElFormItem label="提示词" prop="text" label-width="120">
- <ElInput type="text" v-model="formData.text" />
- </ElFormItem>
- </ElForm>
- <template #footer>
- <div class="flex justify-end space-x-2">
- <ElButton @click="handleCancel">取消</ElButton>
- <ElButton type="primary" @click="handleConfirm">确定</ElButton>
- </div>
- </template>
- </ElDialog>
- </div>
- </template>
|