fix(Sidebar): should emit change when v-model changed (#6383)

This commit is contained in:
neverland 2020-05-26 19:44:59 +08:00 committed by GitHub
parent 126f2124a2
commit 67a5a8d76c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 8 deletions

View File

@ -31,7 +31,7 @@ export default createComponent({
this.$emit('click', this.index);
this.parent.$emit('input', this.index);
this.parent.$emit('change', this.index);
this.parent.setIndex(this.index);
route(this.$router, this);
},
},

View File

@ -17,6 +17,27 @@ export default createComponent({
},
},
data() {
return {
index: +this.activeKey,
};
},
watch: {
activeKey() {
this.setIndex(+this.activeKey);
},
},
methods: {
setIndex(index) {
if (index !== this.index) {
this.index = index;
this.$emit('change', index);
}
},
},
render() {
return <div class={bem()}>{this.slots()}</div>;
},

View File

@ -7,6 +7,7 @@ test('click event & change event', () => {
const wrapper = mount({
template: `
<van-sidebar @change="onChange">
<van-sidebar-item>Text</van-sidebar-item>
<van-sidebar-item @click="onClick">Text</van-sidebar-item>
</van-sidebar>
`,
@ -16,16 +17,17 @@ test('click event & change event', () => {
},
});
wrapper.find('.van-sidebar-item').trigger('click');
expect(onClick).toHaveBeenCalledWith(0);
expect(onChange).toHaveBeenCalledWith(0);
wrapper.findAll('.van-sidebar-item').at(1).trigger('click');
expect(onClick).toHaveBeenCalledWith(1);
expect(onChange).toHaveBeenCalledWith(1);
wrapper.vm.$destroy();
});
test('v-model', () => {
const onChange = jest.fn();
const wrapper = mount({
template: `
<van-sidebar v-model="active">
<van-sidebar v-model="active" @change="onChange">
<van-sidebar-item>Text</van-sidebar-item>
<van-sidebar-item>Text</van-sidebar-item>
</van-sidebar>
@ -35,10 +37,14 @@ test('v-model', () => {
active: 0,
};
},
methods: {
onChange,
},
});
wrapper.findAll('.van-sidebar-item').at(1).trigger('click');
expect(wrapper.vm.active).toEqual(1);
expect(onChange).toHaveBeenCalledWith(1);
});
test('disabled prop', () => {

View File

@ -20,6 +20,10 @@ const mockItems = [
text: 'group1',
children: [mockItem],
},
{
text: 'group2',
children: [mockItem],
},
];
test('click-nav event', () => {
@ -39,10 +43,10 @@ test('click-nav event', () => {
});
const navItems = wrapper.findAll('.van-tree-select__nav-item');
navItems.at(0).trigger('click');
navItems.at(1).trigger('click');
expect(onNavClick).toHaveBeenCalledWith(0);
expect(onClickNav).toHaveBeenCalledWith(0);
expect(onNavClick).toHaveBeenCalledWith(1);
expect(onClickNav).toHaveBeenCalledWith(1);
});
test('click-item event', () => {