Skip to content

Commit 55edd7a

Browse files
pearofduckslozjackson
authored andcommitted
Use idiomatic Vue event binding to improve/expand event support (#799)
1 parent 6ecf714 commit 55edd7a

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

vue/src/vueTextMask.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ export default {
77
domProps: {
88
value: this.value
99
},
10-
on: {
10+
attrs: this.$attrs,
11+
on: Object.assign({...this.$listeners}, {
1112
input: (event) => this.updateValue(event.target.value),
12-
focus: (event) => this.emitEvent(event),
13-
blur: (event) => this.emitEvent(event),
14-
keypress: (event) => this.emitEvent(event),
15-
click: (event) => this.emitEvent(event)
16-
}
13+
})
1714
})
1815
},
1916

2017
name: 'masked-input',
2118

19+
inheritAttrs: false,
20+
2221
props: {
2322
value: {
2423
type: String,
@@ -85,10 +84,6 @@ export default {
8584
this.textMaskInputElement.update(value)
8685
this.$emit('input', this.$refs.input.value)
8786
},
88-
89-
emitEvent(event) {
90-
this.$emit(event.type, event)
91-
}
9287
},
9388

9489
watch: {

vue/test/vueTextMask.spec.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import packageJson from '../package.json'
2-
import Vue from 'vue'
2+
import Vue from 'vue/dist/vue.js'
33

44
const VueTextMask = (isVerify()) ?
55
require(`../${packageJson.main}`) :
@@ -17,6 +17,24 @@ function mountComponent(Component, propsData) {
1717
return new Ctor({propsData}).$mount()
1818
}
1919

20+
const eventTest = Vue.extend({
21+
template: `<div>
22+
<masked-input
23+
ref="maskedInput"
24+
type="text"
25+
name="test"
26+
:mask="[/\d/,/\d/,/\d/]"
27+
@focus="callback('focus')"
28+
@blur="callback('blur')"
29+
@keypress="callback('keypress')">
30+
</masked-input>
31+
</div>`,
32+
components: {maskedInput},
33+
methods: {
34+
callback(e) { },
35+
},
36+
})
37+
2038
describe('inputMask', () => {
2139
it('renders', () => {
2240
const vm = mountComponent(maskedInput, {
@@ -219,18 +237,15 @@ describe('inputMask', () => {
219237
})
220238

221239
it('emits focus and blur events for parent components', () => {
222-
const vm = mountComponent(maskedInput, {
223-
value: '123',
224-
mask: ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
225-
})
240+
const vm = mountComponent(eventTest)
226241

227-
vm.emitEvent = sinon.spy()
242+
vm.callback = sinon.spy()
228243

229-
vm.$el.focus()
230-
vm.$el.blur()
231-
expect(vm.emitEvent.callCount).to.equal(2)
232-
expect(vm.emitEvent.getCall(0).args[0].type).to.equal('focus')
233-
expect(vm.emitEvent.getCall(1).args[0].type).to.equal('blur')
244+
vm.$refs.maskedInput.$el.focus()
245+
vm.$refs.maskedInput.$el.blur()
246+
expect(vm.callback.callCount).to.equal(2)
247+
expect(vm.callback.getCall(0).args[0]).to.equal('focus')
248+
expect(vm.callback.getCall(1).args[0]).to.equal('blur')
234249
})
235250

236251
it('does not emit "input" event after component mount', () => {
@@ -248,22 +263,19 @@ describe('inputMask', () => {
248263
})
249264

250265
it('emits keypress event for parent components', () => {
251-
const vm = mountComponent(maskedInput, {
252-
value: '123',
253-
mask: ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
254-
})
266+
const vm = mountComponent(eventTest)
255267

256-
vm.emitEvent = sinon.spy()
268+
vm.callback = sinon.spy()
257269

258270
const e = new window.KeyboardEvent('keypress', {
259271
key: 'e',
260272
bubbles: true,
261273
cancelable: true
262274
})
263-
vm.$el.dispatchEvent(e)
275+
vm.$refs.maskedInput.$el.dispatchEvent(e)
264276

265-
expect(vm.emitEvent.callCount).to.equal(1)
266-
expect(vm.emitEvent.getCall(0).args[0].type).to.equal('keypress')
277+
expect(vm.callback.callCount).to.equal(1)
278+
expect(vm.callback.getCall(0).args[0]).to.equal('keypress')
267279
})
268280
})
269281

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy