Skip to content

Commit 117303b

Browse files
Dmitriy Sharshakov aka. sh7dmjohnleider
Dmitriy Sharshakov aka. sh7dm
authored andcommitted
Fixed slider value rounding. Fixes #4575 (#4665)
* fix(v-slider): fix slider value rounding * fix(v-slider): added unit test for value rounding with offset * fix(v-slider spec): fixed unit test
1 parent 43b9e7a commit 117303b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/components/VSlider/VSlider.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,9 @@ export default {
507507
const decimals = trimmedStep.indexOf('.') > -1
508508
? (trimmedStep.length - trimmedStep.indexOf('.') - 1)
509509
: 0
510+
const offset = this.min % this.stepNumeric
510511

511-
const newValue = Math.round(value / this.stepNumeric) * this.stepNumeric
512+
const newValue = Math.round((value - offset) / this.stepNumeric) * this.stepNumeric + offset
512513

513514
return parseFloat(Math.min(newValue, this.max).toFixed(decimals))
514515
},

test/unit/components/VSlider/VSlider.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ test('VSlider.vue', ({ mount }) => {
109109
expect(warning).toHaveBeenTipped()
110110
})
111111

112+
it('should round value with offset correct', async () => {
113+
const wrapper = mount(VSlider, {
114+
propsData: {
115+
min: 3,
116+
max: 15,
117+
step: 3
118+
}
119+
})
120+
121+
const input = jest.fn()
122+
wrapper.vm.$on('input', input)
123+
124+
wrapper.setProps({ value: 5 })
125+
await wrapper.vm.$nextTick()
126+
expect(input).toHaveBeenLastCalledWith(6)
127+
128+
wrapper.setProps({ value: 7 })
129+
await wrapper.vm.$nextTick()
130+
expect(input).toHaveBeenLastCalledWith(6)
131+
132+
expect(warning).toHaveBeenTipped()
133+
})
134+
112135
it('should react to keydown event', async () => {
113136
const wrapper = mount(VSlider, {
114137
propsData: {
@@ -333,6 +356,30 @@ test('VSlider.vue', ({ mount }) => {
333356
expect(warning).toHaveBeenTipped()
334357
})
335358

359+
it('should return a rounded value with offset', () => {
360+
const wrapper = mount(VSlider, {
361+
propsData: { step: 0 }
362+
})
363+
364+
expect(wrapper.vm.roundValue(1.234)).toBe(1.234)
365+
366+
wrapper.setProps({ step: 1, step: 2 })
367+
368+
expect(wrapper.vm.roundValue(1.234)).toBe(2)
369+
370+
wrapper.setProps({ step: 4, min: 2 })
371+
372+
expect(wrapper.vm.roundValue(5.667)).toBe(6)
373+
374+
expect(wrapper.vm.roundValue(7.667)).toBe(6)
375+
376+
wrapper.setProps({ step: 2.5, min: 5 })
377+
378+
expect(wrapper.vm.roundValue(5.667)).toBe(5)
379+
380+
expect(warning).toHaveBeenTipped()
381+
})
382+
336383
it('should not update if value matches lazy value', () => {
337384
const validate = jest.fn()
338385
const wrapper = mount(VSlider, {

0 commit comments

Comments
 (0)