浏览代码

Select: fix an illegal filter regexp bug

Leopoldthecoder 8 年之前
父节点
当前提交
aa27888a58
共有 2 个文件被更改,包括 8 次插入6 次删除
  1. 6 4
      packages/select/src/option.vue
  2. 2 2
      packages/select/src/select.vue

+ 6 - 4
packages/select/src/option.vue

@@ -3,7 +3,7 @@
     @mouseenter="hoverItem"
     @click.stop="selectOptionClick"
     class="el-select-dropdown__item"
-    v-show="queryPassed"
+    v-show="visible"
     :class="{ 'selected': itemSelected, 'is-disabled': disabled || groupDisabled, 'hover': parent.hoverIndex === index }">
     <slot>
       <span>{{ currentLabel }}</span>
@@ -40,7 +40,7 @@
       return {
         index: -1,
         groupDisabled: false,
-        queryPassed: true,
+        visible: true,
         hitState: false
       };
     },
@@ -97,8 +97,10 @@
       },
 
       queryChange(query) {
-        this.queryPassed = new RegExp(query, 'i').test(this.currentLabel);
-        if (!this.queryPassed) {
+        // query 里如果有正则中的特殊字符,需要先将这些字符转义
+        let parsedQuery = query.replace(/(\^|\(|\)|\[|\]|\$|\*|\+|\.|\?|\\|\{|\}|\|)/g, '\\$1');
+        this.visible = new RegExp(parsedQuery, 'i').test(this.currentLabel);
+        if (!this.visible) {
           this.parent.filteredOptionsCount--;
         }
       },

+ 2 - 2
packages/select/src/select.vue

@@ -444,7 +444,7 @@
             this.resetScrollTop();
             if (this.options[this.hoverIndex].disabled === true ||
               this.options[this.hoverIndex].groupDisabled === true ||
-              !this.options[this.hoverIndex].queryPassed) {
+              !this.options[this.hoverIndex].visible) {
               this.navigateOptions('next');
             }
           }
@@ -456,7 +456,7 @@
             this.resetScrollTop();
             if (this.options[this.hoverIndex].disabled === true ||
               this.options[this.hoverIndex].groupDisabled === true ||
-              !this.options[this.hoverIndex].queryPassed) {
+              !this.options[this.hoverIndex].visible) {
               this.navigateOptions('prev');
             }
           }