Skip to content

Commit 075c735

Browse files
committed
fix: checks for input existence
Sometimes on dom mutation input could not be found
1 parent 87a8eda commit 075c735

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/input.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class MaskInput {
3131
}
3232

3333
updateValue (input: HTMLInputElement): void {
34-
if (input.value !== '' && input.value !== this.processInput(input).masked) {
34+
if (input.value !== '' && input.value !== this.processInput(input)?.masked) {
3535
this.setValue(input, input.value)
3636
}
3737
}
@@ -53,11 +53,7 @@ export class MaskInput {
5353
const mask = new Mask(parseInput(input, defaults))
5454
this.items.set(input, mask)
5555

56-
queueMicrotask(() => {
57-
if (document.body.contains(input)) {
58-
this.updateValue(input)
59-
}
60-
})
56+
queueMicrotask(() => this.updateValue(input))
6157

6258
if (input.selectionStart === null && mask.isEager()) {
6359
console.warn('Maska: input of `%s` type is not supported', input.type)
@@ -87,7 +83,10 @@ export class MaskInput {
8783
}
8884

8985
const input = e.target as HTMLInputElement
90-
const mask = this.items.get(input) as Mask
86+
const mask = this.items.get(input)
87+
88+
if (mask === undefined) return
89+
9190
const isDelete = 'inputType' in e && e.inputType.startsWith('delete')
9291
const isEager = mask.isEager()
9392

@@ -111,8 +110,11 @@ export class MaskInput {
111110
const valueNew = input.value
112111
const leftPart = value.slice(0, pos)
113112
const leftPartNew = valueNew.slice(0, pos)
114-
const unmasked = this.processInput(input, leftPart).unmasked
115-
const unmaskedNew = this.processInput(input, leftPartNew).unmasked
113+
const unmasked = this.processInput(input, leftPart)?.unmasked
114+
const unmaskedNew = this.processInput(input, leftPartNew)?.unmasked
115+
116+
if (unmasked === undefined || unmaskedNew === undefined) return
117+
116118
let posFixed = pos
117119

118120
if (leftPart !== leftPartNew) {
@@ -127,6 +129,8 @@ export class MaskInput {
127129
private setValue (input: HTMLInputElement, value: string): void {
128130
const detail = this.processInput(input, value)
129131

132+
if (detail === undefined) return
133+
130134
input.value = detail.masked
131135

132136
if (this.options.onMaska != null) {
@@ -141,8 +145,11 @@ export class MaskInput {
141145
input.dispatchEvent(new CustomEvent('input', { detail: detail.masked }))
142146
}
143147

144-
private processInput (input: HTMLInputElement, value?: string): MaskaDetail {
145-
const mask = this.items.get(input) as Mask
148+
private processInput (input: HTMLInputElement, value?: string): MaskaDetail | undefined {
149+
const mask = this.items.get(input)
150+
151+
if (mask === undefined) return undefined
152+
146153
let valueNew = value ?? input.value
147154

148155
if (this.options.preProcess != null) {

0 commit comments

Comments
 (0)