fix(Picker): should watch columns change (#5614)

This commit is contained in:
陈嘉涵 2020-02-01 15:34:12 +08:00
parent 6b71efd187
commit 633231f9b8
3 changed files with 46 additions and 6 deletions

View File

@ -73,6 +73,8 @@ export default createComponent({
}, },
watch: { watch: {
initialOptions: 'setOptions',
defaultIndex(val) { defaultIndex(val) {
this.setIndex(val); this.setIndex(val);
}, },
@ -89,6 +91,13 @@ export default createComponent({
}, },
methods: { methods: {
setOptions(options) {
if (JSON.stringify(options) !== JSON.stringify(this.options)) {
this.options = deepClone(options);
this.setIndex(this.defaultIndex);
}
},
onTouchStart(event) { onTouchStart(event) {
this.touchStart(event); this.touchStart(event);

View File

@ -182,12 +182,9 @@ export default createComponent({
// set options of column by index // set options of column by index
setColumnValues(index, options) { setColumnValues(index, options) {
const column = this.children[index]; const column = this.children[index];
if (
column && if (column) {
JSON.stringify(column.options) !== JSON.stringify(options) column.setOptions(options);
) {
column.options = options;
column.setIndex(0);
} }
}, },

View File

@ -262,3 +262,37 @@ test('cascade columns', () => {
'Gulou', 'Gulou',
]); ]);
}); });
test('watch columns change', () => {
const wrapper = mount(Picker, {
propsData: {
showToolbar: true,
columns: ['1', '2'],
defaultIndex: 1,
},
});
wrapper.setProps({
columns: ['2', '3'],
});
wrapper.find('.van-picker__confirm').trigger('click');
expect(wrapper.emitted('confirm')[0]).toEqual(['3', 1]);
});
test('should not reset index when columns unchanged', () => {
const wrapper = mount(Picker, {
propsData: {
showToolbar: true,
columns: ['1', '2'],
},
});
wrapper.vm.setIndexes([1]);
wrapper.setProps({
columns: ['1', '2'],
});
wrapper.find('.van-picker__confirm').trigger('click');
expect(wrapper.emitted('confirm')[0]).toEqual(['2', 1]);
});