Skip to content

Commit f178874

Browse files
committed
fix(v-model): generate separate modifiers for v-model with args
1 parent c53ca29 commit f178874

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default function render() {
7676
const _ctx = this
7777
return (openBlock(), createBlock(\\"input\\", {
7878
[_ctx.value]: _ctx.model,
79-
[\\"onUpdate:\\"+_ctx.value]: $event => (_ctx.model = $event)
79+
[\\"onUpdate:\\" + _ctx.value]: $event => (_ctx.model = $event)
8080
}, null, 16 /* FULL_PROPS */))
8181
}"
8282
`;
@@ -90,7 +90,7 @@ return function render() {
9090
9191
return (_openBlock(), _createBlock(\\"input\\", {
9292
[value]: model,
93-
[\\"onUpdate:\\"+value]: $event => (model = $event)
93+
[\\"onUpdate:\\" + value]: $event => (model = $event)
9494
}, null, 16 /* FULL_PROPS */))
9595
}
9696
}"

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

+33-10
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,7 @@ describe('compiler: transform v-model', () => {
265265
expect(props[1]).toMatchObject({
266266
key: {
267267
children: [
268-
{
269-
content: 'onUpdate:',
270-
isStatic: true
271-
},
272-
'+',
268+
'"onUpdate:" + ',
273269
{
274270
content: 'value',
275271
isStatic: false
@@ -313,11 +309,7 @@ describe('compiler: transform v-model', () => {
313309
expect(props[1]).toMatchObject({
314310
key: {
315311
children: [
316-
{
317-
content: 'onUpdate:',
318-
isStatic: true
319-
},
320-
'+',
312+
'"onUpdate:" + ',
321313
{
322314
content: '_ctx.value',
323315
isStatic: false
@@ -405,6 +397,37 @@ describe('compiler: transform v-model', () => {
405397
expect(args[4]).toBe(`["modelValue", "onUpdate:modelValue"]`)
406398
})
407399

400+
test('should generate modelModifers for component v-model with arguments', () => {
401+
const root = parseWithVModel(
402+
'<Comp v-model:foo.trim="foo" v-model:bar.number="bar" />',
403+
{
404+
prefixIdentifiers: true
405+
}
406+
)
407+
const args = ((root.children[0] as ComponentNode)
408+
.codegenNode as CallExpression).arguments
409+
// props
410+
expect(args[1]).toMatchObject({
411+
properties: [
412+
{ key: { content: `foo` } },
413+
{ key: { content: `onUpdate:foo` } },
414+
{
415+
key: { content: 'fooModifiers' },
416+
value: { content: `{ trim: true }`, isStatic: false }
417+
},
418+
{ key: { content: `bar` } },
419+
{ key: { content: `onUpdate:bar` } },
420+
{
421+
key: { content: 'barModifiers' },
422+
value: { content: `{ number: true }`, isStatic: false }
423+
}
424+
]
425+
})
426+
// should NOT include modelModifiers in dynamicPropNames because it's never
427+
// gonna change
428+
expect(args[4]).toBe(`["foo", "onUpdate:foo", "bar", "onUpdate:bar"]`)
429+
})
430+
408431
describe('errors', () => {
409432
test('missing expression', () => {
410433
const onError = jest.fn()

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
4343
const propName = arg ? arg : createSimpleExpression('modelValue', true)
4444
const eventName = arg
4545
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
46-
? createSimpleExpression('onUpdate:' + arg.content, true)
46+
? `onUpdate:${arg.content}`
4747
: createCompoundExpression([
48-
createSimpleExpression('onUpdate:', true),
49-
'+',
48+
'"onUpdate:" + ',
5049
...(arg.type === NodeTypes.SIMPLE_EXPRESSION ? [arg] : arg.children)
5150
])
52-
: createSimpleExpression('onUpdate:modelValue', true)
51+
: `onUpdate:modelValue`
5352

5453
const props = [
5554
// modelValue: foo
@@ -80,9 +79,19 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
8079
const modifiers = dir.modifiers
8180
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
8281
.join(`, `)
82+
const modifiersKey = arg
83+
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
84+
? `${arg.content}Modifiers`
85+
: createCompoundExpression([
86+
...(arg.type === NodeTypes.SIMPLE_EXPRESSION
87+
? [arg]
88+
: arg.children),
89+
' + "Modifiers"'
90+
])
91+
: `modelModifiers`
8392
props.push(
8493
createObjectProperty(
85-
`modelModifiers`,
94+
modifiersKey,
8695
createSimpleExpression(`{ ${modifiers} }`, false, dir.loc, true)
8796
)
8897
)

0 commit comments

Comments
 (0)