-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ssr): properly render <select v-model> initial state
fix #6986
- Loading branch information
Showing
4 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import show from './show' | ||
import model from './model' | ||
|
||
export default { | ||
show | ||
show, | ||
model | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* @flow */ | ||
|
||
import { looseEqual, looseIndexOf } from 'shared/util' | ||
|
||
// this is only applied for <select v-model> because it is the only edge case | ||
// that must be done at runtime instead of compile time. | ||
export default function model (node: VNodeWithData, dir: VNodeDirective) { | ||
if (!node.children) return | ||
const value = dir.value | ||
const isMultiple = node.data.attrs && node.data.attrs.multiple | ||
for (let i = 0, l = node.children.length; i < l; i++) { | ||
const option = node.children[i] | ||
if (option.tag === 'option') { | ||
if (isMultiple) { | ||
const selected = | ||
Array.isArray(value) && | ||
(looseIndexOf(value, getValue(option)) > -1) | ||
if (selected) { | ||
setSelected(option) | ||
} | ||
} else { | ||
if (looseEqual(value, getValue(option))) { | ||
setSelected(option) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function getValue (option) { | ||
const data = option.data || {} | ||
return ( | ||
(data.attrs && data.attrs.value) || | ||
(data.domProps && data.domProps.value) || | ||
(option.children && option.children[0] && option.children[0].text) | ||
) | ||
} | ||
|
||
function setSelected (option) { | ||
const data = option.data || (option.data = {}) | ||
const attrs = data.attrs || (data.attrs = {}) | ||
attrs.selected = '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters