Skip to content
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

Adds handler for kebab-case event for v-bind:sync; fix #6428 #8297

Merged
merged 4 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 9 additions & 3 deletions src/compiler/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parseHTML } from './html-parser'
import { parseText } from './text-parser'
import { parseFilters } from './filter-parser'
import { genAssignmentCode } from '../directives/model'
import { extend, cached, no, camelize } from 'shared/util'
import { extend, cached, no, camelize, kebabize } from 'shared/util'
import { isIE, isEdge, isServerRendering } from 'core/util/env'

import {
Expand Down Expand Up @@ -512,7 +512,7 @@ function processComponent (el) {

function processAttrs (el) {
const list = el.attrsList
let i, l, name, rawName, value, modifiers, isProp
let i, l, name, rawName, value, modifiers, isProp, syncGen
for (i = 0, l = list.length; i < l; i++) {
name = rawName = list[i].name
value = list[i].value
Expand All @@ -538,10 +538,16 @@ function processAttrs (el) {
name = camelize(name)
}
if (modifiers.sync) {
syncGen = genAssignmentCode(value, `$event`)
addHandler(
el,
`update:${kebabize(name)}`,
Copy link
Member

Choose a reason for hiding this comment

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

The hyphenate function might be a better fit here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point! 🙂 I forgot that one existed. @shortdiv, can you update to use hyphenate instead?

syncGen
)
addHandler(
el,
`update:${camelize(name)}`,
genAssignmentCode(value, `$event`)
syncGen
)
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ export const camelize = cached((str: string): string => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
})

/**
* Kebabize a camelCased string
*/
const kebabizeRE = /([a-z])([A-Z])/g
export const kebabize = cached((str: string): string => {
return str.replace(kebabizeRE, (m, p1, p2) => `${p1}-${p2}`).toLowerCase()
})

Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're no longer using this, can you delete it please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You got it!

/**
* Capitalize a string.
*/
Expand Down
21 changes: 21 additions & 0 deletions test/unit/features/directives/bind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ describe('Directive v-bind', () => {
}).then(done)
})

it('.sync modifier with kebab case event', done => {
const vm = new Vue({
template: `<test :foo-bar.sync="bar"/>`,
data: {
bar: 1
},
components: {
test: {
props: ['fooBar'],
template: `<div @click="$emit('update:foo-bar', 2)">{{ fooBar }}</div>`
}
}
}).$mount()

expect(vm.$el.textContent).toBe('1')
triggerEvent(vm.$el, 'click')
waitForUpdate(() => {
expect(vm.$el.textContent).toBe('2')
}).then(done)
})

it('bind object', done => {
const vm = new Vue({
template: '<input v-bind="test">',
Expand Down