95 lines
4.3 KiB
TypeScript
95 lines
4.3 KiB
TypeScript
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 <section className={`card ${className}`}>{children}</section>;
|
||
}
|
||
|
||
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 <label className={`field ${error ? "has-error" : ""}`}><span className="field-label">{label}{unit && <em>{unit}</em>}</span>{children}{hint && !unit && <small>{hint}</small>}{error && <small className="field-error">{error}</small>}</label>;
|
||
}
|
||
|
||
type InputProps = InputHTMLAttributes<HTMLInputElement> & { 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<HTMLInputElement>) => {
|
||
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<HTMLInputElement>);
|
||
}
|
||
};
|
||
return <>
|
||
<input
|
||
{...props}
|
||
type="text"
|
||
className={className ?? "input"}
|
||
inputMode="decimal"
|
||
dir="ltr"
|
||
value={groupedNumber(rawValue)}
|
||
onChange={handleChange}
|
||
/>
|
||
{name && <input type="hidden" name={name} value={rawValue}/>}
|
||
</>;
|
||
}
|
||
|
||
export function Input({ groupDigits = false, ...props }: InputProps) {
|
||
if (props.type === "date") {
|
||
const { name, value, defaultValue, required, disabled, placeholder, onChange } = props;
|
||
return <PersianDateInput
|
||
name={name}
|
||
value={value === undefined ? undefined : String(value)}
|
||
defaultValue={defaultValue === undefined ? undefined : String(defaultValue)}
|
||
required={required}
|
||
disabled={disabled}
|
||
placeholder={placeholder}
|
||
onChange={next => onChange?.({ target: { value: next, name: name ?? "" } } as ChangeEvent<HTMLInputElement>)}
|
||
/>;
|
||
}
|
||
if (groupDigits) return <GroupedNumberInput {...props}/>;
|
||
return <input className="input" {...props}/>;
|
||
}
|
||
export function Select(props: SelectHTMLAttributes<HTMLSelectElement>) { return <select className="input" {...props}/>; }
|
||
export function Button({ className = "", ...props }: ButtonHTMLAttributes<HTMLButtonElement>) { return <button className={`button ${className}`} {...props}/>; }
|
||
|
||
export function PageTitle({ title, description, action }: { title: string; description: string; action?: ReactNode }) {
|
||
return <div className="page-title"><div><h2>{title}</h2><p>{description}</p></div>{action}</div>;
|
||
}
|
||
|
||
export function EmptyState({ title, description }: { title: string; description: string }) {
|
||
return <div className="empty-state"><strong>{title}</strong><p>{description}</p></div>;
|
||
}
|
||
|
||
export function ErrorBanner({ message }: { message: string }) { return <div className="error-banner" role="alert">{message}</div>; }
|
||
|
||
|