Browse Source

优化参照组件,满足用户只输入不选择情况下清空输入值

002390 1 year ago
parent
commit
272d48ee87
2 changed files with 60 additions and 1 deletions
  1. 29 0
      src/components/popover-select-v2/index.vue
  2. 31 1
      src/components/popover-select/index.vue

+ 29 - 0
src/components/popover-select-v2/index.vue

@@ -224,6 +224,33 @@ export default {
       const { referName } = this.$props;
       this.$emit("copyChange", this.innerValue.split(/,|,|\s+/));
     },
+    useBlur() {
+      const {
+        $props: { source, dataMapping },
+      } = this;
+
+      for (let key in dataMapping) {
+        if (dataMapping[key] === "id" || dataMapping[key] === "code") {
+          if (!source[key] || source[key] === "") {
+            this.innerValue = "";
+          }
+        }
+      }
+    },
+    handleKeyupDel() {
+      console.log("focus");
+      const {
+        $props: { source, dataMapping },
+      } = this;
+
+      for (let key in dataMapping) {
+        if (dataMapping[key] === "id" || dataMapping[key] === "code") {
+          if (source[key] && source[key] !== "") {
+            source[key] = undefined;
+          }
+        }
+      }
+    },
     handleClear() {
       this.innerValue = "";
     },
@@ -255,7 +282,9 @@ export default {
       v-model="innerValue"
       :value-key="valueKey"
       :fetch-suggestions="useAutocomplete"
+      @blur="useBlur"
       @select="useConfirm([$event])"
+      @keyup.delete.native="handleKeyupDel"
       style="width: 100%"
     >
       <i class="el-icon-search" slot="suffix" @click="open"> </i>

+ 31 - 1
src/components/popover-select/index.vue

@@ -298,6 +298,34 @@ export default {
       const { type, source } = this.$props;
       this.$emit("copyChange", this.innerValue.split(/,|,|\s+/));
     },
+    useBlur() {
+      const {
+        $props: { source, dataMapping, multiple },
+      } = this;
+      if (!multiple) {
+        for (let key in dataMapping) {
+          if (dataMapping[key] === "id" || dataMapping[key] === "code") {
+            if (!source[key] || source[key] === "") {
+              this.innerValue = "";
+            }
+          }
+        }
+      }
+    },
+    handleKeyupDel() {
+      console.log("focus");
+      const {
+        $props: { source, dataMapping },
+      } = this;
+
+      for (let key in dataMapping) {
+        if (dataMapping[key] === "id" || dataMapping[key] === "code") {
+          if (source[key] && source[key] !== "") {
+            source[key] = undefined;
+          }
+        }
+      }
+    },
   },
   created() {},
   mounted() {},
@@ -326,10 +354,12 @@ export default {
       v-bind="$attrs"
       v-model="innerValue"
       :size="size"
+      :disabled="disabled"
       :value-key="valueKey"
       :fetch-suggestions="useAutocomplete"
+      @blur="useBlur"
       @select="useConfirm([$event])"
-      :disabled="disabled"
+      @keyup.delete.native="handleKeyupDel"
       style="width: 100%"
     >
       <i :size="size" class="el-icon-search" slot="suffix" @click="open"> </i>