فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(q01): preserve fractional quantities and Persian digits in numeric inputs
davoodmehraban89 committed
Oct 31, 2026 at 18:14 UTC
78e806f454a09dfa64513ec4addf2e1d2b2f38b4
1 file changed
+75
-73
src/utils/NumberInput.js
+75
-73
@@ -1,59 +1,86 @@
1
// ============================================================
2
-// NumberInput — جداکنندهی هزارگان زنده برای input ها
2
+// NumberInput — locale-aware grouped numeric inputs
3
+// Keep fractional stock quantities and Persian/Arabic digits intact.
4
// ============================================================
5
6
+// Normalize numeric glyphs without converting the value to Number first:
7
+// doing so would round fractional quantities and large integer strings.
8
+function normalizeNumeric(value) {
9
+ return String(value)
10
+ .replace(/[۰-۹]/g, digit => String(digit.charCodeAt(0) - 1776))
11
+ .replace(/[٠-٩]/g, digit => String(digit.charCodeAt(0) - 1632))
12
+ .replace(/٫/g, '.')
13
+ .replace(/−/g, '-')
14
+ .replace(/[,،٬\s\u00a0\u202f]/g, '');
15
+}
16
+
17
+function groupInteger(value) {
18
+ return value.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
19
+}
20
+
21
+function formatNormalized(value) {
22
+ const match = /^(-?)(\d+)(\.(\d*))?$/.exec(value);
23
+ if (!match) return '';
24
+ return match[1] + groupInteger(match[2]) + (match[3] ?? '');
25
+}
26
+
27
export const NumberInput = {
6
- // فرمت کردن عدد → "1,234,567"
7
- format(n) {
8
- if (n === '' || n === null || n === undefined) return '';
9
- const num = Math.round(Number(n));
10
- if (isNaN(num)) return '';
11
- return num.toLocaleString('en-US');
28
+ // Format without rounding valid decimal quantities (for example, 2.375).
29
+ format(value) {
30
+ if (value === '' || value === null || value === undefined) return '';
31
+ const normalized = normalizeNumeric(value);
32
+ const formatted = formatNormalized(normalized);
33
+ if (formatted) return formatted;
34
+ // Retain the historic empty-string behavior for malformed input.
35
+ return '';
36
},
37
14
- // تبدیل رشته یا element → عدد خالص
38
+ // Read an element or text with Persian and Arabic digit support.
39
+ // Empty/invalid inputs retain the existing 0 fallback for compatibility.
40
parse(input) {
41
if (input === null || input === undefined) return 0;
17
- const s = typeof input === 'string' ? input : (input.value || '');
18
- const cleaned = String(s).replace(/[^\d.-]/g, '');
19
- return Number(cleaned) || 0;
42
+ const raw = typeof input === 'string' || typeof input === 'number'
43
+ ? input : (input.value ?? '');
44
+ const normalized = normalizeNumeric(raw);
45
+ if (!/^-?(?:\d+(?:\.\d+)?|\.\d+)$/.test(normalized)) return 0;
46
+ const number = Number(normalized);
47
+ return Number.isFinite(number) ? number : 0;
48
},
49
22
- // خواندن مقدار عددی از element
50
value(el) {
51
return this.parse(el);
52
},
53
27
- // فرمتدهی زنده با حفظ موقعیت cursor
54
+ // Format while typing; do not delete decimal separators or fractional digits.
55
_formatLive(el) {
29
- const cursorStart = el.selectionStart;
30
- const oldValue = el.value;
31
-
32
- let raw = oldValue.replace(/[^\d]/g, '');
33
- if (raw.length > 1) raw = raw.replace(/^0+/, '');
34
-
35
- const formatted = raw ? Number(raw).toLocaleString('en-US') : '';
36
- if (formatted === oldValue) return;
37
-
38
- const digitsBefore = oldValue.slice(0, cursorStart).replace(/\D/g, '').length;
39
-
40
- el.value = formatted;
41
-
42
- if (digitsBefore === 0) {
43
- el.setSelectionRange(0, 0);
44
- } else if (digitsBefore >= raw.length) {
45
- el.setSelectionRange(formatted.length, formatted.length);
46
- } else {
47
- let pos = 0, count = 0;
48
- for (let i = 0; i < formatted.length; i++) {
49
- if (/\d/.test(formatted[i])) count++;
50
- if (count === digitsBefore) { pos = i + 1; break; }
51
- }
52
- el.setSelectionRange(pos, pos);
56
+ const cursorStart = el.selectionStart ?? el.value.length;
57
+ const oldValue = String(el.value);
58
+ const normalized = normalizeNumeric(oldValue);
59
+ const negative = normalized.startsWith('-');
60
+ const numeric = normalized.replace(/[^\d.]/g, '');
61
+ const dot = numeric.indexOf('.');
62
+ const hasDecimal = dot !== -1;
63
+ let whole = (hasDecimal ? numeric.slice(0, dot) : numeric).replace(/^0+(?=\d)/, '');
64
+ if (!whole && hasDecimal) whole = '0';
65
+ const fractional = hasDecimal ? numeric.slice(dot + 1).replace(/\./g, '') : '';
66
+ const result = (negative ? '-' : '') + (whole ? groupInteger(whole) : '') +
67
+ (hasDecimal ? '.' + fractional : '');
68
+ if (result === oldValue) return;
69
+
70
+ // Count meaningful tokens before the caret, ignoring thousands separators.
71
+ const before = normalizeNumeric(oldValue.slice(0, cursorStart))
72
+ .replace(/[^\d.\-]/g, '').length;
73
+ let caret = 0;
74
+ let seen = 0;
75
+ for (; caret < result.length && seen < before; caret++) {
76
+ if (/[\d.\-]/.test(result[caret])) seen++;
77
+ }
78
+ el.value = result;
79
+ if (typeof el.setSelectionRange === 'function') {
80
+ el.setSelectionRange(caret, caret);
81
}
82
},
83
56
- // بررسی اینکه آیا این input باید فرمت بشه؟
84
_shouldFormat(el) {
85
if (!el || el.tagName !== 'INPUT') return false;
86
if (el.type === 'number') return true;
@@ -62,65 +89,40 @@ export const NumberInput = {
89
return false;
90
},
91
65
- // تبدیل و علامتگذاری input ها
92
_markAndFormat(el) {
93
if (!this._shouldFormat(el)) return;
68
-
69
- // تبدیل number به text
70
- if (el.type === 'number') {
71
- el.type = 'text';
72
- }
73
-
74
- // علامتگذاری
94
+ if (el.type === 'number') el.type = 'text';
95
el.setAttribute('data-money', '1');
96
el.setAttribute('inputmode', 'numeric');
97
el.setAttribute('autocomplete', 'off');
78
-
79
- // فرمت کردن مقدار اولیه اگه خالص عددی بود
80
- const raw = (el.value || '').replace(/,/g, '').trim();
81
- if (raw && /^-?\d+$/.test(raw) && raw !== '0') {
82
- el.value = this.format(raw);
83
- } else if (raw === '0') {
84
- // 0 رو نگهدار بدون فرمت اضافی
85
- el.value = '0';
98
+ const normalized = normalizeNumeric(el.value || '');
99
+ if (/^-?\d+(?:\.\d+)?$/.test(normalized)) {
100
+ el.value = this.format(normalized);
101
}
102
},
103
89
- // اسکن کل DOM
104
_convertAll() {
105
document.querySelectorAll('input').forEach(el => {
92
- if (this._shouldFormat(el)) {
93
- this._markAndFormat(el);
94
- }
106
+ if (this._shouldFormat(el)) this._markAndFormat(el);
107
});
108
},
109
110
init() {
99
- // تبدیل اولیه
111
this._convertAll();
101
-
102
- // ناظر برای ورودیهای جدید (مودالها، ردیفهای جدید و...)
103
- const observer = new MutationObserver(() => {
104
- this._convertAll();
105
- });
112
+ const observer = new MutationObserver(() => { this._convertAll(); });
113
observer.observe(document.body, { childList: true, subtree: true });
114
108
- // فرمتدهی زنده در هنگام تایپ
109
- document.addEventListener('input', (e) => {
115
+ document.addEventListener('input', e => {
116
const el = e.target;
117
if (!el || !el.matches || !el.matches('input')) return;
118
if (el.getAttribute('data-money') !== '1') return;
119
this._formatLive(el);
120
});
115
-
116
- // پاک کردن مقدار صفر در ورودی اگه کاربر کلیک کرد و تایپ کرد
117
- document.addEventListener('focus', (e) => {
121
+ document.addEventListener('focus', e => {
122
const el = e.target;
123
if (!el || !el.matches || !el.matches('input')) return;
124
if (el.getAttribute('data-money') !== '1') return;
121
- if (el.value === '0') {
122
- el.value = '';
123
- }
125
+ if (el.value === '0') el.value = '';
126
}, true);
127
}
126
-};
\ No newline at end of file
128
+};