|
@@ -5,8 +5,8 @@
|
|
|
:placeholder="placeholder"
|
|
|
:value="value"
|
|
|
:clearable="clearable"
|
|
|
- @focus="preSearch.focus = true"
|
|
|
- @blur="preSearch.focus = false"
|
|
|
+ @focus="focus"
|
|
|
+ @blur="blur"
|
|
|
@input="input"
|
|
|
@keyup.enter.native="onSearch"
|
|
|
>
|
|
@@ -29,6 +29,12 @@
|
|
|
>
|
|
|
<slot name="preSearchContent"></slot>
|
|
|
</div>
|
|
|
+ <div
|
|
|
+ class="search-history-list"
|
|
|
+ v-show="historyShow"
|
|
|
+ >
|
|
|
+ <slot name="history"></slot>
|
|
|
+ </div>
|
|
|
</div>
|
|
|
</template>
|
|
|
|
|
@@ -51,6 +57,10 @@ export default {
|
|
|
perSearchEnabled: {
|
|
|
type: Boolean,
|
|
|
default: false
|
|
|
+ },
|
|
|
+ historyEnabled: {
|
|
|
+ type: Boolean,
|
|
|
+ default: false
|
|
|
}
|
|
|
},
|
|
|
model: {
|
|
@@ -72,6 +82,9 @@ export default {
|
|
|
this.perSearchEnabled &&
|
|
|
(this.preSearch.focus || this.preSearch.hover)
|
|
|
)
|
|
|
+ },
|
|
|
+ historyShow() {
|
|
|
+ return !this.value && this.preSearch.focus && this.historyEnabled
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
@@ -80,6 +93,19 @@ export default {
|
|
|
},
|
|
|
onSearch() {
|
|
|
this.$emit('onSearch', this.value)
|
|
|
+ },
|
|
|
+ focus(e) {
|
|
|
+ this.preSearch.focus = true
|
|
|
+ this.$emit('focus', e)
|
|
|
+ },
|
|
|
+ blur(e) {
|
|
|
+ // 由于JavaScript为单线程,同一时间只能执行处理一个事件,“blur优先于click执行”,下拉框内的click事件将会执行不了
|
|
|
+ // 解决方案1:对blur事件进行延迟,让click先执行
|
|
|
+ // 方案2:用mousedown事件替换click事件(鼠标事件优先于blur事件)
|
|
|
+ setTimeout(() => {
|
|
|
+ this.preSearch.focus = false
|
|
|
+ this.$emit('blur', e)
|
|
|
+ }, 300)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -162,4 +188,19 @@ export default {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+.search-history-list{
|
|
|
+ padding: 20px 0;
|
|
|
+ position: absolute;
|
|
|
+ z-index: 6;
|
|
|
+ top: 42px;
|
|
|
+ width: 100%;
|
|
|
+ max-height: 520px;
|
|
|
+ background: #fff;
|
|
|
+ box-shadow: 0 0 20px rgb(0, 0, 0, 0.1);
|
|
|
+ border-radius: 8px;
|
|
|
+ overflow-y: auto;
|
|
|
+ &::-webkit-scrollbar{
|
|
|
+ width: 4px;
|
|
|
+ }
|
|
|
+}
|
|
|
</style>
|