feat(ssr): support getSSRProps for vnode directives

This commit is contained in:
Evan You
2020-03-16 18:36:19 -04:00
parent a46f3b354d
commit c450ede12d
5 changed files with 458 additions and 6 deletions

View File

@@ -218,7 +218,7 @@ function callModelHook(
binding: DirectiveBinding,
vnode: VNode,
prevVNode: VNode | null,
hook: keyof ObjectDirective
hook: 'beforeMount' | 'mounted' | 'beforeUpdate' | 'updated'
) {
let modelToUse: ObjectDirective
switch (el.tagName) {
@@ -243,3 +243,24 @@ function callModelHook(
const fn = modelToUse[hook]
fn && fn(el, binding, vnode, prevVNode)
}
// SSR vnode transforms
if (__NODE_JS__) {
vModelText.getSSRProps = ({ value }) => ({ value })
vModelRadio.getSSRProps = ({ value }, vnode) => {
if (vnode.props && looseEqual(vnode.props.value, value)) {
return { checked: true }
}
}
vModelCheckbox.getSSRProps = ({ value }, vnode) => {
if (isArray(value)) {
if (vnode.props && looseIndexOf(value, vnode.props.value) > -1) {
return { checked: true }
}
} else if (value) {
return { checked: true }
}
}
}

View File

@@ -40,6 +40,14 @@ export const vShow: ObjectDirective<VShowElement> = {
}
}
if (__NODE_JS__) {
vShow.getSSRProps = ({ value }) => {
if (!value) {
return { style: { display: 'none' } }
}
}
}
function setDisplay(el: VShowElement, value: unknown): void {
el.style.display = value ? el._vod : 'none'
}