Line data Source code
1 : import { computed, toValue } from 'vue'
2 : import type { MaybeRefOrGetter } from 'vue'
3 :
4 : interface NumberWidgetOptions {
5 : step?: number
6 : step2?: number
7 : precision?: number
8 : }
9 :
10 8419 : export function useNumberStepCalculation(
11 : optionsArg: MaybeRefOrGetter<NumberWidgetOptions | undefined>,
12 : precisionArg: MaybeRefOrGetter<number | undefined>,
13 : returnUndefinedForDefault = false
14 : ) {
15 8419 : return computed(() => {
16 12032 : const options = toValue(optionsArg)
17 12032 : const precision = toValue(precisionArg)
18 12032 : if (options?.step2 !== undefined) {
19 12003 : return options.step2
20 : }
21 12032 : const step = options?.step
22 12032 : if (step !== undefined && step > 10) {
23 0 : return step / 10
24 : }
25 :
26 177 : if (precision === undefined) {
27 27 : return returnUndefinedForDefault ? undefined : 0
28 : }
29 :
30 150 : if (precision === 0) return 1
31 :
32 2 : const calculatedStep = 1 / Math.pow(10, precision)
33 12032 : return returnUndefinedForDefault
34 : ? calculatedStep
35 : : Number(calculatedStep.toFixed(precision))
36 : })
37 : }
|