fix(v-model): generate separate modifiers for v-model with args
This commit is contained in:
parent
c53ca29ea1
commit
f178874ace
@ -76,7 +76,7 @@ export default function render() {
|
|||||||
const _ctx = this
|
const _ctx = this
|
||||||
return (openBlock(), createBlock(\\"input\\", {
|
return (openBlock(), createBlock(\\"input\\", {
|
||||||
[_ctx.value]: _ctx.model,
|
[_ctx.value]: _ctx.model,
|
||||||
[\\"onUpdate:\\"+_ctx.value]: $event => (_ctx.model = $event)
|
[\\"onUpdate:\\" + _ctx.value]: $event => (_ctx.model = $event)
|
||||||
}, null, 16 /* FULL_PROPS */))
|
}, null, 16 /* FULL_PROPS */))
|
||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
@ -90,7 +90,7 @@ return function render() {
|
|||||||
|
|
||||||
return (_openBlock(), _createBlock(\\"input\\", {
|
return (_openBlock(), _createBlock(\\"input\\", {
|
||||||
[value]: model,
|
[value]: model,
|
||||||
[\\"onUpdate:\\"+value]: $event => (model = $event)
|
[\\"onUpdate:\\" + value]: $event => (model = $event)
|
||||||
}, null, 16 /* FULL_PROPS */))
|
}, null, 16 /* FULL_PROPS */))
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
|
@ -265,11 +265,7 @@ describe('compiler: transform v-model', () => {
|
|||||||
expect(props[1]).toMatchObject({
|
expect(props[1]).toMatchObject({
|
||||||
key: {
|
key: {
|
||||||
children: [
|
children: [
|
||||||
{
|
'"onUpdate:" + ',
|
||||||
content: 'onUpdate:',
|
|
||||||
isStatic: true
|
|
||||||
},
|
|
||||||
'+',
|
|
||||||
{
|
{
|
||||||
content: 'value',
|
content: 'value',
|
||||||
isStatic: false
|
isStatic: false
|
||||||
@ -313,11 +309,7 @@ describe('compiler: transform v-model', () => {
|
|||||||
expect(props[1]).toMatchObject({
|
expect(props[1]).toMatchObject({
|
||||||
key: {
|
key: {
|
||||||
children: [
|
children: [
|
||||||
{
|
'"onUpdate:" + ',
|
||||||
content: 'onUpdate:',
|
|
||||||
isStatic: true
|
|
||||||
},
|
|
||||||
'+',
|
|
||||||
{
|
{
|
||||||
content: '_ctx.value',
|
content: '_ctx.value',
|
||||||
isStatic: false
|
isStatic: false
|
||||||
@ -405,6 +397,37 @@ describe('compiler: transform v-model', () => {
|
|||||||
expect(args[4]).toBe(`["modelValue", "onUpdate:modelValue"]`)
|
expect(args[4]).toBe(`["modelValue", "onUpdate:modelValue"]`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('should generate modelModifers for component v-model with arguments', () => {
|
||||||
|
const root = parseWithVModel(
|
||||||
|
'<Comp v-model:foo.trim="foo" v-model:bar.number="bar" />',
|
||||||
|
{
|
||||||
|
prefixIdentifiers: true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const args = ((root.children[0] as ComponentNode)
|
||||||
|
.codegenNode as CallExpression).arguments
|
||||||
|
// props
|
||||||
|
expect(args[1]).toMatchObject({
|
||||||
|
properties: [
|
||||||
|
{ key: { content: `foo` } },
|
||||||
|
{ key: { content: `onUpdate:foo` } },
|
||||||
|
{
|
||||||
|
key: { content: 'fooModifiers' },
|
||||||
|
value: { content: `{ trim: true }`, isStatic: false }
|
||||||
|
},
|
||||||
|
{ key: { content: `bar` } },
|
||||||
|
{ key: { content: `onUpdate:bar` } },
|
||||||
|
{
|
||||||
|
key: { content: 'barModifiers' },
|
||||||
|
value: { content: `{ number: true }`, isStatic: false }
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
// should NOT include modelModifiers in dynamicPropNames because it's never
|
||||||
|
// gonna change
|
||||||
|
expect(args[4]).toBe(`["foo", "onUpdate:foo", "bar", "onUpdate:bar"]`)
|
||||||
|
})
|
||||||
|
|
||||||
describe('errors', () => {
|
describe('errors', () => {
|
||||||
test('missing expression', () => {
|
test('missing expression', () => {
|
||||||
const onError = jest.fn()
|
const onError = jest.fn()
|
||||||
|
@ -43,13 +43,12 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
|
|||||||
const propName = arg ? arg : createSimpleExpression('modelValue', true)
|
const propName = arg ? arg : createSimpleExpression('modelValue', true)
|
||||||
const eventName = arg
|
const eventName = arg
|
||||||
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
|
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
|
||||||
? createSimpleExpression('onUpdate:' + arg.content, true)
|
? `onUpdate:${arg.content}`
|
||||||
: createCompoundExpression([
|
: createCompoundExpression([
|
||||||
createSimpleExpression('onUpdate:', true),
|
'"onUpdate:" + ',
|
||||||
'+',
|
|
||||||
...(arg.type === NodeTypes.SIMPLE_EXPRESSION ? [arg] : arg.children)
|
...(arg.type === NodeTypes.SIMPLE_EXPRESSION ? [arg] : arg.children)
|
||||||
])
|
])
|
||||||
: createSimpleExpression('onUpdate:modelValue', true)
|
: `onUpdate:modelValue`
|
||||||
|
|
||||||
const props = [
|
const props = [
|
||||||
// modelValue: foo
|
// modelValue: foo
|
||||||
@ -80,9 +79,19 @@ export const transformModel: DirectiveTransform = (dir, node, context) => {
|
|||||||
const modifiers = dir.modifiers
|
const modifiers = dir.modifiers
|
||||||
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
|
.map(m => (isSimpleIdentifier(m) ? m : JSON.stringify(m)) + `: true`)
|
||||||
.join(`, `)
|
.join(`, `)
|
||||||
|
const modifiersKey = arg
|
||||||
|
? arg.type === NodeTypes.SIMPLE_EXPRESSION && arg.isStatic
|
||||||
|
? `${arg.content}Modifiers`
|
||||||
|
: createCompoundExpression([
|
||||||
|
...(arg.type === NodeTypes.SIMPLE_EXPRESSION
|
||||||
|
? [arg]
|
||||||
|
: arg.children),
|
||||||
|
' + "Modifiers"'
|
||||||
|
])
|
||||||
|
: `modelModifiers`
|
||||||
props.push(
|
props.push(
|
||||||
createObjectProperty(
|
createObjectProperty(
|
||||||
`modelModifiers`,
|
modifiersKey,
|
||||||
createSimpleExpression(`{ ${modifiers} }`, false, dir.loc, true)
|
createSimpleExpression(`{ ${modifiers} }`, false, dir.loc, true)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user