import { useState, type ButtonHTMLAttributes, type ChangeEvent, type InputHTMLAttributes, type ReactNode, type SelectHTMLAttributes } from "react"; import { PersianDateInput } from "./PersianDateInput"; export function Card({ children, className = "" }: { children: ReactNode; className?: string }) { return
{children}
; } export function Field({ label, hint, error, children }: { label: string; hint?: string; error?: string; children: ReactNode }) { const unit = hint?.startsWith("واحد:") ? hint.slice("واحد:".length).trim() : undefined; return ; } type InputProps = InputHTMLAttributes & { groupDigits?: boolean }; const latinDigits = (value: string) => value .replace(/[۰-۹]/g, digit => String("۰۱۲۳۴۵۶۷۸۹".indexOf(digit))) .replace(/[٠-٩]/g, digit => String("٠١٢٣٤٥٦٧٨٩".indexOf(digit))); function rawNumber(value: unknown) { const source = latinDigits(value == null ? "" : String(value)).replace(/[٬,\s]/g, ""); const sign = source.startsWith("-") ? "-" : ""; const unsigned = source.replace(/-/g, "").replace(/[^\d.]/g, ""); const [integer = "", ...fractions] = unsigned.split("."); return sign + integer + (unsigned.includes(".") ? `.${fractions.join("")}` : ""); } function groupedNumber(value: string) { if (!value || value === "-") return value; const sign = value.startsWith("-") ? "-" : ""; const unsigned = sign ? value.slice(1) : value; const [integer, fraction] = unsigned.split("."); const grouped = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ","); return `${sign}${grouped}${fraction === undefined ? "" : `.${fraction}`}`; } function GroupedNumberInput({ name, value, defaultValue, onChange, className, ...props }: InputProps) { const [uncontrolledValue, setUncontrolledValue] = useState(() => rawNumber(defaultValue)); const controlled = value !== undefined; const rawValue = controlled ? rawNumber(value) : uncontrolledValue; const handleChange = (event: ChangeEvent) => { const next = rawNumber(event.target.value); if (!controlled) setUncontrolledValue(next); if (onChange) { onChange({ ...event, target: { ...event.target, value: next }, currentTarget: { ...event.currentTarget, value: next }, } as ChangeEvent); } }; return <> {name && } ; } export function Input({ groupDigits = false, ...props }: InputProps) { if (props.type === "date") { const { name, value, defaultValue, required, disabled, placeholder, onChange } = props; return onChange?.({ target: { value: next, name: name ?? "" } } as ChangeEvent)} />; } if (groupDigits) return ; return ; } export function Select(props: SelectHTMLAttributes) { return