App patterns
FieldsSheet
An edit sheet for one to three text fields. Errors show only after Save, then follow the input. It closes when onSubmit succeeds, stays open when its Promise is rejected, and opens from the bottom below 768 px. The content can be a function that gets the draft values, so a note can follow the typing. A field inputMode sets the keyboard a phone shows, for example numeric for a port. returnFocusTo names the element that gets the focus when the sheet closes, for example a section heading after a link in the sheet moved the person there. Without it, the focus goes back to the element that had the focus when the sheet opened. The element must take the focus (a heading needs tabIndex={-1}).
import { FieldsSheet, type SheetField } from "@percentsa/ui";Examples
Code
export function FieldsSheetExample() {
const t = useT();
const [open, setOpen] = useState(false);
const [saved, setSaved] = useState({ port: "8080", path: "/health" });
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>
{t("Edit start and health", "تعديل التشغيل والفحص")}
</Button>
<FieldsSheet
title={t("Edit start and health", "تعديل التشغيل والفحص")}
open={open}
onOpenChange={setOpen}
fields={[
{
key: "port",
label: t("Port", "المنفذ"),
value: saved.port,
inputMode: "numeric",
check: (x) => (/^\d+$/.test(x) && +x >= 1 && +x <= 65535 ? undefined : t("Enter a port from 1 to 65535.", "أدخل منفذًا من 1 إلى 65535.")),
},
{
key: "path",
label: t("Health check path", "مسار فحص الصحة"),
value: saved.path,
mono: true,
suggestions: [{ value: "/health" }, { value: "/ready" }, { value: "/status" }],
},
]}
onSubmit={(v) => new Promise((r) => setTimeout(() => (setSaved({ port: v.port!, path: v.path! }), r(null)), 600))}
>
{(draft) => (
<p className="text-sm text-fg-3">
{t("The check opens", "يفتح الفحص")} <span dir="ltr" className="font-mono">:{draft.port}{draft.path}</span>
</p>
)}
</FieldsSheet>
</>
);
}