Skip to content

fix(compat): allow v-model built in modifiers on component #12654

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 3 commits into
base: main
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
12 changes: 10 additions & 2 deletions packages/runtime-core/src/componentEmits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,18 @@ export function emit(
}

let args = rawArgs
const isModelListener = event.startsWith('update:')
let isModelListener
let modifiers
if (
__COMPAT__ &&
(isModelListener = compatModelEventPrefix + event in props)
) {
modifiers = props.modelModifiers
} else if ((isModelListener = event.startsWith('update:'))) {
modifiers = getModelModifiers(props, event.slice(7))
}
Comment on lines +154 to +163
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big issue, but I was wondering about the extra bytes added to the non-compat build. It's only 6 bytes, but still.

I experimented with a few ways to write this, and this version would keep the non-compat builds unchanged:

Suggested change
let isModelListener
let modifiers
if (
__COMPAT__ &&
(isModelListener = compatModelEventPrefix + event in props)
) {
modifiers = props.modelModifiers
} else if ((isModelListener = event.startsWith('update:'))) {
modifiers = getModelModifiers(props, event.slice(7))
}
const isCompatModelListener =
__COMPAT__ && compatModelEventPrefix + event in props
const isModelListener = isCompatModelListener || event.startsWith('update:')
const modifiers = isCompatModelListener
? props.modelModifiers
: isModelListener && getModelModifiers(props, event.slice(7))

Of course there are other considerations besides size.


// for v-model update:xxx events, apply modifiers on args
const modifiers = isModelListener && getModelModifiers(props, event.slice(7))
if (modifiers) {
if (modifiers.trim) {
args = rawArgs.map(a => (isString(a) ? a.trim() : a))
Expand Down
58 changes: 58 additions & 0 deletions packages/vue-compat/__tests__/componentVModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,62 @@ describe('COMPONENT_V_MODEL', () => {
template: `<input :value="input" @input="$emit('update', $event.target.value)">`,
})
})

async function runTestWithModifier(CustomInput: ComponentOptions) {
const vm = new Vue({
data() {
return {
text: ' foo ',
}
},
components: {
CustomInput,
},
template: `
<div>
<span>{{ text }}</span>
<custom-input v-model.trim="text"></custom-input>
</div>
`,
}).$mount() as any

const input = vm.$el.querySelector('input')
const span = vm.$el.querySelector('span')

expect(input.value).toBe(' foo ')
expect(span.textContent).toBe(' foo ')

expect(
(deprecationData[DeprecationTypes.COMPONENT_V_MODEL].message as Function)(
CustomInput,
),
).toHaveBeenWarned()

input.value = ' bar '
triggerEvent(input, 'input')
await nextTick()

expect(input.value).toBe('bar')
expect(span.textContent).toBe('bar')
}

test('with model modifiers', async () => {
await runTestWithModifier({
name: 'CustomInput',
props: ['value'],
template: `<input :value="value" @input="$emit('input', $event.target.value)">`,
})
})

test('with model modifiers and model option', async () => {
await runTestWithModifier({
name: 'CustomInput',
props: ['foo'],
model: {
prop: 'foo',
event: 'bar',
},
template: `<input :value="foo" @input="$emit('bar', $event.target.value)">`,
})
})
})
Loading