Skip to content

Commit 67c6894

Browse files
author
Dipak Sarkar
committed
added min and max options to input field
1 parent 3dc63b4 commit 67c6894

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

src/core.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,19 @@ export function updateValue(el, vnode, { emit = true, force = false, clean = fal
7272
oldValue = oldValue || ''
7373
currentValue = currentValue || ''
7474

75-
if (force || oldValue !== currentValue) {
76-
const number = new NumberFormat(config).clean(clean)
77-
const masked = number.format(currentValue)
78-
const unmasked = number.unformat(currentValue)
75+
const number = new NumberFormat(config).clean(clean)
76+
let masked = number.format(currentValue)
77+
let unmasked = number.unformat(currentValue)
78+
79+
// check value with in range max and min value
80+
if ((config.max && unmasked > config.max) || (config.min && unmasked < config.min)) {
81+
masked = number.format(oldValue)
82+
unmasked = number.unformat(oldValue)
83+
}
7984

85+
if (force || oldValue !== currentValue) {
8086
el[CONFIG_KEY].oldValue = masked
8187
el.unmaskedValue = unmasked
82-
8388
// safari makes the cursor jump to the end if el.value gets assign even if to the same value
8489
if (el.value !== masked) {
8590
el.value = masked

src/options.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ export default {
55
decimal: '.',
66
precision: 2,
77
prefill: true,
8-
null_value: 0
8+
reverse: false,
9+
min: false,
10+
max: false,
11+
null_value: ''
912
}

tests/unit/number-format.custom.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ describe('should not throw error on empty config', () => {
55
prefix: '$',
66
separator: '.',
77
decimal: ',',
8-
null_value: '',
8+
null_value: 0,
99
})).not.toThrow()
1010
})
1111
describe('when the value is invalid with custom config', () => {
1212
const numberFormat = new NumberFormat({
1313
prefix: '$',
1414
separator: '.',
1515
decimal: ',',
16-
null_value: '',
16+
null_value: 0,
1717
})
1818
it('should return as follows', () => {
1919
expect(numberFormat.format('')).toEqual('')

tests/unit/number-format.default.spec.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@ describe('should not throw error on empty config', () => {
66
describe('when the value is invalid with default config', () => {
77
const numberFormat = new NumberFormat({})
88
it('should return as follows', () => {
9-
expect(numberFormat.format('')).toEqual(0)
10-
expect(numberFormat.format('foo')).toEqual(0)
11-
expect(numberFormat.format('-foo')).toEqual(0)
12-
expect(numberFormat.format('-fo,o-')).toEqual(0)
13-
expect(numberFormat.format('!@#$%^&*()')).toEqual(0)
9+
expect(numberFormat.format('')).toEqual('')
10+
expect(numberFormat.format('foo')).toEqual('')
11+
expect(numberFormat.format('-foo')).toEqual('')
12+
expect(numberFormat.format('-fo,o-')).toEqual('')
13+
expect(numberFormat.format('!@#$%^&*()')).toEqual('')
1414
})
1515
it('should return as follows', () => {
16-
expect(numberFormat.clean().format('')).toEqual(0)
17-
expect(numberFormat.clean().format('foo')).toEqual(0)
18-
expect(numberFormat.clean().format('-foo')).toEqual(0)
19-
expect(numberFormat.clean().format('-fo,o-')).toEqual(0)
20-
expect(numberFormat.clean().format('!@#$%^&*()')).toEqual(0)
16+
expect(numberFormat.clean().format('')).toEqual('')
17+
expect(numberFormat.clean().format('foo')).toEqual('')
18+
expect(numberFormat.clean().format('-foo')).toEqual('')
19+
expect(numberFormat.clean().format('-fo,o-')).toEqual('')
20+
expect(numberFormat.clean().format('!@#$%^&*()')).toEqual('')
2121
})
2222
it('should return as follows', () => {
23-
expect(numberFormat.unformat('')).toEqual(0)
24-
expect(numberFormat.unformat('foo')).toEqual(0)
25-
expect(numberFormat.unformat('-foo')).toEqual(0)
26-
expect(numberFormat.unformat('-fo,o-')).toEqual(0)
27-
expect(numberFormat.unformat('!@#$%^&*()')).toEqual(0)
23+
expect(numberFormat.unformat('')).toEqual('')
24+
expect(numberFormat.unformat('foo')).toEqual('')
25+
expect(numberFormat.unformat('-foo')).toEqual('')
26+
expect(numberFormat.unformat('-fo,o-')).toEqual('')
27+
expect(numberFormat.unformat('!@#$%^&*()')).toEqual('')
2828
})
2929
})
3030
describe('format when options are default', () => {

0 commit comments

Comments
 (0)