Skip to content

Commit

Permalink
Fix eager mask bug #128
Browse files Browse the repository at this point in the history
  • Loading branch information
beholdr committed Jan 29, 2023
1 parent ea47223 commit 056b6fb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/mask-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,13 @@ export class MaskInput {
let value = valueOld

if (mask.isEager()) {
const masked = mask.masked(valueOld)
const unmasked = mask.unmasked(valueOld)
const maskedUnmasked = mask.masked(unmasked)

if (unmasked === '' && 'data' in e && e.data != null) {
// empty state and something like `space` pressed
value = e.data
} else if (
maskedUnmasked.startsWith(valueOld) ||
mask.completed(unmasked)
) {
} else if (unmasked !== mask.unmasked(masked)) {
value = unmasked
}
}
Expand Down
80 changes: 80 additions & 0 deletions test/mask-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,71 @@ describe('#-# eager mask', () => {
})
})

describe('+1 (#) #-# eager mask', () => {
beforeAll(() => {
input = prepareInput({ mask: '+1 (#) #-#', eager: true })
})

afterEach(async () => {
await user.clear(input)
})

test('input 1', async () => {
await user.type(input, '1')
expect(input).toHaveValue('+1 (1) ')
})

test('input 12', async () => {
await user.type(input, '12')
expect(input).toHaveValue('+1 (1) 2-')
})

test('input 123', async () => {
await user.type(input, '123')
expect(input).toHaveValue('+1 (1) 2-3')
})

test('input 123', async () => {
await user.type(input, '123')
expect(input).toHaveValue('+1 (1) 2-3')
})

test('input 2', async () => {
await user.type(input, '2')
expect(input).toHaveValue('+1 (2) ')
})

test('input 234', async () => {
await user.type(input, '234')
expect(input).toHaveValue('+1 (2) 3-4')
})

test('input 2{ArrowLeft}3', async () => {
await user.type(input, '2{ArrowLeft}3')
expect(input).toHaveValue('+1 (2) 3-')
})

test('input 234{backspace}', async () => {
await user.type(input, '234{backspace}')
expect(input).toHaveValue('+1 (2) 3-')
})

test('input 234{backspace}×2', async () => {
await user.type(input, '234{backspace}{backspace}')
expect(input).toHaveValue('+1 (2) 3-')
})

test('input 234{backspace}×3', async () => {
await user.type(input, '234{backspace}{backspace}{backspace}')
expect(input).toHaveValue('+1 (2) ')
})

test('input 234{backspace}×4', async () => {
await user.type(input, '234{backspace}{backspace}{backspace}{backspace}')
expect(input).toHaveValue('')
})
})

describe('#-#--# mask', () => {
beforeAll(() => {
input = prepareInput({ mask: '#-#--#' })
Expand Down Expand Up @@ -1104,6 +1169,11 @@ describe('12## eager mask', () => {
expect(input).toHaveValue('123')
})

test('input 11', async () => {
await user.type(input, '11')
expect(input).toHaveValue('1211')
})

test('input 12', async () => {
await user.type(input, '12')
expect(input).toHaveValue('1212')
Expand Down Expand Up @@ -1578,6 +1648,16 @@ describe('IP eager mask', () => {
await user.type(input, '123.456.789.0123')
expect(input).toHaveValue('123.456.789.012')
})

test('input 12.3{ArrowLeft}a', async () => {
await user.type(input, '12.3{ArrowLeft}a')
expect(input).toHaveValue('12.3')
})

test('input 12.3{ArrowLeft}1', async () => {
await user.type(input, '12.3{ArrowLeft}1')
expect(input).toHaveValue('12.13')
})
})

describe('Repeated mask', () => {
Expand Down

0 comments on commit 056b6fb

Please sign in to comment.