Skip to content

Commit fac8cf1

Browse files
committed
[Fix] @input:model-value doesn't allow to write negative numbers
1 parent e8129bb commit fac8cf1

File tree

4 files changed

+42
-67
lines changed

4 files changed

+42
-67
lines changed

src/directive.js

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export default {
5858
core.updateCursor(el, positionFromEnd)
5959
// trigger input event
6060
el.dispatchEvent(new Event('input'))
61+
} else if ([config.prefix, '-'].includes(character)) {
62+
e.preventDefault()
63+
el.value = ''
64+
el.dispatchEvent(new Event('input'))
6165
}
6266
}
6367
}

src/number-format.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ export default function NumberFormat(config = options) {
88
this.options = Object.assign(options, config)
99
this.input = ''
1010
this.number = ''
11-
this.isClean = false
11+
this.isClean = true
1212

13-
this.isNull = (input = this.input) =>
14-
!this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
13+
this.isNull = (input = this.input) => {
14+
if (this.isClean) {
15+
return !this.numberOnly(input, new RegExp('[^0-9]+', 'gi'))
16+
}
17+
return !this.numberOnly(input, new RegExp('[^0-9\\-]+', 'gi'))
18+
}
1519

1620
this.clean = (clean = false) => {
1721
this.isClean = clean
1822
return this
1923
}
2024

2125
this.sign = () => {
22-
const sign =
23-
this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0
26+
if (this.isClean) {
27+
return this.input.toString().indexOf('-') >= 0 && this.realNumber() > 0
2428
? '-'
2529
: ''
26-
return sign
30+
}
31+
return this.input.toString().indexOf('-') >= 0 ? '-' : ''
2732
}
2833

2934
function between(min, n, max) {
@@ -75,9 +80,9 @@ export default function NumberFormat(config = options) {
7580

7681
this.parts = (number = '', decimal = this.options.decimal) => {
7782
var parts = number.toString().split(decimal)
78-
parts[0] = this.toNumber(parts[0]) || 0
7983

8084
if (parts.length > 1) {
85+
parts[0] = this.toNumber(parts[0]) || 0
8186
parts[1] = parts.slice(1, parts.length).join('')
8287
parts = parts.slice(0, 2)
8388
}

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

+13-33
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ describe('when the value is invalid with custom config', () => {
2626
expect(numberFormat.format('-fo.o-')).toEqual('')
2727
expect(numberFormat.format('!@#$%^&*()')).toEqual('')
2828
})
29-
it('should return as follows', () => {
30-
expect(numberFormat.clean(true).format('')).toEqual('')
31-
expect(numberFormat.clean(true).format('foo')).toEqual('')
32-
expect(numberFormat.clean(true).format('-foo')).toEqual('')
33-
expect(numberFormat.clean(true).format('-fo.o-')).toEqual('')
34-
expect(numberFormat.clean(true).format('-fo,o-')).toEqual('')
35-
expect(numberFormat.clean(true).format('!@#$%^&*()')).toEqual('')
36-
})
3729
it('should return as follows', () => {
3830
expect(numberFormat.clean(true).unformat('')).toEqual('')
3931
expect(numberFormat.clean(true).unformat('foo')).toEqual('')
@@ -53,17 +45,17 @@ describe('format when options are custom', () => {
5345
expect(numberFormat.format()).toEqual('')
5446
expect(numberFormat.format('')).toEqual('')
5547
expect(numberFormat.format('0')).toEqual('$0')
56-
expect(numberFormat.format('0,')).toEqual('$0,')
57-
expect(numberFormat.format('-0,0')).toEqual('$0,0')
58-
expect(numberFormat.format('0,10')).toEqual('$0,10')
59-
expect(numberFormat.format('0,0-')).toEqual('$0,0')
60-
expect(numberFormat.format('0,10-')).toEqual('-$0,10')
61-
expect(numberFormat.format('12.345,54921')).toEqual('$12.345,54921')
62-
expect(numberFormat.format('--12.345,12345')).toEqual('-$12.345,12345')
48+
expect(numberFormat.format('0,')).toEqual('$0')
49+
expect(numberFormat.format('-0,0')).toEqual('$0')
50+
expect(numberFormat.format('0,10')).toEqual('$0,1')
51+
expect(numberFormat.format('0,0-')).toEqual('$0')
52+
expect(numberFormat.format('0,10-')).toEqual('-$0,1')
53+
expect(numberFormat.format('12.345,54921')).toEqual('$12.345,55')
54+
expect(numberFormat.format('--12.345,12345')).toEqual('-$12.345,12')
6355
expect(numberFormat.format('12.345.54321,12945')).toEqual(
64-
'$1.234.554.321,12945'
56+
'$1.234.554.321,13'
6557
)
66-
expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54321')
58+
expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54')
6759
})
6860
it('format numerical value', () => {
6961
expect(numberFormat.format(0)).toEqual('$0')
@@ -72,21 +64,9 @@ describe('format when options are custom', () => {
7264
expect(numberFormat.format(-0.1)).toEqual('-$0,1')
7365
expect(numberFormat.format(-0.0)).toEqual('$0')
7466
expect(numberFormat.format(0.1)).toEqual('$0,1')
75-
expect(numberFormat.format(12345.54921)).toEqual('$12.345,54921')
76-
expect(numberFormat.format(12345.12345)).toEqual('$12.345,12345')
77-
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54321')
78-
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54321')
79-
})
80-
it('format and clean numerical value', () => {
81-
expect(numberFormat.clean(true).format(0)).toEqual('$0')
82-
expect(numberFormat.clean(true).format(0)).toEqual('$0')
83-
expect(numberFormat.clean(true).format(0.0)).toEqual('$0')
84-
expect(numberFormat.clean(true).format(0.1)).toEqual('$0,1')
85-
expect(numberFormat.clean(true).format(-0.0)).toEqual('$0')
86-
expect(numberFormat.clean(true).format(-0.1)).toEqual('-$0,1')
87-
expect(numberFormat.clean(true).format(12345.54921)).toEqual('$12.345,55')
88-
expect(numberFormat.clean(true).format(12345.12345)).toEqual('$12.345,12')
89-
expect(numberFormat.clean(true).format(12345.54321)).toEqual('$12.345,54')
90-
expect(numberFormat.clean(true).format(12345.54321)).toEqual('$12.345,54')
67+
expect(numberFormat.format(12345.54921)).toEqual('$12.345,55')
68+
expect(numberFormat.format(12345.12345)).toEqual('$12.345,12')
69+
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54')
70+
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54')
9171
})
9272
})

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

+13-27
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@ describe('format when options are default', () => {
3636
expect(numberFormat.format()).toEqual('')
3737
expect(numberFormat.format('')).toEqual('')
3838
expect(numberFormat.format('0')).toEqual('0')
39-
expect(numberFormat.format('0.')).toEqual('0.')
40-
expect(numberFormat.format('-0.0')).toEqual('0.0')
41-
expect(numberFormat.format('0.10')).toEqual('0.10')
42-
expect(numberFormat.format('0.0-')).toEqual('0.0')
43-
expect(numberFormat.format('0.10-')).toEqual('-0.10')
44-
expect(numberFormat.format('12,345.54921')).toEqual('12,345.54921')
45-
expect(numberFormat.format('--12,345.12345')).toEqual('-12,345.12345')
46-
expect(numberFormat.format('12,345.54321.12345')).toEqual(
47-
'12,345.5432112345'
48-
)
49-
expect(numberFormat.format('-12,345..54321-')).toEqual('-12,345.54321')
39+
expect(numberFormat.format('0.')).toEqual('0')
40+
expect(numberFormat.format('-0.0')).toEqual('0')
41+
expect(numberFormat.format('0.10')).toEqual('0.1')
42+
expect(numberFormat.format('0.0-')).toEqual('0')
43+
expect(numberFormat.format('0.10-')).toEqual('-0.1')
44+
expect(numberFormat.format('12,345.54921')).toEqual('12,345.55')
45+
expect(numberFormat.format('--12,345.12345')).toEqual('-12,345.12')
46+
expect(numberFormat.format('12,345.54321.12345')).toEqual('12,345.54')
47+
expect(numberFormat.format('-12,345..54321-')).toEqual('-12,345.54')
5048
})
5149
it('format numerical value', () => {
5250
expect(numberFormat.format(0)).toEqual('0')
@@ -55,21 +53,9 @@ describe('format when options are default', () => {
5553
expect(numberFormat.format(-0.1)).toEqual('-0.1')
5654
expect(numberFormat.format(-0.0)).toEqual('0')
5755
expect(numberFormat.format(0.1)).toEqual('0.1')
58-
expect(numberFormat.format(12345.54921)).toEqual('12,345.54921')
59-
expect(numberFormat.format(12345.12345)).toEqual('12,345.12345')
60-
expect(numberFormat.format(12345.54321)).toEqual('12,345.54321')
61-
expect(numberFormat.format(12345.54321)).toEqual('12,345.54321')
62-
})
63-
it('format and clean numerical value', () => {
64-
expect(numberFormat.clean(true).format(0)).toEqual('0')
65-
expect(numberFormat.clean(true).format(0)).toEqual('0')
66-
expect(numberFormat.clean(true).format(0.0)).toEqual('0')
67-
expect(numberFormat.clean(true).format(0.1)).toEqual('0.1')
68-
expect(numberFormat.clean(true).format(-0.0)).toEqual('0')
69-
expect(numberFormat.clean(true).format(-0.1)).toEqual('-0.1')
70-
expect(numberFormat.clean(true).format(12345.54921)).toEqual('12,345.55')
71-
expect(numberFormat.clean(true).format(12345.12345)).toEqual('12,345.12')
72-
expect(numberFormat.clean(true).format(12345.54321)).toEqual('12,345.54')
73-
expect(numberFormat.clean(true).format(12345.54321)).toEqual('12,345.54')
56+
expect(numberFormat.format(12345.54921)).toEqual('12,345.55')
57+
expect(numberFormat.format(12345.12345)).toEqual('12,345.12')
58+
expect(numberFormat.format(12345.54321)).toEqual('12,345.54')
59+
expect(numberFormat.format(12345.54321)).toEqual('12,345.54')
7460
})
7561
})

0 commit comments

Comments
 (0)