fixed some params and ui

This commit is contained in:
Jelaletdin12
2026-08-25 22:58:08 +05:00
parent f00d2abcac
commit f293806d96
25 changed files with 547 additions and 87 deletions

View File

@@ -86,6 +86,73 @@ const withError = (error?: string) => (error ? "border-destructive focus-visible
// ─── TextInput ────────────────────────────────────────────────────────────────
// ─── formatNumber helpers ─────────────────────────────────────────────────────
/** "1000000.50" → "1 000 000.50" (integer part only gets spaces) */
function formatNumberDisplay(raw: string): string {
if (!raw) return "";
const [intPart, decPart] = raw.split(".");
const formatted = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, " ");
return decPart !== undefined ? `${formatted}.${decPart}` : formatted;
}
/** "1 000 000.50" → "1000000.50" */
function stripNumberFormat(display: string): string {
return display.replace(/ /g, "");
}
// ─── NumberInput ──────────────────────────────────────────────────────────────
function NumberInput({ value = "", onChange, placeholder, error, disabled, required, label, className, name, readOnly }: FormInputProps) {
// Display state — formatted for the user to see
const [display, setDisplay] = useState(() => formatNumberDisplay(value));
// Sync display when value changes externally (e.g. form reset)
useEffect(() => {
const current = stripNumberFormat(display);
if (current !== value) {
setDisplay(formatNumberDisplay(value));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const raw = e.target.value;
// Allow only digits, dots, and spaces (we manage spaces ourselves)
const stripped = raw.replace(/ /g, "");
if (stripped !== "" && !/^\d*\.?\d*$/.test(stripped)) return;
if (stripped !== "" && Number(stripped) < 0) return;
setDisplay(formatNumberDisplay(stripped));
onChange?.(stripped);
};
return (
<div className={className}>
<FieldLabel label={label} required={required} />
<div className="relative">
<Input
name={name}
type="text"
inputMode="decimal"
value={display}
onChange={handleChange}
placeholder={placeholder}
disabled={disabled}
readOnly={readOnly}
className={cn("pr-10", withError(error))}
/>
<Hash size={14} className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground pointer-events-none" />
</div>
<ErrorMsg error={error} />
</div>
);
}
// ─── TextInput ────────────────────────────────────────────────────────────────
function TextInput({
type = "text",
value = "",
@@ -104,7 +171,6 @@ function TextInput({
const isPassword = type === "password";
const isTextarea = type === "textarea";
const isNumber = type === "number";
return (
<div className={className}>
@@ -125,21 +191,13 @@ function TextInput({
) : (
<Input
name={name}
type={isPassword ? (showPass ? "text" : "password") : isNumber ? "number" : type === "email" ? "email" : "text"}
min={isNumber ? 0 : undefined}
type={isPassword ? (showPass ? "text" : "password") : type === "email" ? "email" : "text"}
value={value}
onChange={(e) => {
if (isNumber) {
const num = e.target.value;
if (num === "" || Number(num) >= 0) onChange?.(num);
} else {
onChange?.(e.target.value);
}
}}
onChange={(e) => onChange?.(e.target.value)}
placeholder={placeholder}
disabled={disabled}
readOnly={readOnly}
className={cn(isPassword && "pr-10", isNumber && "pr-10", withError(error))}
className={cn(isPassword && "pr-10", withError(error))}
/>
)}
@@ -153,8 +211,6 @@ function TextInput({
{showPass ? <EyeOff size={15} /> : <Eye size={15} />}
</button>
)}
{isNumber && <Hash size={14} className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground pointer-events-none" />}
</div>
<ErrorMsg error={error} />
@@ -667,6 +723,7 @@ function FileInput({
export function FormInput(props: FormInputProps) {
const { type = "text" } = props;
if (type === "number") return <NumberInput {...props} />;
if (type === "phone") return <PhoneInput {...props} />;
if (type === "date") return <DateInput {...props} />;
if (type === "select") return <SelectInput {...props} />;