mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 07:27:33 +08:00
support changing numeral system
This commit is contained in:
@@ -4,6 +4,7 @@ import { useI18n } from '@/locales/helpers.ts';
|
||||
import { type CommonNumberInputProps, useCommonNumberInputBase } from '@/components/base/CommonNumberInputBase.ts';
|
||||
|
||||
import { isNumber, replaceAll, removeAll } from '@/lib/common.ts';
|
||||
import { NumeralSystem } from '@/core/numeral.ts';
|
||||
|
||||
export interface NumberInputProps extends CommonNumberInputProps {
|
||||
minValue?: number;
|
||||
@@ -17,6 +18,7 @@ export interface NumberInputEmits {
|
||||
|
||||
export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmits) {
|
||||
const {
|
||||
getCurrentNumeralSystemType,
|
||||
getCurrentDecimalSeparator,
|
||||
getCurrentDigitGroupingSymbol
|
||||
} = useI18n();
|
||||
@@ -32,17 +34,20 @@ export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmi
|
||||
return 0;
|
||||
}
|
||||
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
let finalValue = '';
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (!('0' <= value[i] && value[i] <= '9') && value[i] !== '-' && value[i] !== decimalSeparator) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(value[i]) && !numeralSystem.isDigit(value[i]) && value[i] !== '-' && value[i] !== decimalSeparator) {
|
||||
break;
|
||||
}
|
||||
|
||||
finalValue += value[i];
|
||||
}
|
||||
|
||||
finalValue = numeralSystem.replaceLocalizedDigitsToWesternArabicDigits(finalValue);
|
||||
|
||||
if (decimalSeparator !== '.') {
|
||||
finalValue = replaceAll(finalValue, decimalSeparator, '.');
|
||||
}
|
||||
@@ -65,50 +70,72 @@ export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmi
|
||||
}
|
||||
|
||||
function getFormattedValue(value: number): string {
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
|
||||
if (!Number.isNaN(value) && Number.isFinite(value)) {
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
if (isNumber(props.maxDecimalCount) && props.maxDecimalCount >= 0) {
|
||||
return replaceAll(value.toFixed(props.maxDecimalCount), '.', decimalSeparator);
|
||||
return replaceAll(numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(value.toFixed(props.maxDecimalCount)), '.', decimalSeparator);
|
||||
} else {
|
||||
return replaceAll(value.toString(), '.', decimalSeparator);
|
||||
return replaceAll(numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(value.toString(10)), '.', decimalSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
return '0';
|
||||
return numeralSystem.digitZero;
|
||||
}
|
||||
|
||||
watch(() => props.modelValue, (newValue) => {
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
const numericCurrentValue = parseNumber(currentValue.value);
|
||||
|
||||
if (newValue !== numericCurrentValue) {
|
||||
const newStringValue = getFormattedValue(newValue);
|
||||
|
||||
if (!(newStringValue === '0' && currentValue.value === '')) {
|
||||
if (!(newStringValue === numeralSystem.digitZero && currentValue.value === '')) {
|
||||
currentValue.value = newStringValue;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
watch(currentValue, (newValue) => {
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
let actualNumeralSystem: NumeralSystem | undefined = undefined;
|
||||
let finalValue = '';
|
||||
|
||||
if (newValue) {
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
for (let i = 0; i < newValue.length; i++) {
|
||||
if (!('0' <= newValue[i] && newValue[i] <= '9') && newValue[i] !== '-' && newValue[i] !== decimalSeparator) {
|
||||
break;
|
||||
if (newValue[0] === '-' || newValue[0] === decimalSeparator) {
|
||||
actualNumeralSystem = NumeralSystem.detect(newValue[1]);
|
||||
} else {
|
||||
actualNumeralSystem = NumeralSystem.detect(newValue[0]);
|
||||
}
|
||||
|
||||
if (actualNumeralSystem && (actualNumeralSystem.type === NumeralSystem.WesternArabicNumerals.type || actualNumeralSystem.type === numeralSystem.type)) {
|
||||
for (let i = 0; i < newValue.length; i++) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(newValue[i]) && !numeralSystem.isDigit(newValue[i]) && newValue[i] !== '-' && newValue[i] !== decimalSeparator) {
|
||||
break;
|
||||
}
|
||||
|
||||
finalValue += newValue[i];
|
||||
}
|
||||
|
||||
finalValue += newValue[i];
|
||||
finalValue = numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(finalValue);
|
||||
} else if (newValue === '-' || newValue === decimalSeparator || newValue === `-${decimalSeparator}`) {
|
||||
finalValue = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
if (finalValue !== newValue) {
|
||||
currentValue.value = finalValue;
|
||||
} else {
|
||||
const value: number = parseNumber(finalValue);
|
||||
let value: number = parseNumber(finalValue);
|
||||
|
||||
if (Number.isNaN(value) || !Number.isFinite(value)) {
|
||||
value = 0;
|
||||
}
|
||||
|
||||
emit('update:modelValue', value);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user