Browse Source

fix multiple select render (#3908)

* fix multiple select render. The mutliple attribute of select dosen't apply at first, so the mutli selected option dosen't work when patching

* keep the vnode.data the same as before
defcc 9 years ago
parent
commit
8bb1e58df0

+ 1 - 1
src/core/vdom/patch.js

@@ -115,7 +115,7 @@ export function createPatchFunction (backend) {
       }
       vnode.elm = vnode.ns
         ? nodeOps.createElementNS(vnode.ns, tag)
-        : nodeOps.createElement(tag)
+        : nodeOps.createElement(tag, vnode)
       setScope(vnode)
       createChildren(vnode, children, insertedVnodeQueue)
       if (isDef(data)) {

+ 9 - 2
src/platforms/web/runtime/node-ops.js

@@ -2,8 +2,15 @@
 
 import { namespaceMap } from 'web/util/index'
 
-export function createElement (tagName: string): Element {
-  return document.createElement(tagName)
+export function createElement (tagName: string, vnode: VNode): Element {
+  const elm = document.createElement(tagName)
+  if (tagName !== 'select') {
+    return elm
+  }
+  if (vnode.data && vnode.data.attrs && 'multiple' in vnode.data.attrs) {
+    elm.setAttribute('multiple', 'multiple')
+  }
+  return elm
 }
 
 export function createElementNS (namespace: string, tagName: string): Element {

+ 15 - 0
test/unit/features/directives/model-select.spec.js

@@ -189,6 +189,21 @@ describe('Directive v-model select', () => {
     }).then(done)
   })
 
+  it('multiple with static template', () => {
+    const vm = new Vue({
+      template:
+      '<select multiple>' +
+        '<option selected>a</option>' +
+        '<option selected>b</option>' +
+        '<option selected>c</option>' +
+      '</select>'
+    }).$mount()
+    var opts = vm.$el.options
+    expect(opts[0].selected).toBe(true)
+    expect(opts[1].selected).toBe(true)
+    expect(opts[2].selected).toBe(true)
+  })
+
   it('multiple + v-for', done => {
     const vm = new Vue({
       data: {