Skip to content

Commit

Permalink
Select: fix slice of value when value is undefined or null (ElemeFE#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyCao authored and lzq4047 committed May 22, 2020
1 parent 71c34dc commit 801c1c0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/select/src/option.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@
contains(arr = [], target) {
if (!this.isObject) {
return arr.indexOf(target) > -1;
return arr && arr.indexOf(target) > -1;
} else {
const valueKey = this.select.valueKey;
return arr.some(item => {
return arr && arr.some(item => {
return getValueByPath(item, valueKey) === getValueByPath(target, valueKey);
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/select/src/select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@
value(val, oldVal) {
if (this.multiple) {
this.resetInputHeight();
if (val.length > 0 || (this.$refs.input && this.query !== '')) {
if ((val && val.length > 0) || (this.$refs.input && this.query !== '')) {
this.currentPlaceholder = '';
} else {
this.currentPlaceholder = this.cachedPlaceHolder;
Expand Down Expand Up @@ -671,7 +671,7 @@
handleOptionSelect(option, byClick) {
if (this.multiple) {
const value = this.value.slice();
const value = (this.value || []).slice();
const optionIndex = this.getValueIndex(value, option.value);
if (optionIndex > -1) {
value.splice(optionIndex, 1);
Expand Down
37 changes: 37 additions & 0 deletions test/unit/specs/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,43 @@ describe('Select', () => {
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', () => {
const getSelectComponentVm = (configs) => {
vm = getSelectVm(configs || {});
Expand Down

0 comments on commit 801c1c0

Please sign in to comment.