Skip to content

Commit 0a1c3ad

Browse files
committed
fix no precision issue
1 parent caafcfb commit 0a1c3ad

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

src/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export function inputHandler(event: CustomInputEvent) {
157157
positionFromEnd = target.value.length - target.selectionEnd
158158
}
159159

160-
updateValue(target, null, { emit: false })
160+
updateValue(target, null, { emit: false, clean: !options.precision })
161161

162162
// updated cursor position
163163
if (options.suffix) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { mount } from '@vue/test-utils'
2+
import { component as VueNumber } from '../../src'
3+
4+
describe('VueNumber', () => {
5+
it('renders an input element', () => {
6+
const wrapper = mount(VueNumber, {
7+
propsData: {
8+
value: '123',
9+
precision: 0
10+
}
11+
})
12+
expect(wrapper.contains('input')).toBe(true)
13+
})
14+
15+
test('should emit input event with the new maskedValue and unmaskedValue on input', async () => {
16+
const wrapper = mount(VueNumber, {
17+
propsData: {
18+
value: 123456.893,
19+
precision: 0
20+
}
21+
})
22+
23+
const input = wrapper.find('input')
24+
expect(input.element.value).toBe('123,457')
25+
26+
input.element.value = 123457.89
27+
await input.trigger('input')
28+
expect(wrapper.vm.unmaskedValue).toBe('123458')
29+
expect(wrapper.vm.maskedValue).toBe('123,458')
30+
31+
await input.trigger('blur')
32+
expect(wrapper.vm.unmaskedValue).toBe('123458')
33+
expect(wrapper.vm.maskedValue).toBe('123,458')
34+
35+
input.element.value = '1234.568'
36+
await input.trigger('input')
37+
expect(wrapper.vm.unmaskedValue).toBe('1235')
38+
expect(wrapper.vm.maskedValue).toBe('1,235')
39+
40+
await input.trigger('blur')
41+
expect(wrapper.vm.unmaskedValue).toBe('1235')
42+
expect(wrapper.vm.maskedValue).toBe('1,235')
43+
})
44+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { mount } from '@vue/test-utils'
2+
import { directive } from '../../src'
3+
4+
describe('v-number directive', () => {
5+
it('should emit input event with the new value on input', async () => {
6+
const wrapper = mount({
7+
template: `<input v-number="options" type="text" v-model="value" />`,
8+
directives: {
9+
number: directive
10+
},
11+
data() {
12+
return {
13+
value: '1234.536',
14+
options: {
15+
precision: 0
16+
}
17+
}
18+
}
19+
})
20+
21+
const input = wrapper.find('input')
22+
expect(input.element.value).toBe('1,235')
23+
24+
input.element.value = '1234.529'
25+
await input.trigger('input')
26+
expect(input.element.value).toBe('1,235')
27+
28+
await input.trigger('blur')
29+
expect(input.element.value).toBe('1,235')
30+
expect(input.element.oldValue).toBe('1,235')
31+
})
32+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NumberFormat } from '../../src'
2+
import { expect, test } from 'vitest'
3+
4+
test('format when options are custom', () => {
5+
const numberFormat = new NumberFormat({
6+
precision: 0
7+
})
8+
expect(numberFormat.format(12345.54921)).toEqual('12,346')
9+
expect(numberFormat.format(12345.12345)).toEqual('12,345')
10+
expect(numberFormat.format(12345.54321)).toEqual('12,346')
11+
expect(numberFormat.format(12345.54321)).toEqual('12,346')
12+
})

0 commit comments

Comments
 (0)