Skip to content
العربية
App patterns

FormSheet

An edit form in a sheet. Cancel and Save stay at the bottom while the form scrolls, and Enter in a text field saves. pending shows Save loading and keeps the sheet open. With dirty, every way to close it (Escape, a click outside, the close button, a drag, Cancel) asks “Discard your changes?” first, and the browser asks before the page is left. The sheet does not close itself after a submit: close it when the save succeeds. blocked says why the submit is refused now: the reason shows above the buttons (announced to screen readers), the submit stays visible but disabled, and pending wins over it. danger makes the submit a danger button, cancelLabel renames the close button, size="lg" makes the sheet 56rem wide (a document made from another document), and side="bottom" puts the two large buttons in two equal columns. side="auto" opens at the end from 768 px and at the bottom below. 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}). It works only without trigger.

import { FormSheet } from "@percentsa/ui";

Examples

Code
export function FormSheetExample() {
  const t = useT();
  const [open, setOpen] = useState(false);
  const [saved, setSaved] = useState({ name: "", note: "" });
  const [draft, setDraft] = useState(saved);
  const [pending, setPending] = useState(false);
  return (
    <FormSheet
      trigger={<Button variant="secondary">{t("Edit customer", "تعديل العميل")}</Button>}
      title={t("Edit customer", "تعديل العميل")}
      description={t("Close it after a change: it asks first.", "أغلقه بعد تعديل: سيسألك أولًا.")}
      open={open}
      onOpenChange={(o) => {
        if (o) setDraft(saved);
        setOpen(o);
      }}
      dirty={draft.name !== saved.name || draft.note !== saved.note}
      pending={pending}
      onSubmit={(e) => {
        e.preventDefault();
        setPending(true);
        // The app closes the sheet when the save succeeds.
        setTimeout(() => {
          setSaved(draft);
          setPending(false);
          setOpen(false);
          toast({ title: t("Saved", "تم الحفظ") });
        }, 800);
      }}
    >
      <Field label={t("Name", "الاسم")}>
        <Input value={draft.name} onChange={(e) => setDraft({ ...draft, name: e.target.value })} autoComplete="name" />
      </Field>
      <Field label={t("Note", "ملاحظة")} optional>
        <Textarea value={draft.note} onChange={(e) => setDraft({ ...draft, note: e.target.value })} />
      </Field>
    </FormSheet>
  );
}
Code
export function FormSheetBlockedExample() {
  const t = useT();
  const [paid, setPaid] = useState(false);
  const [done, setDone] = useState<string | null>(null);
  return (
    <div className="flex flex-wrap gap-3">
      <FormSheet
        side="bottom"
        trigger={<Button variant="secondary">{t("Complete the job", "إنهاء المهمة")}</Button>}
        title={t("Complete the job", "إنهاء المهمة")}
        description={t("The customer is told that the car is ready.", "يُبلَّغ العميل أن السيارة جاهزة.")}
        submitLabel={t("Complete", "إنهاء")}
        blocked={paid ? null : t("A machining charge is not paid. Pay it or delete it before you complete.", "رسوم التشغيل غير مدفوعة. ادفعها أو احذفها قبل الإنهاء.")}
        onSubmit={(e) => (e.preventDefault(), setDone(t("Job completed", "اكتملت المهمة")))}
      >
        <Checkbox checked={paid} onCheckedChange={(v) => setPaid(v === true)} label={t("The machining charge is paid", "رسوم التشغيل مدفوعة")} />
      </FormSheet>
      <FormSheet
        side="bottom"
        trigger={<Button variant="secondary">{t("Cancel the appointment", "إلغاء الموعد")}</Button>}
        title={t("Cancel the appointment", "إلغاء الموعد")}
        description={t("The office is told. You cannot undo this.", "يُبلَّغ المكتب. لا يمكنك التراجع.")}
        cancelLabel={t("Go back", "تراجع")}
        submitLabel={t("Cancel the appointment", "إلغاء الموعد")}
        danger
        onSubmit={(e) => (e.preventDefault(), setDone(t("Appointment cancelled", "أُلغي الموعد")))}
      >
        <Field label={t("Reason", "السبب")}>
          <Input />
        </Field>
      </FormSheet>
      {done && <Text tone="muted" className="self-center">{done}</Text>}
    </div>
  );
}

/** A document made from another document opens in a large sheet. */
Code
export function FormSheetLargeExample() {
  const t = useT();
  return (
    <FormSheet
      size="lg"
      trigger={<Button variant="secondary">{t("Convert to order", "تحويل إلى طلب")}</Button>}
      title={t("Convert to order", "تحويل إلى طلب")}
      description={t("The quote lines become the lines of the order.", "تصبح بنود العرض بنود الطلب.")}
      submitLabel={t("Create the order", "إنشاء الطلب")}
      onSubmit={noSubmit}
    >
      <Field label={t("Delivery date", "تاريخ التسليم")}>
        <Input type="date" dir="ltr" />
      </Field>
      <Field label={t("Note", "ملاحظة")} optional>
        <Textarea />
      </Field>
    </FormSheet>
  );
}

/** `side="auto"`: at the end of the screen from 768 px, at the bottom on a phone. */
Code
export function FormSheetAutoExample() {
  const t = useT();
  return (
    <FormSheet
      side="auto"
      trigger={<Button variant="secondary">{t("Opens at the bottom on a phone", "يفتح من الأسفل على الهاتف")}</Button>}
      title={t("Rename the site", "تغيير اسم الموقع")}
      submitLabel={t("Save", "حفظ")}
      onSubmit={noSubmit}
    >
      <Field label={t("Name", "الاسم")}>
        <Input />
      </Field>
    </FormSheet>
  );
}