From 9c55056b0ac5c81d854f36798be024926638060c Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Tue, 17 Mar 2020 19:52:55 +0800 Subject: [PATCH] fix(Picker): update cascade after setColumnValue (#5807) --- src/picker/index.js | 9 ++++- src/picker/test/cascade.spec.js | 59 +++++++++++++++++++++++++++++++++ src/picker/test/index.spec.js | 32 ------------------ 3 files changed, 67 insertions(+), 33 deletions(-) create mode 100644 src/picker/test/cascade.spec.js diff --git a/src/picker/index.js b/src/picker/index.js index 180dd149f..07d88de91 100644 --- a/src/picker/index.js +++ b/src/picker/index.js @@ -156,7 +156,14 @@ export default createComponent({ // set column value by index setColumnValue(index, value) { const column = this.getColumn(index); - column && column.setValue(value); + + if (column) { + column.setValue(value); + + if (this.dataType === 'cascade') { + this.onCascadeChange(index); + } + } }, // @exposed-api diff --git a/src/picker/test/cascade.spec.js b/src/picker/test/cascade.spec.js new file mode 100644 index 000000000..02bd3966d --- /dev/null +++ b/src/picker/test/cascade.spec.js @@ -0,0 +1,59 @@ +import Picker from '..'; +import { mount, triggerDrag } from '../../../test'; +import { cascadeColumns } from '../demo/data'; + +test('cascade columns', () => { + const wrapper = mount(Picker, { + propsData: { + showToolbar: true, + columns: cascadeColumns['en-US'], + }, + }); + + wrapper.find('.van-picker__confirm').trigger('click'); + expect(wrapper.emitted('confirm')[0][0]).toEqual([ + 'Zhejiang', + 'Hangzhou', + 'Xihu', + ]); + + triggerDrag(wrapper.find('.van-picker-column'), 0, -100); + wrapper.find('.van-picker-column ul').trigger('transitionend'); + expect(wrapper.emitted('change')[0][1]).toEqual([ + 'Fujian', + 'Fuzhou', + 'Gulou', + ]); + + wrapper.find('.van-picker__confirm').trigger('click'); + expect(wrapper.emitted('confirm')[1][0]).toEqual([ + 'Fujian', + 'Fuzhou', + 'Gulou', + ]); +}); + +test('setColumnValue of cascade columns', () => { + const wrapper = mount(Picker, { + propsData: { + showToolbar: true, + columns: cascadeColumns['en-US'], + }, + }); + + wrapper.vm.setColumnValue(0, 'Fujian'); + wrapper.find('.van-picker__confirm').trigger('click'); + expect(wrapper.emitted('confirm')[0][0]).toEqual([ + 'Fujian', + 'Fuzhou', + 'Gulou', + ]); + + wrapper.vm.setColumnValue(1, 'Xiamen'); + wrapper.find('.van-picker__confirm').trigger('click'); + expect(wrapper.emitted('confirm')[1][0]).toEqual([ + 'Fujian', + 'Xiamen', + 'Siming', + ]); +}); diff --git a/src/picker/test/index.spec.js b/src/picker/test/index.spec.js index 542644f96..6e7664d2f 100644 --- a/src/picker/test/index.spec.js +++ b/src/picker/test/index.spec.js @@ -1,6 +1,5 @@ import Picker from '..'; import PickerColumn from '../PickerColumn'; -import { cascadeColumns } from '../demo/data'; import { mount, triggerDrag, later } from '../../../test'; const simpleColumn = ['1990', '1991', '1992', '1993', '1994', '1995']; @@ -232,37 +231,6 @@ test('columns-top、columns-bottom prop', () => { expect(wrapper).toMatchSnapshot(); }); -test('cascade columns', () => { - const wrapper = mount(Picker, { - propsData: { - showToolbar: true, - columns: cascadeColumns['en-US'], - }, - }); - - wrapper.find('.van-picker__confirm').trigger('click'); - expect(wrapper.emitted('confirm')[0][0]).toEqual([ - 'Zhejiang', - 'Hangzhou', - 'Xihu', - ]); - - triggerDrag(wrapper.find('.van-picker-column'), 0, -100); - wrapper.find('.van-picker-column ul').trigger('transitionend'); - expect(wrapper.emitted('change')[0][1]).toEqual([ - 'Fujian', - 'Fuzhou', - 'Gulou', - ]); - - wrapper.find('.van-picker__confirm').trigger('click'); - expect(wrapper.emitted('confirm')[1][0]).toEqual([ - 'Fujian', - 'Fuzhou', - 'Gulou', - ]); -}); - test('watch columns change', () => { const wrapper = mount(Picker, { propsData: {