import { createContext, isValidElement, useContext, useState, type ButtonHTMLAttributes, type ChangeEvent, type InputHTMLAttributes, type ReactNode, type SelectHTMLAttributes } from "react";
import { PersianDateInput } from "./PersianDateInput";
export function Card({ children, className = "", dataTab }: { children: ReactNode; className?: string; dataTab?: string }) {
return ;
}
const formErrors = createContext>({});
export function FormErrorProvider({ errors, children }: { errors: Record; children: ReactNode }) {
return {children};
}
export function Field({ label, hint, error, name, children }: { label: string; hint?: string; error?: string; name?: string; children: ReactNode }) {
const errors = useContext(formErrors);
const childName = isValidElement<{name?: string}>(children) ? children.props.name : undefined;
const key = name ?? childName;
const inlineError = error ?? (key
? errors[key] ?? errors[key.charAt(0).toUpperCase() + key.slice(1)] ??
Object.entries(errors).find(([field]) => field.toLowerCase().endsWith(`.${key.toLowerCase()}`))?.[1]
: undefined);
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) {
return latinDigits(value == null ? "" : String(value)).replace(/[٬,\s]/g, "");
}
function groupedNumber(value: string) {
if (!value || value === "-") return value;
if (!/^-?\d*(?:\.\d*)?$/.test(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);
event.currentTarget.setCustomValidity(next === "" || /^-?\d+(?:\.\d+)?$/.test(next)
? "" : "عدد معتبر وارد کنید؛ از حرف و نشانههای دیگر استفاده نکنید.");
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 ;
const limits: Record = {
projectNumber: 100, title: 1000, innovatorFullName: 500, fullName: 1000,
writer: 500, writerOrTranslator: 500, publishingOrganization: 500,
publicationPlace: 500, registrationPlace: 500, place: 500, organizer: 500,
area: 500, specializedField: 500, email: 320,
website: 2048, socialPage: 2048, link: 2048, directLink: 2048,
};
const maxLength = props.maxLength ?? (props.name ? limits[props.name] : undefined);
return ;
}
export function Select(props: SelectHTMLAttributes) { return ; }
export function Button({ className = "", ...props }: ButtonHTMLAttributes) { return ; }
export function PageTitle({ title, description, action }: { title: string; description: string; action?: ReactNode }) {
return ;
}
export function EmptyState({ title, description }: { title: string; description: string }) {
return ;
}
export function ErrorBanner({ message }: { message: string }) { return {message}
; }