Skip to content

Commit e5a44ec

Browse files
committed
update event listiner
1 parent f055564 commit e5a44ec

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/core.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,14 @@ export function inputHandler(event: CustomInputEvent) {
169169
/**
170170
* Blur event handler
171171
*
172-
* @param {Event} event The event object
172+
* @param {CustomInputEvent} event The event object
173173
*/
174-
export function blurHandler(event: Event) {
175-
const { target } = event as CustomInputEvent
176-
177-
// since we will be emitting our own custom input event
178-
// we can stop propagation of this native event
179-
event.stopPropagation()
174+
export function blurHandler(event: CustomInputEvent) {
175+
const { target } = event
180176

181177
const { oldValue } = target
182178

183-
updateValue(target, null, { force: true, emit: false, clean: true })
179+
updateValue(target, null, { force: true, clean: true })
184180

185181
if (oldValue !== target.value) {
186182
target.dispatchEvent(InputEvent('change'))
@@ -189,6 +185,8 @@ export function blurHandler(event: Event) {
189185

190186
/**
191187
* Keydown event handler
188+
*
189+
* @param {KeyboardEvent} event The event object
192190
*/
193191
export function keydownHandler(event: KeyboardEvent, el: CustomInputElement) {
194192
const { options } = el

src/directive.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,31 @@ export default {
3636
core.inputHandler(e as core.CustomInputEvent)
3737
}
3838

39-
handlerOwner.addEventListener('input', oninput, true)
40-
41-
el.onblur = (e) => core.blurHandler(e)
39+
const onblur = (e: Event) => {
40+
if (e.target !== el) {
41+
return
42+
}
43+
core.blurHandler(e as core.CustomInputEvent)
44+
}
4245

4346
// check decimal key and insert to current element
4447
// updated cursor position after format the value
45-
el.onkeydown = (e) => core.keydownHandler(e, el)
48+
const onkeydown = (e: Event) => {
49+
if (e.target !== el) {
50+
return
51+
}
52+
core.keydownHandler(e as KeyboardEvent, el)
53+
}
4654

47-
el.cleanup = () => handlerOwner.removeEventListener('input', oninput, true)
55+
handlerOwner.addEventListener('input', oninput, true)
56+
handlerOwner.addEventListener('blur', onblur, true)
57+
handlerOwner.addEventListener('keydown', onkeydown, true)
58+
59+
el.cleanup = () => {
60+
handlerOwner.removeEventListener('input', oninput, true)
61+
handlerOwner.removeEventListener('blur', onblur, true)
62+
handlerOwner.removeEventListener('keydown', onkeydown, true)
63+
}
4864
},
4965

5066
updated: (el: core.CustomInputElement, { value, oldValue, modifiers }: DirectiveBinding, vnode: VNode) => {

0 commit comments

Comments
 (0)