152
152
<script lang="ts">
153
153
import { defineComponent , PropType } from " vue" ;
154
154
155
+ import { clamp } from " @/utilities/math" ;
156
+
155
157
export type IncrementBehavior = " Add" | " Multiply" | " Callback" | " None" ;
156
158
export type IncrementDirection = " Decrease" | " Increase" ;
157
159
@@ -240,13 +242,21 @@ export default defineComponent({
240
242
if (invalid ) sanitized = this .value ;
241
243
242
244
if (this .isInteger ) sanitized = Math .round (sanitized );
243
- if (typeof this .min === " number" && ! Number .isNaN (this .min )) sanitized = Math .max (sanitized , this .min );
244
- if (typeof this .max === " number" && ! Number .isNaN (this .max )) sanitized = Math .min (sanitized , this .max );
245
+ sanitized = clamp (newValue , this .min , this .max );
245
246
246
247
if (! invalid ) this .$emit (" update:value" , sanitized );
247
248
248
- const roundingPower = 10 ** this .displayDecimalPlaces ;
249
- const displayValue = Math .round (sanitized * roundingPower ) / roundingPower ;
249
+ this .setText (sanitized );
250
+ },
251
+ setText(value : number ) {
252
+ // Find the amount of digits on the left side of the Decimal
253
+ // 10.25 == 2
254
+ // 1.23 == 1
255
+ // 0.23 == 0 - Reason for the slightly more complicated code
256
+ const leftSideDigits = Math .max (Math .floor (value ).toString ().length , 0 ) * Math .sign (value );
257
+
258
+ const roundingPower = 10 ** Math .max (this .displayDecimalPlaces - leftSideDigits , 0 );
259
+ const displayValue = Math .round (value * roundingPower ) / roundingPower ;
250
260
this .text = ` ${displayValue }${this .unit } ` ;
251
261
},
252
262
},
@@ -258,13 +268,9 @@ export default defineComponent({
258
268
return ;
259
269
}
260
270
261
- let sanitized = newValue ;
262
- if (typeof this .min === " number" ) sanitized = Math .max (sanitized , this .min );
263
- if (typeof this .max === " number" ) sanitized = Math .min (sanitized , this .max );
271
+ const sanitized = clamp (newValue , this .min , this .max );
264
272
265
- const roundingPower = 10 ** this .displayDecimalPlaces ;
266
- const displayValue = Math .round (sanitized * roundingPower ) / roundingPower ;
267
- this .text = ` ${displayValue }${this .unit } ` ;
273
+ this .setText (sanitized );
268
274
},
269
275
},
270
276
mounted() {
0 commit comments