mirror of
https://gitee.com/ElemeFE/element.git
synced 2024-12-03 20:58:09 +08:00
Select: add default-first-option
default-first-option: boolean * if set to `true`, select first matching option on enter * only works if `filterable` or `remote` is `true` test is included
This commit is contained in:
parent
ac571ce6d8
commit
e2e31790da
@ -191,7 +191,8 @@
|
||||
default() {
|
||||
return t('el.select.placeholder');
|
||||
}
|
||||
}
|
||||
},
|
||||
defaultFirstOption: Boolean
|
||||
},
|
||||
|
||||
data() {
|
||||
@ -264,6 +265,25 @@
|
||||
this.broadcast('ElOption', 'queryChange', val);
|
||||
this.broadcast('ElOptionGroup', 'queryChange');
|
||||
}
|
||||
if (this.defaultFirstOption && (this.filterable || this.remote) && this.filteredOptionsCount) {
|
||||
this.hoverIndex = -1;
|
||||
for (let i = 0; i !== this.options.length; ++i) {
|
||||
const option = this.options[i];
|
||||
if (val) {
|
||||
// pick first options that passes the filter
|
||||
if (!option.disabled && !option.groupDisabled && option.visible) {
|
||||
this.hoverIndex = i;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// pick currently selected option
|
||||
if (option.itemSelected) {
|
||||
this.hoverIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
visible(val) {
|
||||
|
@ -404,6 +404,49 @@ describe('Select', () => {
|
||||
}, 10);
|
||||
});
|
||||
|
||||
it('default-first-option', done => {
|
||||
vm = createVue({
|
||||
template: `
|
||||
<div>
|
||||
<el-select
|
||||
v-model="value"
|
||||
default-first-option
|
||||
filterable
|
||||
>
|
||||
<el-option
|
||||
v-for="item in options"
|
||||
:label="item"
|
||||
:value="item"
|
||||
/>
|
||||
</el-select>
|
||||
</div>
|
||||
`,
|
||||
data() {
|
||||
return {
|
||||
options: ['1', '2', '3', '4', '5'],
|
||||
value: ''
|
||||
};
|
||||
}
|
||||
}, true);
|
||||
|
||||
const select = vm.$children[0];
|
||||
setTimeout(() => {
|
||||
select.$el.querySelector('input').focus();
|
||||
select.query = '3';
|
||||
select.selectedLabel = '3';
|
||||
setTimeout(() => {
|
||||
const enterKey = document.createEvent('Events');
|
||||
enterKey.initEvent('keydown', true, true);
|
||||
enterKey.keyCode = 13;
|
||||
select.$el.querySelector('input').dispatchEvent(enterKey);
|
||||
setTimeout(() => {
|
||||
expect(select.value).to.equal('3');
|
||||
done();
|
||||
}, 10);
|
||||
}, 10);
|
||||
}, 10);
|
||||
});
|
||||
|
||||
it('allow create', done => {
|
||||
vm = getSelectVm({ filterable: true, allowCreate: true });
|
||||
const select = vm.$children[0];
|
||||
|
Loading…
Reference in New Issue
Block a user