EditForm.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <script setup lang="ts">
  2. import { ElDialog, ElForm, ElFormItem, ElInput, ElDatePicker, ElButton, ElMessage, ElRadio } from 'element-plus'
  3. import type { FormInstance, FormRules } from 'element-plus'
  4. // 定义表单数据类型
  5. type IFormData = {
  6. text: string
  7. }
  8. // 定义组件的属性
  9. interface Props {
  10. visible: boolean
  11. modelValue?: IFormData | null
  12. mode?: 'create' | 'edit'
  13. }
  14. // 定义组件的事件
  15. interface Emits {
  16. (e: 'update:visible', value: boolean): void
  17. (e: 'cancel'): void
  18. (e: 'refresh'): void
  19. }
  20. // 接收属性和事件
  21. const props = withDefaults(defineProps<Props>(), {
  22. visible: false,
  23. modelValue: null,
  24. mode: 'create'
  25. })
  26. const emit = defineEmits<Emits>()
  27. // 创建本地响应式变量用于 v-model 绑定
  28. const dialogVisible = ref(props.visible)
  29. // 监听 props.visible 变化,更新本地变量
  30. watch(() => props.visible, (newVisible) => {
  31. dialogVisible.value = newVisible
  32. })
  33. // 监听本地变量变化,发出 update 事件
  34. watch(dialogVisible, (newVisible) => {
  35. emit('update:visible', newVisible)
  36. })
  37. // 表单引用
  38. const editFormRef = ref<FormInstance>();
  39. // 响应式数据 - 直接定义所有必需字段
  40. const formData = ref<Partial<IFormData>>({
  41. text: '',
  42. });
  43. // 监听 props.modelValue 变化
  44. watch(() => props.modelValue, (newModelValue) => {
  45. if (newModelValue) {
  46. formData.value = {
  47. ...formData.value,
  48. ...newModelValue
  49. };
  50. }
  51. });
  52. // 表单验证规则
  53. const formRules = ref<FormRules>({
  54. text: [
  55. { required: true, message: '输入名称', trigger: 'blur' },
  56. ],
  57. })
  58. // 监听可见性变化
  59. watch(() => props.visible, (newVisible) => {
  60. resetForm()
  61. })
  62. // 重置表单
  63. function resetForm() {
  64. console.log('reset')
  65. if (editFormRef.value) {
  66. editFormRef.value.resetFields()
  67. }
  68. formData.value = {
  69. text: '',
  70. }
  71. }
  72. // 处理确认
  73. async function handleConfirm() {
  74. if (!editFormRef.value) return
  75. try {
  76. await editFormRef.value.validate()
  77. // const { code } = await updateCallings({
  78. // cloneId: props.cloneId,
  79. // callings: formData.value.text || ''
  80. // })
  81. // if (code === 0) {
  82. // ElMessage.success('操作成功')
  83. // emit('refresh')
  84. // }
  85. // 关闭对话框
  86. emit('update:visible', false)
  87. } catch (error) {
  88. // 验证失败,不做处理
  89. }
  90. }
  91. function handleCancel() {
  92. emit('cancel')
  93. emit('update:visible', false)
  94. }
  95. function handleClose() {
  96. emit('update:visible', false)
  97. }
  98. </script>
  99. <template>
  100. <div>
  101. <ElDialog title="编辑提示词" v-model="dialogVisible" align-center @close="handleClose"
  102. width="400" :z-index="2000">
  103. <ElForm ref="editFormRef" :model="formData" :rules="formRules" label-width="120px" class="mt-4 space-y-4 w-full">
  104. <ElFormItem label="提示词" prop="text" label-width="120">
  105. <ElInput type="text" v-model="formData.text" />
  106. </ElFormItem>
  107. </ElForm>
  108. <template #footer>
  109. <div class="flex justify-end space-x-2">
  110. <ElButton @click="handleCancel">取消</ElButton>
  111. <ElButton type="primary" @click="handleConfirm">确定</ElButton>
  112. </div>
  113. </template>
  114. </ElDialog>
  115. </div>
  116. </template>