Skip to content

Commit 5e97643

Browse files
committed
feat(compiler-core/v-model): generate modelModifiers for component v-model
1 parent 25dd507 commit 5e97643

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

packages/compiler-core/__tests__/transforms/vModel.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,27 @@ describe('compiler: transform v-model', () => {
371371
expect(codegen.arguments[4]).toBe(`["modelValue", "onUpdate:modelValue"]`)
372372
})
373373

374+
test('should generate modelModifers for component v-model', () => {
375+
const root = parseWithVModel('<Comp v-model.trim.bar-baz="foo" />', {
376+
prefixIdentifiers: true
377+
})
378+
const args = (root.children[0] as ComponentNode).codegenNode!.arguments
379+
// props
380+
expect(args[1]).toMatchObject({
381+
properties: [
382+
{ key: { content: `modelValue` } },
383+
{ key: { content: `onUpdate:modelValue` } },
384+
{
385+
key: { content: 'modelModifiers' },
386+
value: { content: `{ trim: true, "bar-baz": true }`, isStatic: false }
387+
}
388+
]
389+
})
390+
// should NOT include modelModifiers in dynamicPropNames because it's never
391+
// gonna change
392+
expect(args[4]).toBe(`["modelValue"]`)
393+
})
394+
374395
describe('errors', () => {
375396
test('missing expression', () => {
376397
const onError = jest.fn()

packages/compiler-core/src/transforms/vModel.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
NodeTypes,
77
Property,
88
CompoundExpressionNode,
9-
createInterpolation
9+
createInterpolation,
10+
ElementTypes
1011
} from '../ast'
1112
import { createCompilerError, ErrorCodes } from '../errors'
1213
import { isMemberExpression, isSimpleIdentifier } from '../utils'
@@ -64,7 +65,9 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
6465
}
6566

6667
const props = [
68+
// modelValue: foo
6769
createObjectProperty(propName, dir.exp!),
70+
// "onUpdate:modelValue": $event => (foo = $event)
6871
createObjectProperty(
6972
eventName,
7073
createCompoundExpression([
@@ -75,8 +78,17 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
7578
)
7679
]
7780

78-
if (dir.modifiers.length) {
79-
// TODO add modelModifiers prop
81+
// modelModifiers: { foo: true, "bar-baz": true }
82+
if (dir.modifiers.length && node.tagType === ElementTypes.COMPONENT) {
83+
const modifiers = dir.modifiers
84+
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
85+
.join(`, `)
86+
props.push(
87+
createObjectProperty(
88+
`modelModifiers`,
89+
createSimpleExpression(`{ ${modifiers} }`, false, dir.loc, true)
90+
)
91+
)
8092
}
8193

8294
return createTransformProps(props)

0 commit comments

Comments
 (0)