feat(compiler-core/v-model): generate modelModifiers for component v-model

This commit is contained in:
Evan You 2019-10-16 14:18:29 -04:00
parent 25dd507f71
commit 5e97643c85
2 changed files with 36 additions and 3 deletions

View File

@ -371,6 +371,27 @@ describe('compiler: transform v-model', () => {
expect(codegen.arguments[4]).toBe(`["modelValue", "onUpdate:modelValue"]`) expect(codegen.arguments[4]).toBe(`["modelValue", "onUpdate:modelValue"]`)
}) })
test('should generate modelModifers for component v-model', () => {
const root = parseWithVModel('<Comp v-model.trim.bar-baz="foo" />', {
prefixIdentifiers: true
})
const args = (root.children[0] as ComponentNode).codegenNode!.arguments
// props
expect(args[1]).toMatchObject({
properties: [
{ key: { content: `modelValue` } },
{ key: { content: `onUpdate:modelValue` } },
{
key: { content: 'modelModifiers' },
value: { content: `{ trim: true, "bar-baz": true }`, isStatic: false }
}
]
})
// should NOT include modelModifiers in dynamicPropNames because it's never
// gonna change
expect(args[4]).toBe(`["modelValue"]`)
})
describe('errors', () => { describe('errors', () => {
test('missing expression', () => { test('missing expression', () => {
const onError = jest.fn() const onError = jest.fn()

View File

@ -6,7 +6,8 @@ import {
NodeTypes, NodeTypes,
Property, Property,
CompoundExpressionNode, CompoundExpressionNode,
createInterpolation createInterpolation,
ElementTypes
} from '../ast' } from '../ast'
import { createCompilerError, ErrorCodes } from '../errors' import { createCompilerError, ErrorCodes } from '../errors'
import { isMemberExpression, isSimpleIdentifier } from '../utils' import { isMemberExpression, isSimpleIdentifier } from '../utils'
@ -64,7 +65,9 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
} }
const props = [ const props = [
// modelValue: foo
createObjectProperty(propName, dir.exp!), createObjectProperty(propName, dir.exp!),
// "onUpdate:modelValue": $event => (foo = $event)
createObjectProperty( createObjectProperty(
eventName, eventName,
createCompoundExpression([ createCompoundExpression([
@ -75,8 +78,17 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
) )
] ]
if (dir.modifiers.length) { // modelModifiers: { foo: true, "bar-baz": true }
// TODO add modelModifiers prop if (dir.modifiers.length && node.tagType === ElementTypes.COMPONENT) {
const modifiers = dir.modifiers
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
.join(`, `)
props.push(
createObjectProperty(
`modelModifiers`,
createSimpleExpression(`{ ${modifiers} }`, false, dir.loc, true)
)
)
} }
return createTransformProps(props) return createTransformProps(props)