Skip to content

Commit 86a3f11

Browse files
authored
Update form-spinbutton.js
1 parent 3c9b72e commit 86a3f11

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/components/form-spinbutton/form-spinbutton.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,20 @@ export const BFormSpinbutton = /*#__PURE__*/ Vue.extend({
151151
}
152152
},
153153
methods: {
154-
setValue(value) {
155-
if (!this.disabled) {
154+
stepValue(direction) {
155+
// Sets a new incremented or decremented value, supporting optional wrapping
156+
// Direction is either +1 or -1
157+
let value = this.localValue
158+
if (!this.disabled && !isNull(value)) {
159+
const step = this.computedStep * direction
156160
const min = this.computedMin
157161
const max = this.computedMax
162+
const mult = this.computedMult
158163
const wrap = this.wrap
164+
// We ensure that the value steps like a native input
165+
value = Math.round((value - min) / step) * step + min + step)
166+
// We ensure that precision is maintained (decimals)
167+
value = Math.round(value * mult) / mult
159168
this.localValue =
160169
value > max ? (wrap ? min : max) : value < min ? (wrap ? max : min) : value
161170
}
@@ -172,21 +181,15 @@ export const BFormSpinbutton = /*#__PURE__*/ Vue.extend({
172181
if (isNull(value)) {
173182
this.localValue = this.computedMin
174183
} else {
175-
const step = this.computedStep
176-
const mult = this.computedMult
177-
// We ensure that precision is maintained
178-
this.setValue(Math.floor(value * mult + step * mult) / mult)
184+
this.stepValue(+1)
179185
}
180186
},
181187
decrement() {
182188
const value = this.localValue
183189
if (isNull(value)) {
184190
this.localValue = this.wrap ? this.computedMax : this.computedMin
185191
} else {
186-
const step = this.computedStep
187-
const mult = this.computedMult
188-
// We ensure that precision is maintained
189-
this.setValue(Math.floor(value * mult - step * mult) / mult)
192+
this.stepValue(-1)
190193
}
191194
},
192195
onKeydown(evt) {

0 commit comments

Comments
 (0)