2
0
mirror of https://github.com/tenrok/maska.git synced 2026-06-14 18:42:27 +03:00

Add mask raw value attribute and @mask event

This commit is contained in:
Alexander Shabunevich
2021-02-06 20:10:51 +03:00
parent 75fb1638ba
commit 99013bd69d
10 changed files with 48 additions and 24 deletions
+15 -2
View File
@@ -51,18 +51,27 @@ export default class Maska {
updateValue (el, evt) {
const wrongNum = el.type.match(/^number$/i) && el.validity.badInput
if ((!el.value && !wrongNum) || !el.dataset.mask) return
if ((!el.value && !wrongNum) || !el.dataset.mask) {
el.dataset.maskRawValue = ''
this.dispatch('maska', el, evt)
return
}
let position = el.selectionEnd
const oldValue = el.value
const digit = oldValue[position - 1]
el.dataset.maskRawValue = mask(el.value, el.dataset.mask, this._opts.tokens, false)
el.value = mask(el.value, el.dataset.mask, this._opts.tokens)
if (evt && evt.inputType === 'insertText' && position === oldValue.length) {
position = el.value.length
}
fixInputSelection(el, position, digit)
this.dispatch('maska', el, evt)
if (el.value !== oldValue) {
el.dispatchEvent(event('input', (evt && evt.inputType) || null))
this.dispatch('input', el, evt)
}
}
@@ -71,4 +80,8 @@ export default class Maska {
e.preventDefault()
}
}
dispatch (name, el, evt) {
el.dispatchEvent(event(name, (evt && evt.inputType) || null))
}
}