|
| 1 | +import { watch } from 'vue'; |
| 2 | + |
| 3 | +import { useI18n } from '@/locales/helpers.ts'; |
| 4 | +import { type CommonNumberInputProps, useCommonNumberInputBase } from '@/components/base/CommonNumberInputBase.ts'; |
| 5 | + |
| 6 | +import { isNumber, replaceAll, removeAll } from '@/lib/common.ts'; |
| 7 | + |
| 8 | +export interface NumberInputProps extends CommonNumberInputProps { |
| 9 | + minValue?: number; |
| 10 | + maxValue?: number; |
| 11 | + maxDecimalCount?: number; |
| 12 | +} |
| 13 | + |
| 14 | +export interface NumberInputEmits { |
| 15 | + (e: 'update:modelValue', value: number): void; |
| 16 | +} |
| 17 | + |
| 18 | +export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmits) { |
| 19 | + const { |
| 20 | + getCurrentDecimalSeparator, |
| 21 | + getCurrentDigitGroupingSymbol |
| 22 | + } = useI18n(); |
| 23 | + |
| 24 | + const { |
| 25 | + currentValue, |
| 26 | + onKeyUpDown, |
| 27 | + onPaste |
| 28 | + } = useCommonNumberInputBase(props, props.maxDecimalCount ?? -1, getFormattedValue(props.modelValue), parseNumber, getFormattedValue, getValidFormattedValue); |
| 29 | + |
| 30 | + function parseNumber(value: string): number { |
| 31 | + if (!value) { |
| 32 | + return 0; |
| 33 | + } |
| 34 | + |
| 35 | + const decimalSeparator = getCurrentDecimalSeparator(); |
| 36 | + |
| 37 | + let finalValue = ''; |
| 38 | + for (let i = 0; i < value.length; i++) { |
| 39 | + if (!('0' <= value[i] && value[i] <= '9') && value[i] !== '-' && value[i] !== decimalSeparator) { |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + finalValue += value[i]; |
| 44 | + } |
| 45 | + |
| 46 | + if (decimalSeparator !== '.') { |
| 47 | + finalValue = replaceAll(finalValue, decimalSeparator, '.'); |
| 48 | + } |
| 49 | + |
| 50 | + return parseFloat(finalValue); |
| 51 | + } |
| 52 | + |
| 53 | + function getValidFormattedValue(value: number, textualValue: string): string { |
| 54 | + if (isNumber(props.minValue) && value < props.minValue) { |
| 55 | + return getFormattedValue(props.minValue); |
| 56 | + } |
| 57 | + |
| 58 | + if (isNumber(props.maxValue) && value > props.maxValue) { |
| 59 | + return getFormattedValue(props.maxValue); |
| 60 | + } |
| 61 | + |
| 62 | + const decimalSeparator = getCurrentDecimalSeparator(); |
| 63 | + const digitGroupingSymbol = getCurrentDigitGroupingSymbol(); |
| 64 | + return replaceAll(removeAll(textualValue, digitGroupingSymbol), '.', decimalSeparator); |
| 65 | + } |
| 66 | + |
| 67 | + function getFormattedValue(value: number): string { |
| 68 | + if (!Number.isNaN(value) && Number.isFinite(value)) { |
| 69 | + const decimalSeparator = getCurrentDecimalSeparator(); |
| 70 | + |
| 71 | + if (isNumber(props.maxDecimalCount) && props.maxDecimalCount >= 0) { |
| 72 | + return replaceAll(value.toFixed(props.maxDecimalCount), '.', decimalSeparator); |
| 73 | + } else { |
| 74 | + return replaceAll(value.toString(), '.', decimalSeparator); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return '0'; |
| 79 | + } |
| 80 | + |
| 81 | + watch(() => props.modelValue, (newValue) => { |
| 82 | + const numericCurrentValue = parseNumber(currentValue.value); |
| 83 | + |
| 84 | + if (newValue !== numericCurrentValue) { |
| 85 | + const newStringValue = getFormattedValue(newValue); |
| 86 | + |
| 87 | + if (!(newStringValue === '0' && currentValue.value === '')) { |
| 88 | + currentValue.value = newStringValue; |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + watch(currentValue, (newValue) => { |
| 94 | + let finalValue = ''; |
| 95 | + |
| 96 | + if (newValue) { |
| 97 | + const decimalSeparator = getCurrentDecimalSeparator(); |
| 98 | + |
| 99 | + for (let i = 0; i < newValue.length; i++) { |
| 100 | + if (!('0' <= newValue[i] && newValue[i] <= '9') && newValue[i] !== '-' && newValue[i] !== decimalSeparator) { |
| 101 | + break; |
| 102 | + } |
| 103 | + |
| 104 | + finalValue += newValue[i]; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (finalValue !== newValue) { |
| 109 | + currentValue.value = finalValue; |
| 110 | + } else { |
| 111 | + const value: number = parseNumber(finalValue); |
| 112 | + emit('update:modelValue', value); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + return { |
| 117 | + // states |
| 118 | + currentValue, |
| 119 | + // functions |
| 120 | + onKeyUpDown, |
| 121 | + onPaste |
| 122 | + } |
| 123 | +} |
0 commit comments