[bugfix] Checkbox: should not prevent click event

This commit is contained in:
陈嘉涵 2019-05-07 15:41:34 +08:00
parent 6d0b5e5a0d
commit ecadb5d1e7
3 changed files with 18 additions and 6 deletions

View File

@ -30,7 +30,15 @@ export default sfc({
methods: { methods: {
toggle() { toggle() {
this.checked = !this.checked; const checked = !this.checked;
// When toggle method is called multiple times at the same time,
// only the last call is valid.
// This is a hack for usage inside Cell.
clearTimeout(this.toggleTask);
this.toggleTask = setTimeout(() => {
this.checked = checked;
});
}, },
onClickIcon() { onClickIcon() {

View File

@ -1,8 +1,8 @@
import Checkbox from '..'; import Checkbox from '..';
import CheckboxGroup from '../../checkbox-group'; import CheckboxGroup from '../../checkbox-group';
import { mount } from '../../../test/utils'; import { mount, later } from '../../../test/utils';
test('switch checkbox', () => { test('switch checkbox', async () => {
const wrapper = mount(Checkbox); const wrapper = mount(Checkbox);
wrapper.vm.$on('input', value => { wrapper.vm.$on('input', value => {
@ -11,8 +11,9 @@ test('switch checkbox', () => {
const icon = wrapper.find('.van-checkbox__icon'); const icon = wrapper.find('.van-checkbox__icon');
icon.trigger('click'); icon.trigger('click');
await later();
icon.trigger('click'); icon.trigger('click');
await later();
expect(wrapper.emitted('input')).toEqual([[true], [false]]); expect(wrapper.emitted('input')).toEqual([[true], [false]]);
expect(wrapper.emitted('change')).toEqual([[true], [false]]); expect(wrapper.emitted('change')).toEqual([[true], [false]]);
}); });
@ -42,7 +43,7 @@ test('label disabled', () => {
expect(wrapper.emitted('input')).toBeFalsy(); expect(wrapper.emitted('input')).toBeFalsy();
}); });
test('checkbox group', () => { test('checkbox group', async () => {
const wrapper = mount({ const wrapper = mount({
template: ` template: `
<checkbox-group v-model="result" :max="2"> <checkbox-group v-model="result" :max="2">
@ -63,11 +64,15 @@ test('checkbox group', () => {
const icons = wrapper.findAll('.van-checkbox__icon'); const icons = wrapper.findAll('.van-checkbox__icon');
icons.at(0).trigger('click'); icons.at(0).trigger('click');
await later();
icons.at(1).trigger('click'); icons.at(1).trigger('click');
await later();
icons.at(2).trigger('click'); icons.at(2).trigger('click');
expect(wrapper.vm.result).toEqual(['a', 'b']); expect(wrapper.vm.result).toEqual(['a', 'b']);
await later();
icons.at(0).trigger('click'); icons.at(0).trigger('click');
await later();
expect(wrapper.vm.result).toEqual(['b']); expect(wrapper.vm.result).toEqual(['b']);
}); });

View File

@ -60,7 +60,6 @@ export const CheckboxMixin = (parent, bem) => ({
<div <div
class={bem()} class={bem()}
onClick={event => { onClick={event => {
event.stopPropagation();
this.$emit('click', event); this.$emit('click', event);
}} }}
> >