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', () => {
test('missing expression', () => {
const onError = jest.fn()
NodeTypes,
Property,
CompoundExpressionNode,
- createInterpolation
+ createInterpolation,
+ ElementTypes
} from '../ast'
import { createCompilerError, ErrorCodes } from '../errors'
import { isMemberExpression, isSimpleIdentifier } from '../utils'
}
const props = [
+ // modelValue: foo
createObjectProperty(propName, dir.exp!),
+ // "onUpdate:modelValue": $event => (foo = $event)
createObjectProperty(
eventName,
createCompoundExpression([
)
]
- if (dir.modifiers.length) {
- // TODO add modelModifiers prop
+ // modelModifiers: { foo: true, "bar-baz": true }
+ 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)