AppendModal.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <script setup lang="ts">
  2. import { ref, reactive, toRaw, watch } from "vue";
  3. import type { FormInstance, FormRules } from "element-plus";
  4. import _ from "lodash";
  5. import type { TreeData } from "./Tree";
  6. const props = defineProps<{ contextData: TreeData; appendLoading: boolean }>();
  7. const show = defineModel<boolean>("show");
  8. const emit = defineEmits(["ok", "cancel"]);
  9. const form$ = ref<FormInstance>();
  10. const formData = reactive({
  11. isDir: false,
  12. filename: "",
  13. });
  14. const rules = reactive<FormRules<typeof formData>>({
  15. filename: [
  16. {
  17. validator: (rule, value, callback, source, options) => {
  18. if (
  19. _.includes(
  20. props.contextData.children?.map((c) => c.key),
  21. `${props.contextData.key ? `${props.contextData.key}/` : ""}${value}`
  22. )
  23. ) {
  24. callback("文件重名");
  25. }
  26. callback();
  27. },
  28. trigger: "change",
  29. },
  30. {
  31. pattern: /[^\\/:"*?<>|]/, message: '有非法字符: \\/:"*?<>|', trigger: 'change'
  32. }
  33. ],
  34. });
  35. const onOk = async () => {
  36. try {
  37. const result = await form$.value?.validate();
  38. if (result) {
  39. emit("ok", _.cloneDeep(toRaw(formData)));
  40. }
  41. } catch (e) {
  42. console.warn(e);
  43. }
  44. };
  45. const onCancel = () => {
  46. emit("cancel");
  47. show.value = false;
  48. };
  49. watch(
  50. () => show.value,
  51. () => {
  52. if (!show.value) {
  53. form$.value?.resetFields();
  54. }
  55. }
  56. );
  57. </script>
  58. <template>
  59. <el-dialog v-model="show">
  60. <el-form ref="form$" :model="formData" :rules="rules">
  61. <el-form-item label="是否文件夹" prop="isDir">
  62. <el-switch v-model="formData.isDir" inline-prompt></el-switch>
  63. </el-form-item>
  64. <el-form-item :label="formData.isDir ? '文件夹名' : '文件名'" prop="filename">
  65. <el-input v-model="formData.filename"> </el-input>
  66. </el-form-item>
  67. <el-form-item>
  68. <el-button
  69. :disabled="!formData.filename"
  70. :loading="appendLoading"
  71. @click="onOk"
  72. type="primary"
  73. >ok</el-button
  74. >
  75. <el-button @click="onCancel">cancel</el-button>
  76. </el-form-item>
  77. </el-form>
  78. </el-dialog>
  79. </template>