Преглед изворни кода

fix(runtime-dom): `v-model` string/number coercion for multiselect options (#10576)

Co-authored-by: RicardoErii <‘1974364190@qq.com’>
Co-authored-by: yangchangtao <yangchangtao@kuaishou.com>
yangxiuxiu пре 2 година
родитељ
комит
db374e54c9

+ 32 - 0
packages/runtime-dom/__tests__/directives/vModel.spec.ts

@@ -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)
+  })
 })

+ 1 - 3
packages/runtime-dom/src/directives/vModel.ts

@@ -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
         }