Optimize append and remove options
This commit is contained in:
parent
17db7df4fd
commit
9c32a05699
3 changed files with 30 additions and 29 deletions
|
@ -50,6 +50,7 @@
|
||||||
import { oneOf, findComponentDownward } from '../../utils/assist';
|
import { oneOf, findComponentDownward } from '../../utils/assist';
|
||||||
import Emitter from '../../mixins/emitter';
|
import Emitter from '../../mixins/emitter';
|
||||||
import Locale from '../../mixins/locale';
|
import Locale from '../../mixins/locale';
|
||||||
|
import {debounce} from './utils';
|
||||||
|
|
||||||
const prefixCls = 'ivu-select';
|
const prefixCls = 'ivu-select';
|
||||||
|
|
||||||
|
@ -623,6 +624,18 @@
|
||||||
this.broadcast('iOption', 'on-query-change', val);
|
this.broadcast('iOption', 'on-query-change', val);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
debouncedAppendRemove: debounce(function(){
|
||||||
|
if (!this.remote) {
|
||||||
|
this.modelToQuery();
|
||||||
|
this.$nextTick(() => this.broadcastQuery(''));
|
||||||
|
} else {
|
||||||
|
this.findChild((child) => {
|
||||||
|
child.selected = this.multiple ? this.model.indexOf(child.value) > -1 : this.model === child.value;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.slotChange();
|
||||||
|
this.updateOptions(true, true);
|
||||||
|
}),
|
||||||
// 处理 remote 初始值
|
// 处理 remote 初始值
|
||||||
updateLabel () {
|
updateLabel () {
|
||||||
if (this.remote) {
|
if (this.remote) {
|
||||||
|
@ -656,34 +669,8 @@
|
||||||
this.updateOptions(true);
|
this.updateOptions(true);
|
||||||
document.addEventListener('keydown', this.handleKeydown);
|
document.addEventListener('keydown', this.handleKeydown);
|
||||||
|
|
||||||
this.$on('append', () => {
|
this.$on('append', this.debouncedAppendRemove);
|
||||||
if (!this.remote) {
|
this.$on('remove', this.debouncedAppendRemove);
|
||||||
this.modelToQuery();
|
|
||||||
this.$nextTick(() => {
|
|
||||||
this.broadcastQuery('');
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.findChild(child => {
|
|
||||||
child.selected = this.multiple ? this.model.indexOf(child.value) > -1 : this.model === child.value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
this.slotChange();
|
|
||||||
this.updateOptions(true, true);
|
|
||||||
});
|
|
||||||
this.$on('remove', () => {
|
|
||||||
if (!this.remote) {
|
|
||||||
this.modelToQuery();
|
|
||||||
this.$nextTick(() => {
|
|
||||||
this.broadcastQuery('');
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.findChild(child => {
|
|
||||||
child.selected = this.multiple ? this.model.indexOf(child.value) > -1 : this.model === child.value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
this.slotChange();
|
|
||||||
this.updateOptions(true, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.$on('on-select-selected', (value) => {
|
this.$on('on-select-selected', (value) => {
|
||||||
if (this.model === value) {
|
if (this.model === value) {
|
||||||
|
|
14
src/components/select/utils.js
Normal file
14
src/components/select/utils.js
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
export function debounce(fn) {
|
||||||
|
let waiting;
|
||||||
|
return function() {
|
||||||
|
if (waiting) return;
|
||||||
|
waiting = true;
|
||||||
|
const context = this,
|
||||||
|
args = arguments;
|
||||||
|
const later = function() {
|
||||||
|
waiting = false;
|
||||||
|
fn.apply(context, args);
|
||||||
|
};
|
||||||
|
this.$nextTick(later);
|
||||||
|
};
|
||||||
|
}
|
|
@ -156,7 +156,7 @@ describe('Select.vue', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Performance tests', () => {
|
describe('Performance tests', () => {
|
||||||
xit('should handle big numbers of options', done => {
|
it('should handle big numbers of options', done => {
|
||||||
const manyLaterOptions = Array.apply(null, Array(200)).map((_, i) => {
|
const manyLaterOptions = Array.apply(null, Array(200)).map((_, i) => {
|
||||||
return {
|
return {
|
||||||
value: i + 1,
|
value: i + 1,
|
||||||
|
|
Loading…
Add table
Reference in a new issue