Skip to content

Commit 22e8952

Browse files
committed
fix(v-model): add non-matching check back
change event should not be triggered if still can found matching option for each value
1 parent 7f542a9 commit 22e8952

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/platforms/web/runtime/directives/model.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ export default {
6060
const prevOptions = el._vOptions
6161
const curOptions = el._vOptions = [].map.call(el.options, getValue)
6262
if (curOptions.some((o, i) => !looseEqual(o, prevOptions[i]))) {
63-
trigger(el, 'change')
63+
// trigger change event if
64+
// no matching option found for at least one value
65+
const needReset = el.multiple
66+
? binding.value.some(v => hasNoMatchingOption(v, el.options))
67+
: binding.value !== binding.oldValue && hasNoMatchingOption(binding.value, el.options)
68+
if (needReset) {
69+
trigger(el, 'change')
70+
}
6471
}
6572
}
6673
}
@@ -101,6 +108,15 @@ function setSelected (el, binding, vm) {
101108
}
102109
}
103110

111+
function hasNoMatchingOption (value, options) {
112+
for (let i = 0, l = options.length; i < l; i++) {
113+
if (looseEqual(getValue(options[i]), value)) {
114+
return false
115+
}
116+
}
117+
return true
118+
}
119+
104120
function getValue (option) {
105121
return '_value' in option
106122
? option._value

test/unit/features/directives/model-select.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ describe('Directive v-model select', () => {
519519
})
520520

521521
// #6193
522-
it('should not trigger change event when match option can be found for each value', done => {
522+
it('should not trigger change event when matching option can be found for each value', done => {
523523
const spy = jasmine.createSpy()
524524
const vm = new Vue({
525525
data: {

0 commit comments

Comments
 (0)