diff --git a/packages/datetime-picker/index.ts b/packages/datetime-picker/index.ts index 094a08eca..f5753822b 100644 --- a/packages/datetime-picker/index.ts +++ b/packages/datetime-picker/index.ts @@ -106,15 +106,28 @@ VantComponent({ }, methods: { + promiseSetData(data: any) { + return new Promise((resolve) => { + this.setData(data, resolve); + }); + }, + + isEqualForArray(oldData: any[], newData: any[]): boolean { + if (Array.isArray(oldData) && Array.isArray(newData)) { + return oldData.every((item, index) => item === newData[index]); + } + return false; + }, + updateValue() { const { data } = this; const val = this.correctValue(data.value); const isEqual = val === data.innerValue; - this.updateColumnValue(val).then(() => { - if (!isEqual) { + if (!isEqual) { + this.updateColumnValue(val).then(() => { this.$emit('input', val); - } - }); + }); + } }, getPicker() { @@ -136,7 +149,7 @@ VantComponent({ values: column.values.map((value) => formatter(column.type, value)), })); - return this.set({ columns: results }); + return this.promiseSetData({ columns: results }); }, getOriginColumns() { @@ -172,20 +185,10 @@ VantComponent({ ]; } - const { - maxYear, - maxDate, - maxMonth, - maxHour, - maxMinute, - } = this.getBoundary('max', data.innerValue); - const { - minYear, - minDate, - minMonth, - minHour, - minMinute, - } = this.getBoundary('min', data.innerValue); + const { maxYear, maxDate, maxMonth, maxHour, maxMinute } = + this.getBoundary('max', data.innerValue); + const { minYear, minDate, minMonth, minHour, minMinute } = + this.getBoundary('min', data.innerValue); const result = [ { @@ -355,9 +358,18 @@ VantComponent({ } } - return this.set({ innerValue: value }) + return this.promiseSetData({ innerValue: value }) .then(() => this.updateColumns()) - .then(() => picker.setValues(values)); + .then(() => { + const pickerValues = picker.getValues(); + const status = + pickerValues && this.isEqualForArray(pickerValues, values); + + if (status) { + return Promise.resolve(); + } + return picker.setValues(values); + }); }, }, diff --git a/packages/picker-column/index.ts b/packages/picker-column/index.ts index cdd4f6244..ca1d9664c 100644 --- a/packages/picker-column/index.ts +++ b/packages/picker-column/index.ts @@ -46,6 +46,12 @@ VantComponent({ }, methods: { + promiseSetData(data: any) { + return new Promise((resolve) => { + this.setData(data, resolve); + }); + }, + getCount() { return this.data.options.length; }, @@ -119,7 +125,7 @@ VantComponent({ const offset = -index * data.itemHeight; if (index !== data.currentIndex) { - return this.set({ offset, currentIndex: index }).then(() => { + return this.promiseSetData({ offset, currentIndex: index }).then(() => { userAction && this.$emit('change', index); }); }