vuejs / vue

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core
http://v2.vuejs.org
MIT License
207.89k stars 33.69k forks source link

when <select> model and the option list changed at the same time, model may incorrectly set to `undefined` #10614

Open xieranmaya opened 5 years ago

xieranmaya commented 5 years ago

Version

2.6.10

Reproduction link

https://jsfiddle.net/4fyrj95L/

Steps to reproduce

set model binded to select element and the array that iterated the option list at the same time

make sure the new model value do not match any of the new options

the model will be set to undefined

What is expected?

model value to be set to 1

What is actually happening?

model's value set to undefined


This bug only appears when model and the options changed at the same and the new model value does not match any option, other situations behaves correctly as far as I tested.

posva commented 5 years ago

as a workaround you can set a key on the select element that changes alongside the selected list

xieranmaya commented 5 years ago

It has nothing to do with the key, add key or keys do not resolve the problem. Acturally the bug is triggered by an internal logic. See the pr I created above. @posva

posva commented 5 years ago

A workaround is only a temporary fix for the problem for you or anybody to have a solution until a fix is released: The original problem has nothing to deal with keys yet, adding a key does remove the bug

despreston commented 4 years ago

@posva Maybe im reading the description incorrectly but this sounds like expected behavior. If i change the list of available options, and then change the value to something not in the list of available options, it should be undefined, right?

posva commented 4 years ago

It should keep the set value instead of changing it to undefined

xieranmaya commented 4 years ago

This issue is owned by the logic of the code: https://github.com/vuejs/vue/pull/10615/files the $selectedValue variable in line 121 may be an empty array, but the subsequent code read its first element, if the code is in c/c++, that would be an error or index out of bound. setting it to undefined is just an accident.