mirror of https://github.com/ElemeFE/element
Cascader: fix invalid zero value (#3470)
parent
7e67a3b5db
commit
f19cc4c7ab
|
@ -1,4 +1,6 @@
|
||||||
<script>
|
<script>
|
||||||
|
import { isDef } from 'element-ui/src/utils/shared';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'ElCascaderMenu',
|
name: 'ElCascaderMenu',
|
||||||
|
|
||||||
|
@ -54,7 +56,7 @@
|
||||||
const level = activeOptions.length;
|
const level = activeOptions.length;
|
||||||
activeOptions[level] = options;
|
activeOptions[level] = options;
|
||||||
let active = activeValue[level];
|
let active = activeValue[level];
|
||||||
if (active) {
|
if (isDef(active)) {
|
||||||
options = options.filter(option => option.value === active)[0];
|
options = options.filter(option => option.value === active)[0];
|
||||||
if (options && options.children) {
|
if (options && options.children) {
|
||||||
loadActiveOptions(options.children, activeOptions);
|
loadActiveOptions(options.children, activeOptions);
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
export function isDef(val) {
|
||||||
|
return val !== undefined && val !== null;
|
||||||
|
}
|
|
@ -96,6 +96,97 @@ describe('Cascader', () => {
|
||||||
});
|
});
|
||||||
}, 300);
|
}, 300);
|
||||||
});
|
});
|
||||||
|
// Github issue #3470
|
||||||
|
it('should work with zero', done => {
|
||||||
|
vm = createVue({
|
||||||
|
template: `
|
||||||
|
<el-cascader
|
||||||
|
ref="cascader"
|
||||||
|
placeholder="请选择"
|
||||||
|
:options="options"
|
||||||
|
clearable
|
||||||
|
v-model="selectedOptions"
|
||||||
|
></el-cascader>
|
||||||
|
`,
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
options: [{
|
||||||
|
value: 'zhejiang',
|
||||||
|
label: 'Zhejiang',
|
||||||
|
children: [{
|
||||||
|
value: 0,
|
||||||
|
label: 'Hangzhou',
|
||||||
|
children: [{
|
||||||
|
value: 'xihu',
|
||||||
|
label: 'West Lake'
|
||||||
|
}]
|
||||||
|
}, {
|
||||||
|
value: 'ningbo',
|
||||||
|
label: 'NingBo',
|
||||||
|
children: [{
|
||||||
|
value: 'jiangbei',
|
||||||
|
label: 'Jiang Bei'
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
}, {
|
||||||
|
value: 'jiangsu',
|
||||||
|
label: 'Jiangsu',
|
||||||
|
children: [{
|
||||||
|
value: 'nanjing',
|
||||||
|
label: 'Nanjing',
|
||||||
|
children: [{
|
||||||
|
value: 'zhonghuamen',
|
||||||
|
label: 'Zhong Hua Men'
|
||||||
|
}]
|
||||||
|
}]
|
||||||
|
}],
|
||||||
|
selectedOptions: []
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
expect(vm.$el).to.be.exist;
|
||||||
|
vm.$el.click();
|
||||||
|
setTimeout(_ => {
|
||||||
|
expect(document.body.querySelector('.el-cascader-menus')).to.be.exist;
|
||||||
|
|
||||||
|
const menu = vm.$refs.cascader.menu;
|
||||||
|
const menuElm = menu.$el;
|
||||||
|
const item1 = menuElm.querySelector('.el-cascader-menu__item');
|
||||||
|
|
||||||
|
item1.click();
|
||||||
|
menu.$nextTick(_ => {
|
||||||
|
expect(menuElm.children.length).to.be.equal(2);
|
||||||
|
expect(item1.classList.contains('is-active')).to.be.true;
|
||||||
|
|
||||||
|
const item2 = menuElm.children[1].querySelector('.el-cascader-menu__item');
|
||||||
|
item2.click();
|
||||||
|
|
||||||
|
menu.$nextTick(_ => {
|
||||||
|
expect(menuElm.children.length).to.be.equal(3);
|
||||||
|
expect(item2.classList.contains('is-active')).to.be.true;
|
||||||
|
|
||||||
|
const item3 = menuElm.children[2].querySelector('.el-cascader-menu__item');
|
||||||
|
item3.click();
|
||||||
|
|
||||||
|
setTimeout(_ => {
|
||||||
|
expect(document.body.querySelector('.el-cascader-menus').style.display).to.be.equal('none');
|
||||||
|
expect(vm.selectedOptions[0]).to.be.equal('zhejiang');
|
||||||
|
expect(vm.selectedOptions[1]).to.be.equal(0);
|
||||||
|
expect(vm.selectedOptions[2]).to.be.equal('xihu');
|
||||||
|
|
||||||
|
triggerEvent(vm.$refs.cascader.$el, 'mouseenter');
|
||||||
|
vm.$nextTick(_ => {
|
||||||
|
vm.$refs.cascader.$el.querySelector('.el-cascader__clearIcon').click();
|
||||||
|
vm.$nextTick(_ => {
|
||||||
|
expect(vm.selectedOptions.length).to.be.equal(0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, 500);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, 300);
|
||||||
|
});
|
||||||
it('not allow clearable', done => {
|
it('not allow clearable', done => {
|
||||||
vm = createVue({
|
vm = createVue({
|
||||||
template: `
|
template: `
|
||||||
|
|
Loading…
Reference in New Issue