From 0aac165d4ebbf05d87894f86c9e1cf4d92d8a9a3 Mon Sep 17 00:00:00 2001 From: neverland Date: Sun, 4 Sep 2022 21:00:18 +0800 Subject: [PATCH] fix(Picker): failed to update value in some cases (#11009) --- packages/vant/src/picker/Picker.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/vant/src/picker/Picker.tsx b/packages/vant/src/picker/Picker.tsx index 3d240a707..f8d14e5a7 100644 --- a/packages/vant/src/picker/Picker.tsx +++ b/packages/vant/src/picker/Picker.tsx @@ -245,10 +245,17 @@ export default defineComponent({ { immediate: true } ); + // preserve last emitted model value + // when props.modelValue is updated by parent component, + // the new value should be compared with the last emitted value + let lastEmittedModelValue: Numeric[]; watch( () => props.modelValue, (newValues) => { - if (!isSameValue(newValues, selectedValues.value)) { + if ( + !isSameValue(newValues, selectedValues.value) && + !isSameValue(newValues, lastEmittedModelValue) + ) { selectedValues.value = newValues.slice(0); } }, @@ -258,7 +265,8 @@ export default defineComponent({ selectedValues, (newValues) => { if (!isSameValue(newValues, props.modelValue)) { - emit('update:modelValue', newValues.slice(0)); + lastEmittedModelValue = newValues.slice(0); + emit('update:modelValue', lastEmittedModelValue); } }, { immediate: true }