mirror of https://github.com/ElemeFE/element
Select: fix slice of value when value is undefined or null (#15022)
parent
0e0a506193
commit
6ecf925d7e
|
@ -109,10 +109,10 @@
|
||||||
|
|
||||||
contains(arr = [], target) {
|
contains(arr = [], target) {
|
||||||
if (!this.isObject) {
|
if (!this.isObject) {
|
||||||
return arr.indexOf(target) > -1;
|
return arr && arr.indexOf(target) > -1;
|
||||||
} else {
|
} else {
|
||||||
const valueKey = this.select.valueKey;
|
const valueKey = this.select.valueKey;
|
||||||
return arr.some(item => {
|
return arr && arr.some(item => {
|
||||||
return getValueByPath(item, valueKey) === getValueByPath(target, valueKey);
|
return getValueByPath(item, valueKey) === getValueByPath(target, valueKey);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -346,7 +346,7 @@
|
||||||
value(val, oldVal) {
|
value(val, oldVal) {
|
||||||
if (this.multiple) {
|
if (this.multiple) {
|
||||||
this.resetInputHeight();
|
this.resetInputHeight();
|
||||||
if (val.length > 0 || (this.$refs.input && this.query !== '')) {
|
if ((val && val.length > 0) || (this.$refs.input && this.query !== '')) {
|
||||||
this.currentPlaceholder = '';
|
this.currentPlaceholder = '';
|
||||||
} else {
|
} else {
|
||||||
this.currentPlaceholder = this.cachedPlaceHolder;
|
this.currentPlaceholder = this.cachedPlaceHolder;
|
||||||
|
@ -671,7 +671,7 @@
|
||||||
|
|
||||||
handleOptionSelect(option, byClick) {
|
handleOptionSelect(option, byClick) {
|
||||||
if (this.multiple) {
|
if (this.multiple) {
|
||||||
const value = this.value.slice();
|
const value = (this.value || []).slice();
|
||||||
const optionIndex = this.getValueIndex(value, option.value);
|
const optionIndex = this.getValueIndex(value, option.value);
|
||||||
if (optionIndex > -1) {
|
if (optionIndex > -1) {
|
||||||
value.splice(optionIndex, 1);
|
value.splice(optionIndex, 1);
|
||||||
|
|
|
@ -843,6 +843,43 @@ describe('Select', () => {
|
||||||
expect(vm.value).to.be.equal('test');
|
expect(vm.value).to.be.equal('test');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('default value is null or undefined', async() => {
|
||||||
|
vm = createVue({
|
||||||
|
template: `
|
||||||
|
<div>
|
||||||
|
<el-select v-model="value">
|
||||||
|
<el-option
|
||||||
|
v-for="item in options"
|
||||||
|
:label="item.label"
|
||||||
|
:key="item.value"
|
||||||
|
:value="item.value">
|
||||||
|
</el-option>
|
||||||
|
</el-select>
|
||||||
|
</div>
|
||||||
|
`,
|
||||||
|
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
options: [{
|
||||||
|
value: '选项1',
|
||||||
|
label: '黄金糕'
|
||||||
|
}, {
|
||||||
|
value: '选项2',
|
||||||
|
label: '双皮奶'
|
||||||
|
}],
|
||||||
|
value: undefined
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
vm.value = null;
|
||||||
|
await waitImmediate();
|
||||||
|
expect(vm.$el.querySelector('.el-input__inner').value).to.equal('');
|
||||||
|
vm.value = '选项1';
|
||||||
|
await waitImmediate();
|
||||||
|
expect(vm.$el.querySelector('.el-input__inner').value).to.equal('黄金糕');
|
||||||
|
});
|
||||||
|
|
||||||
describe('resetInputHeight', () => {
|
describe('resetInputHeight', () => {
|
||||||
const getSelectComponentVm = (configs) => {
|
const getSelectComponentVm = (configs) => {
|
||||||
vm = getSelectVm(configs || {});
|
vm = getSelectVm(configs || {});
|
||||||
|
|
Loading…
Reference in New Issue