feat(settings): unify settings and ui-texts with canonical aliases, add PriceInput thousand separators, and synchronize financial endpoints
Deploy Canina / deploy (push) Successful in 1m46s
Deploy Canina / deploy (push) Successful in 1m46s
This commit is contained in:
1 parent
45c9db133c
commit
b3c60a1a7d
17 files changed
+79340
-78724
No files matched your search
@@ -0,0 +1,99 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
|
||||
interface PriceInputProps {
|
||||
value: number | string;
|
||||
onChange: (rawNumericValue: string) => void;
|
||||
placeholder?: string;
|
||||
className?: string;
|
||||
suffix?: string;
|
||||
disabled?: boolean;
|
||||
required?: boolean;
|
||||
min?: number;
|
||||
max?: number;
|
||||
name?: string;
|
||||
id?: string;
|
||||
}
|
||||
|
||||
// Convert Persian and Arabic digits to English digits
|
||||
export function toEnglishDigits(str: string): string {
|
||||
if (!str) return '';
|
||||
return str
|
||||
.replace(/[۰-۹]/g, (d) => String(d.charCodeAt(0) - 1776))
|
||||
.replace(/[٠-٩]/g, (d) => String(d.charCodeAt(0) - 1632));
|
||||
}
|
||||
|
||||
// Format numbers with thousand comma separator
|
||||
export function formatPriceWithCommas(value: number | string): string {
|
||||
if (value === undefined || value === null || value === '') return '';
|
||||
const cleanStr = toEnglishDigits(String(value)).replace(/[^0-9]/g, '');
|
||||
if (!cleanStr) return '';
|
||||
return Number(cleanStr).toLocaleString('en-US');
|
||||
}
|
||||
|
||||
export default function PriceInput({
|
||||
value,
|
||||
onChange,
|
||||
placeholder = '۰',
|
||||
className = '',
|
||||
suffix = 'تومان',
|
||||
disabled = false,
|
||||
required = false,
|
||||
min,
|
||||
max,
|
||||
name,
|
||||
id,
|
||||
}: PriceInputProps) {
|
||||
const [displayValue, setDisplayValue] = useState<string>('');
|
||||
|
||||
useEffect(() => {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
setDisplayValue('');
|
||||
} else {
|
||||
setDisplayValue(formatPriceWithCommas(value));
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const rawInput = e.target.value;
|
||||
const cleanNumbers = toEnglishDigits(rawInput).replace(/[^0-9]/g, '');
|
||||
|
||||
if (cleanNumbers === '') {
|
||||
setDisplayValue('');
|
||||
onChange('');
|
||||
return;
|
||||
}
|
||||
|
||||
const numVal = Number(cleanNumbers);
|
||||
if (max !== undefined && numVal > max) {
|
||||
return;
|
||||
}
|
||||
|
||||
setDisplayValue(Number(cleanNumbers).toLocaleString('en-US'));
|
||||
onChange(cleanNumbers);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="relative w-full flex items-center">
|
||||
<input
|
||||
type="text"
|
||||
inputMode="numeric"
|
||||
id={id}
|
||||
name={name}
|
||||
disabled={disabled}
|
||||
required={required}
|
||||
value={displayValue}
|
||||
onChange={handleChange}
|
||||
placeholder={placeholder}
|
||||
dir="ltr"
|
||||
className={`w-full text-left font-mono font-bold tracking-wider outline-none transition-all ${
|
||||
suffix ? 'pr-3 pl-16' : 'px-3'
|
||||
} ${className}`}
|
||||
/>
|
||||
{suffix && (
|
||||
<span className="absolute left-3 text-xs font-bold text-gray-400 pointer-events-none select-none font-vazir">
|
||||
{suffix}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user