Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): Support vnode v-model when input type is dynamic #5787

Merged
merged 2 commits into from May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 34 additions & 19 deletions packages/runtime-dom/src/directives/vModel.ts
Expand Up @@ -266,33 +266,35 @@ export const vModelDynamic: ObjectDirective<
}
}

function callModelHook(
el: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement,
binding: DirectiveBinding,
vnode: VNode,
prevVNode: VNode | null,
hook: keyof ObjectDirective
) {
let modelToUse: ObjectDirective
switch (el.tagName) {
function resolveDynamicModel(tagName: string, type: string | undefined) {
switch (tagName.toUpperCase()) {
catrope marked this conversation as resolved.
Show resolved Hide resolved
case 'SELECT':
modelToUse = vModelSelect
break
return vModelSelect
case 'TEXTAREA':
modelToUse = vModelText
break
return vModelText
default:
switch (vnode.props && vnode.props.type) {
switch (type) {
case 'checkbox':
modelToUse = vModelCheckbox
break
return vModelCheckbox
case 'radio':
modelToUse = vModelRadio
break
return vModelRadio
default:
modelToUse = vModelText
return vModelText
}
}
}

function callModelHook(
el: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement,
binding: DirectiveBinding,
vnode: VNode,
prevVNode: VNode | null,
hook: keyof ObjectDirective
) {
const modelToUse = resolveDynamicModel(
el.tagName,
vnode.props && vnode.props.type
)
const fn = modelToUse[hook] as DirectiveHook
fn && fn(el, binding, vnode, prevVNode)
}
Expand Down Expand Up @@ -321,4 +323,17 @@ export function initVModelForSSR() {
return { checked: true }
}
}

vModelDynamic.getSSRProps = (binding, vnode) => {
if (typeof vnode.type !== 'string') {
return
}
const modelToUse = resolveDynamicModel(
vnode.type,
vnode.props && vnode.props.type
)
if (modelToUse.getSSRProps) {
return modelToUse.getSSRProps(binding, vnode)
}
}
}
95 changes: 95 additions & 0 deletions packages/server-renderer/__tests__/ssrDirectives.spec.ts
Expand Up @@ -11,6 +11,7 @@ import {
vModelText,
vModelRadio,
vModelCheckbox,
vModelDynamic,
resolveDirective
} from 'vue'
import { ssrGetDirectiveProps, ssrRenderAttrs } from '../src'
Expand Down Expand Up @@ -376,6 +377,100 @@ describe('ssr: directives', () => {
})
})

describe('vnode v-model dynamic', () => {
test('text', async () => {
expect(
await renderToString(
createApp({
render() {
return withDirectives(h('input'), [[vModelDynamic, 'hello']])
}
})
)
).toBe(`<input value="hello">`)
})

test('radio', async () => {
expect(
await renderToString(
createApp({
render() {
return withDirectives(
h('input', { type: 'radio', value: 'hello' }),
[[vModelDynamic, 'hello']]
)
}
})
)
).toBe(`<input type="radio" value="hello" checked>`)

expect(
await renderToString(
createApp({
render() {
return withDirectives(
h('input', { type: 'radio', value: 'hello' }),
[[vModelDynamic, 'foo']]
)
}
})
)
).toBe(`<input type="radio" value="hello">`)
})

test('checkbox', async () => {
expect(
await renderToString(
createApp({
render() {
return withDirectives(h('input', { type: 'checkbox' }), [
[vModelDynamic, true]
])
}
})
)
).toBe(`<input type="checkbox" checked>`)

expect(
await renderToString(
createApp({
render() {
return withDirectives(h('input', { type: 'checkbox' }), [
[vModelDynamic, false]
])
}
})
)
).toBe(`<input type="checkbox">`)

expect(
await renderToString(
createApp({
render() {
return withDirectives(
h('input', { type: 'checkbox', value: 'foo' }),
[[vModelDynamic, ['foo']]]
)
}
})
)
).toBe(`<input type="checkbox" value="foo" checked>`)

expect(
await renderToString(
createApp({
render() {
return withDirectives(
h('input', { type: 'checkbox', value: 'foo' }),
[[vModelDynamic, []]]
)
}
})
)
).toBe(`<input type="checkbox" value="foo">`)
})
})

test('custom directive w/ getSSRProps (vdom)', async () => {
expect(
await renderToString(
Expand Down