From 934078bf1801f74f98a79c25c832b2792b316c89 Mon Sep 17 00:00:00 2001 From: 刘汉宸 Date: Fri, 15 Jan 2021 10:55:21 +0800 Subject: [PATCH] fix: 修复Select组件逻辑异常 --- packages/select/index.vue | 63 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/packages/select/index.vue b/packages/select/index.vue index 6b2c1f9..c6e549c 100644 --- a/packages/select/index.vue +++ b/packages/select/index.vue @@ -104,6 +104,10 @@ export default { }, update: Boolean, updateOnce: Boolean, + beforeQuery: { + type: Function, + default: () => true, + }, }, data() { return { @@ -127,7 +131,7 @@ export default { }, options(val) { if (val) { - this.optionsCurrent = this.fixOptions([]); + this.optionsCurrent = this.fixOptions(this.optionsDataSource); } }, }, @@ -207,35 +211,42 @@ export default { // 远程加载 remoteMethod(val = '') { const searchText = val.trim(); - if (searchText.length >= this.triggerSize) { - this.loading = true; - let requestPrimise; - if (this.queryApi) { - requestPrimise = this.queryApi(searchText); + const isQueryValid = this.beforeQuery(searchText); + if (isQueryValid) { + if (searchText.length >= this.triggerSize) { + this.loading = true; + let requestPrimise; + if (this.queryApi) { + requestPrimise = this.queryApi(searchText); + } else { + requestPrimise = this.request({ + url: this.url, + method: 'get', + params: searchText ? { [this.searchKey]: searchText, ...this.params } : this.params, + ...this.config, + }); + } + requestPrimise + .then(res => { + const response = res || {}; + const options = this.fixOptions(response.result); + this.optionsDataSource = options; + this.optionsCurrent = options; + }) + .finally(() => { + this.loading = false; + this.initing = false; + this.loaded = true; + }); } else { - requestPrimise = this.request({ - url: this.url, - method: 'get', - params: searchText ? { [this.searchKey]: searchText, ...this.params } : this.params, - ...this.config, + this.optionsCurrent = this.optionsDataSource.filter(item => { + return item[this.labelKey].includes(val); }); } - requestPrimise - .then(res => { - const response = res || {}; - const options = this.fixOptions(response.result); - this.optionsDataSource = options; - this.optionsCurrent = options; - }) - .finally(() => { - this.loading = false; - this.initing = false; - this.loaded = true; - }); } else { - this.optionsCurrent = this.optionsDataSource.filter(item => { - return item[this.labelKey].includes(val); - }); + this.loading = false; + this.initing = false; + this.loaded = true; } }, }, -- libgit2 0.21.0