]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-dom): `v-model` string/number coercion for multiselect options (#10576)
authoryangxiuxiu <79584569+yangxiuxiu1115@users.noreply.github.com>
Thu, 28 Mar 2024 14:09:16 +0000 (22:09 +0800)
committerGitHub <noreply@github.com>
Thu, 28 Mar 2024 14:09:16 +0000 (22:09 +0800)
Co-authored-by: RicardoErii <‘1974364190@qq.com’>
Co-authored-by: yangchangtao <yangchangtao@kuaishou.com>
packages/runtime-dom/__tests__/directives/vModel.spec.ts
packages/runtime-dom/src/directives/vModel.ts

index 6cc7b53e2c23b17c2d5cf415475bdd261cdefbc6..d6398f37321bfc9c00619c42ebe856c73ad380de 100644 (file)
@@ -1237,4 +1237,36 @@ describe('vModel', () => {
     await nextTick()
     expect(data.value).toEqual('使用拼音输入')
   })
+
+  it('multiple select (model is number, option value is string)', async () => {
+    const component = defineComponent({
+      data() {
+        return {
+          value: [1, 2],
+        }
+      },
+      render() {
+        return [
+          withVModel(
+            h(
+              'select',
+              {
+                multiple: true,
+                'onUpdate:modelValue': setValue.bind(this),
+              },
+              [h('option', { value: '1' }), h('option', { value: '2' })],
+            ),
+            this.value,
+          ),
+        ]
+      },
+    })
+    render(h(component), root)
+
+    await nextTick()
+    const [foo, bar] = root.querySelectorAll('option')
+
+    expect(foo.selected).toEqual(true)
+    expect(bar.selected).toEqual(true)
+  })
 })
index 9e94810d8cd868432284bb8e888a1c801b007b9c..e002e2e10dac6ad650ea773135bab8dbef031392 100644 (file)
@@ -242,9 +242,7 @@ function setSelected(el: HTMLSelectElement, value: any, number: boolean) {
         const optionType = typeof optionValue
         // fast path for string / number values
         if (optionType === 'string' || optionType === 'number') {
-          option.selected = value.includes(
-            number ? looseToNumber(optionValue) : optionValue,
-          )
+          option.selected = value.some(v => String(v) === String(optionValue))
         } else {
           option.selected = looseIndexOf(value, optionValue) > -1
         }