DialogForm.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <script setup lang="ts">
  2. import { ref, watch } from "vue";
  3. import { message } from "@/utils/message";
  4. import { FormInstance } from "element-plus";
  5. const SELECT_OPTIONS = [
  6. { label: "网关", value: 1 },
  7. { label: "人工智能", value: 2 },
  8. { label: "CVM", value: 3 },
  9. { label: "防火墙", value: 4 },
  10. { label: "未知", value: 5 }
  11. ];
  12. const props = defineProps({
  13. visible: {
  14. type: Boolean,
  15. default: false
  16. },
  17. data: {
  18. type: Object,
  19. default: () => {
  20. return {};
  21. }
  22. }
  23. });
  24. const ruleFormRef = ref<FormInstance>();
  25. const formVisible = ref(false);
  26. const formData = ref(props.data);
  27. const textareaValue = ref("");
  28. const submitForm = async (formEl: FormInstance | undefined) => {
  29. if (!formEl) return;
  30. await formEl.validate(valid => {
  31. if (valid) {
  32. message("提交成功", { type: "success" });
  33. formVisible.value = false;
  34. resetForm(formEl);
  35. }
  36. });
  37. };
  38. const resetForm = (formEl: FormInstance | undefined) => {
  39. if (!formEl) return;
  40. formEl.resetFields();
  41. };
  42. const closeDialog = () => {
  43. formVisible.value = false;
  44. resetForm(ruleFormRef.value);
  45. };
  46. const emit = defineEmits(["update:visible"]);
  47. watch(
  48. () => formVisible.value,
  49. val => {
  50. emit("update:visible", val);
  51. }
  52. );
  53. watch(
  54. () => props.visible,
  55. val => {
  56. formVisible.value = val;
  57. }
  58. );
  59. watch(
  60. () => props.data,
  61. val => {
  62. formData.value = val;
  63. }
  64. );
  65. const rules = {
  66. name: [{ required: true, message: "请输入产品名称", trigger: "blur" }]
  67. };
  68. </script>
  69. <template>
  70. <el-dialog
  71. v-model="formVisible"
  72. title="新建产品"
  73. :width="680"
  74. draggable
  75. :before-close="closeDialog"
  76. >
  77. <!-- 表单内容 -->
  78. <el-form
  79. ref="ruleFormRef"
  80. :model="formData"
  81. :rules="rules"
  82. label-width="100px"
  83. >
  84. <el-form-item label="产品名称" prop="name">
  85. <el-input
  86. v-model="formData.name"
  87. :style="{ width: '480px' }"
  88. placeholder="请输入产品名称"
  89. />
  90. </el-form-item>
  91. <el-form-item label="产品状态" prop="status">
  92. <el-radio-group v-model="formData.status">
  93. <el-radio label="0">已停用</el-radio>
  94. <el-radio label="1">已启用</el-radio>
  95. </el-radio-group>
  96. </el-form-item>
  97. <el-form-item label="产品描述" prop="description">
  98. <el-input
  99. v-model="formData.description"
  100. :style="{ width: '480px' }"
  101. placeholder="请输入产品描述"
  102. />
  103. </el-form-item>
  104. <el-form-item label="产品类型" prop="type">
  105. <el-select
  106. v-model="formData.type"
  107. clearable
  108. :style="{ width: '480px' }"
  109. >
  110. <el-option
  111. v-for="(item, index) in SELECT_OPTIONS"
  112. :key="index"
  113. :value="item.value"
  114. :label="item.label"
  115. >
  116. {{ item.label }}
  117. </el-option>
  118. </el-select>
  119. </el-form-item>
  120. <el-form-item label="备注" prop="mark">
  121. <el-input
  122. v-model="textareaValue"
  123. type="textarea"
  124. :style="{ width: '480px' }"
  125. placeholder="请输入内容"
  126. />
  127. </el-form-item>
  128. </el-form>
  129. <template #footer>
  130. <el-button @click="closeDialog">取消</el-button>
  131. <el-button type="primary" @click="submitForm(ruleFormRef)">
  132. 确定
  133. </el-button>
  134. </template>
  135. </el-dialog>
  136. </template>