Skip to content

fix(vapor): v-model and v-model:model co-usage #13070

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

Open
wants to merge 6 commits into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/compiler-core/__tests__/transforms/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,24 @@ describe('compiler: transform v-model', () => {
)
})

test('should generate modelModifiers$ for component v-model:model with arguments', () => {
const root = parseWithVModel('<Comp v-model:model.trim="foo" />', {
prefixIdentifiers: true,
})
const vnodeCall = (root.children[0] as ComponentNode)
.codegenNode as VNodeCall
expect(vnodeCall.props).toMatchObject({
properties: [
{ key: { content: `model` } },
{ key: { content: `onUpdate:model` } },
{
key: { content: 'modelModifiers$' },
value: { content: `{ trim: true }`, isStatic: false },
},
],
})
})

describe('errors', () => {
test('missing expression', () => {
const onError = vi.fn()
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-core/src/transforms/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../utils'
import { IS_REF } from '../runtimeHelpers'
import { BindingTypes } from '../options'
import { camelize } from '@vue/shared'
import { camelize, getModifierPropName } from '@vue/shared'

export const transformModel: DirectiveTransform = (dir, node, context) => {
const { exp, arg } = dir
Expand Down Expand Up @@ -136,7 +136,7 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
.join(`, `)
const modifiersKey = arg
? isStaticExp(arg)
? `${arg.content}Modifiers`
? getModifierPropName(arg.content)
: createCompoundExpression([arg, ' + "Modifiers"'])
: `modelModifiers`
props.push(
Expand Down
5 changes: 2 additions & 3 deletions packages/compiler-sfc/src/script/defineModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ScriptCompileContext } from './context'
import { inferRuntimeType } from './resolveType'
import { UNKNOWN_TYPE, isCallOf, toRuntimeTypeString } from './utils'
import { BindingTypes, unwrapTSNode } from '@vue/compiler-dom'
import { getModifierPropName } from '@vue/shared'

export const DEFINE_MODEL = 'defineModel'

Expand Down Expand Up @@ -167,9 +168,7 @@ export function genModelProps(ctx: ScriptCompileContext) {
modelPropsDecl += `\n ${JSON.stringify(name)}: ${decl},`

// also generate modifiers prop
const modifierPropName = JSON.stringify(
name === 'modelValue' ? `modelModifiers` : `${name}Modifiers`,
)
const modifierPropName = JSON.stringify(getModifierPropName(name))
modelPropsDecl += `\n ${modifierPropName}: {},`
}
return `{${modelPropsDecl}\n }`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ export function render(_ctx) {
}"
`;

exports[`compiler: vModel transform > component > v-model:model with arguments for component should generate modelModifiers$ 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _component_Comp = _resolveComponent("Comp")
const n0 = _createComponentWithFallback(_component_Comp, { model: () => (_ctx.foo),
"onUpdate:model": () => _value => (_ctx.foo = _value),
modelModifiers$: () => ({ trim: true }) }, null, true)
return n0
}"
`;

exports[`compiler: vModel transform > modifiers > .lazy 1`] = `
"import { applyTextModel as _applyTextModel, template as _template } from 'vue';
const t0 = _template("<input>", true)
Expand Down
22 changes: 22 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,28 @@ describe('compiler: vModel transform', () => {
})
})

test('v-model:model with arguments for component should generate modelModifiers$', () => {
const { code, ir } = compileWithVModel(
'<Comp v-model:model.trim="foo" />',
)
expect(code).toMatchSnapshot()
expect(code).contain(`modelModifiers$: () => ({ trim: true })`)
expect(ir.block.dynamic.children[0].operation).toMatchObject({
type: IRNodeTypes.CREATE_COMPONENT_NODE,
tag: 'Comp',
props: [
[
{
key: { content: 'model', isStatic: true },
values: [{ content: 'foo', isStatic: false }],
model: true,
modelModifiers: ['trim'],
},
],
],
})
})

test('v-model with dynamic arguments for component should generate modelModifiers ', () => {
const { code, ir } = compileWithVModel(
'<Comp v-model:[foo].trim="foo" v-model:[bar].number="bar" />',
Expand Down
6 changes: 2 additions & 4 deletions packages/compiler-vapor/src/generators/component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { camelize, extend, isArray } from '@vue/shared'
import { camelize, extend, getModifierPropName, isArray } from '@vue/shared'
import type { CodegenContext } from '../generate'
import {
type CreateComponentIRNode,
Expand Down Expand Up @@ -240,9 +240,7 @@ function genModelModifiers(
if (!modelModifiers || !modelModifiers.length) return []

const modifiersKey = key.isStatic
? key.content === 'modelValue'
? [`modelModifiers`]
: [`${key.content}Modifiers`]
? [getModifierPropName(key.content)]
: ['[', ...genExpression(key, context), ' + "Modifiers"]']

const modifiersVal = genDirectiveModifiers(modelModifiers)
Expand Down
18 changes: 12 additions & 6 deletions packages/runtime-core/src/helpers/useModel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { type Ref, customRef, ref } from '@vue/reactivity'
import { EMPTY_OBJ, camelize, hasChanged, hyphenate } from '@vue/shared'
import {
EMPTY_OBJ,
camelize,
getModifierPropName,
hasChanged,
hyphenate,
} from '@vue/shared'
import type { DefineModelOptions, ModelRef } from '../apiSetupHelpers'
import {
type ComponentInternalInstance,
Expand Down Expand Up @@ -145,9 +151,9 @@ export const getModelModifiers = (
modelName: string,
getter: (props: Record<string, any>, key: string) => any,
): Record<string, boolean> | undefined => {
return modelName === 'modelValue' || modelName === 'model-value'
? getter(props, 'modelModifiers')
: getter(props, `${modelName}Modifiers`) ||
getter(props, `${camelize(modelName)}Modifiers`) ||
getter(props, `${hyphenate(modelName)}Modifiers`)
return (
getter(props, getModifierPropName(modelName)) ||
getter(props, `${camelize(modelName)}Modifiers`) ||
getter(props, `${hyphenate(modelName)}Modifiers`)
)
}
12 changes: 12 additions & 0 deletions packages/shared/src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ export const toHandlerKey: <T extends string>(
},
)

/**
* #13070 When v-model and v-model:model directives are used together,
* they will generate the same modelModifiers prop,
* so a `$` suffix is added to avoid conflicts.
* @private
*/
export const getModifierPropName = (name: string): string => {
return `${
name === 'modelValue' || name === 'model-value' ? 'model' : name
}Modifiers${name === 'model' ? '$' : ''}`
}

// compare whether a value has changed, accounting for NaN.
export const hasChanged = (value: any, oldValue: any): boolean =>
!Object.is(value, oldValue)
Expand Down