mirror of
https://gitee.com/ElemeFE/element.git
synced 2024-12-04 21:27:52 +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() {
|
default() {
|
||||||
return t('el.select.placeholder');
|
return t('el.select.placeholder');
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
defaultFirstOption: Boolean
|
||||||
},
|
},
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
@ -264,6 +265,25 @@
|
|||||||
this.broadcast('ElOption', 'queryChange', val);
|
this.broadcast('ElOption', 'queryChange', val);
|
||||||
this.broadcast('ElOptionGroup', 'queryChange');
|
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) {
|
visible(val) {
|
||||||
|
@ -404,6 +404,49 @@ describe('Select', () => {
|
|||||||
}, 10);
|
}, 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 => {
|
it('allow create', done => {
|
||||||
vm = getSelectVm({ filterable: true, allowCreate: true });
|
vm = getSelectVm({ filterable: true, allowCreate: true });
|
||||||
const select = vm.$children[0];
|
const select = vm.$children[0];
|
||||||
|
Loading…
Reference in New Issue
Block a user