Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TimePicker): fix the 'modelValue' watch defect #13095

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/vant/src/time-picker/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ export default defineComponent({
watch(
() => props.modelValue,
(newValues) => {
newValues = formatValueRange(newValues, columns.value);
if (!isSameValue(newValues, currentValues.value)) {
currentValues.value = newValues;
const _newValues = formatValueRange(newValues, columns.value);
if (
!isSameValue(_newValues, currentValues.value) ||
!isSameValue(_newValues, newValues)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么需要加这个判断呢,从 issue 中没有看到解释

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. v-model 修改为 ['00', '00'] 时,此时 UI 上没有 ['00', '00'] 选项,UI 被迫选中最接近的一个值,但是 v-model 没有同步更新为该值,依旧是 ['00', '00'](UI 变了,但 v-model 没变)(可能是一个 BUG)

对应的是 issue 中这段话

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于新加的这个 if 判断,似乎只有在 currentValues.value 和 _newValues 相等的时候会触发,那下面的 currentValues.value = _newValues; 赋值有什么意义呢?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

该赋值为了触发这段侦听器:

    watch(currentValues, (newValues) => {
      if (!isSameValue(newValues, props.modelValue)) {
        emit('update:modelValue', newValues);
      }
    });

目的是让内部的 currentValues 同步到外部的 v-model 绑定的变量

) {
currentValues.value = _newValues;
}
},
{ immediate: true },
Expand Down