Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

contact edit sheet

Jean-Baptiste Kaisera89622d976d8
diff --git a/registry.json b/registry.json
index e80b3db8..251956fb 100644
--- a/registry.json
+++ b/registry.json
@@ -396,6 +396,10 @@
"path": "src/components/atomic-crm/misc/ImageEditorField.tsx",
"type": "registry:component"
},
+ {
+ "path": "src/components/atomic-crm/misc/EditSheet.tsx",
+ "type": "registry:component"
+ },
{
"path": "src/components/atomic-crm/misc/EditButton.tsx",
"type": "registry:component"
@@ -636,6 +640,10 @@
"path": "src/components/atomic-crm/contacts/ContactEmpty.tsx",
"type": "registry:component"
},
+ {
+ "path": "src/components/atomic-crm/contacts/ContactEditSheet.tsx",
+ "type": "registry:component"
+ },
{
"path": "src/components/atomic-crm/contacts/ContactEdit.tsx",
"type": "registry:component"
diff --git a/src/components/atomic-crm/contacts/ContactEdit.tsx b/src/components/atomic-crm/contacts/ContactEdit.tsx
index 7036447f..e0d14978 100644
--- a/src/components/atomic-crm/contacts/ContactEdit.tsx
+++ b/src/components/atomic-crm/contacts/ContactEdit.tsx
@@ -1,27 +1,15 @@
import { Card, CardContent } from "@/components/ui/card";
-import {
- EditBase,
- Form,
- RecordRepresentation,
- useCreatePath,
- useEditContext,
-} from "ra-core";
+import { EditBase, Form, useEditContext } from "ra-core";
-import { useIsMobile } from "@/hooks/use-mobile";
import { FormToolbar } from "../layout/FormToolbar";
-import { MobileContent } from "../layout/MobileContent";
-import MobileHeader from "../layout/MobileHeader";
-import { MobileBackButton } from "../misc/MobileBackButton";
import type { Contact } from "../types";
import { ContactAside } from "./ContactAside";
import { ContactInputs } from "./ContactInputs";
-import { DeleteButton, SaveButton } from "@/components/admin";
export const ContactEdit = () => {
- const isMobile = useIsMobile();
return (
<EditBase redirect="show">
- {isMobile ? <ContactEditContentMobile /> : <ContactEditContent />}
+ <ContactEditContent />
</EditBase>
);
};
@@ -44,33 +32,3 @@ const ContactEditContent = () => {
</div>
);
};
-
-const ContactEditContentMobile = () => {
- const { isPending, record } = useEditContext<Contact>();
- const createPath = useCreatePath();
- const to = createPath({
- resource: "contacts",
- type: "show",
- id: record?.id,
- });
- if (isPending || !record) return null;
- return (
- <div>
- <MobileHeader>
- <MobileBackButton to={to} />
- <div className="flex flex-1 text-xl font-semibold text-ellipsis overflow-hidden whitespace-nowrap">
- Edit <RecordRepresentation />
- </div>
- </MobileHeader>
- <MobileContent>
- <Form className="flex flex-1 flex-col gap-4">
- <ContactInputs />
- <div className="flex flex-col gap-2 mt-4">
- <SaveButton />
- <DeleteButton />
- </div>
- </Form>
- </MobileContent>
- </div>
- );
-};
diff --git a/src/components/atomic-crm/contacts/ContactEditSheet.tsx b/src/components/atomic-crm/contacts/ContactEditSheet.tsx
new file mode 100644
index 00000000..33778742
--- /dev/null
+++ b/src/components/atomic-crm/contacts/ContactEditSheet.tsx
@@ -0,0 +1,32 @@
+import { RecordRepresentation, type Identifier } from "ra-core";
+import { EditSheet } from "../misc/EditSheet";
+import { ContactInputs } from "./ContactInputs";
+
+export interface ContactEditSheetProps {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ contactId: Identifier;
+}
+
+export const ContactEditSheet = ({
+ open,
+ onOpenChange,
+ contactId,
+}: ContactEditSheetProps) => {
+ return (
+ <EditSheet
+ resource="contacts"
+ id={contactId}
+ title={
+ <>
+ Edit <RecordRepresentation />
+ </>
+ }
+ open={open}
+ onOpenChange={onOpenChange}
+ mutationMode="undoable"
+ >
+ <ContactInputs />
+ </EditSheet>
+ );
+};
diff --git a/src/components/atomic-crm/contacts/ContactShow.tsx b/src/components/atomic-crm/contacts/ContactShow.tsx
index 2317db5a..b56ed18f 100644
--- a/src/components/atomic-crm/contacts/ContactShow.tsx
+++ b/src/components/atomic-crm/contacts/ContactShow.tsx
@@ -14,6 +14,7 @@ import { Button } from "@/components/ui/button";
import { CompanyAvatar } from "../companies/CompanyAvatar";
import { NoteCreate, NotesIterator } from "../notes";
import { NoteCreateSheet } from "../notes/NoteCreateSheet";
+import { ContactEditSheet } from "./ContactEditSheet";
import { TagsListEdit } from "./TagsListEdit";
import { ContactPersonalInfo } from "./ContactPersonalInfo";
import { ContactBackgroundInfo } from "./ContactBackgroundInfo";
@@ -22,7 +23,7 @@ import type { Contact } from "../types";
import { Avatar } from "./Avatar";
import { ContactAside } from "./ContactAside";
import { MobileBackButton } from "../misc/MobileBackButton";
-import { EditButton } from "../misc/EditButton";
+import { Pencil } from "lucide-react";
export const ContactShow = () => {
const isMobile = useIsMobile();
@@ -37,6 +38,7 @@ export const ContactShow = () => {
const ContactShowContentMobile = () => {
const { record, isPending } = useShowContext<Contact>();
const [noteCreateOpen, setNoteCreateOpen] = useState(false);
+ const [editOpen, setEditOpen] = useState(false);
if (isPending || !record) return null;
return (
@@ -46,12 +48,26 @@ const ContactShowContentMobile = () => {
onOpenChange={setNoteCreateOpen}
contact_id={record.id}
/>
+ <ContactEditSheet
+ open={editOpen}
+ onOpenChange={setEditOpen}
+ contactId={record.id}
+ />
<MobileHeader>
<MobileBackButton />
<div className="flex flex-1">
<h1 className="text-xl font-semibold">Contacts</h1>
</div>
- <EditButton />
+ <Button
+ type="button"
+ variant="ghost"
+ size="icon"
+ className="rounded-full"
+ onClick={() => setEditOpen(true)}
+ >
+ <Pencil className="size-5" />
+ <span className="sr-only">Edit record</span>
+ </Button>
</MobileHeader>
<MobileContent>
<div className="mb-6">
diff --git a/src/components/atomic-crm/misc/EditSheet.tsx b/src/components/atomic-crm/misc/EditSheet.tsx
new file mode 100644
index 00000000..5275301a
--- /dev/null
+++ b/src/components/atomic-crm/misc/EditSheet.tsx
@@ -0,0 +1,151 @@
+import { DeleteButton } from "@/components/admin";
+import { SaveButton } from "@/components/admin/form";
+import {
+ Sheet,
+ SheetContent,
+ SheetFooter,
+ SheetHeader,
+ SheetTitle,
+} from "@/components/ui/sheet";
+import {
+ EditBase,
+ Form,
+ useNotify,
+ useRedirect,
+ useResourceContext,
+ useTranslate,
+ type EditBaseProps,
+ type FormProps,
+} from "ra-core";
+import { type ReactNode } from "react";
+
+export interface EditSheetProps extends EditBaseProps {
+ /**
+ * The children elements that will be rendered inside the sheet as form inputs
+ */
+ children: ReactNode;
+
+ /**
+ * Controls whether the sheet is open
+ */
+ open: boolean;
+
+ /**
+ * Callback fired when the sheet open state changes
+ */
+ onOpenChange: (open: boolean) => void;
+
+ /**
+ * The title displayed in the sheet header
+ */
+ title?: ReactNode;
+
+ /**
+ * Default values for the form
+ */
+ defaultValues?: FormProps["defaultValues"];
+}
+
+/**
+ * A Sheet component that contains an edit form with externally controlled open state.
+ *
+ * Renders a Sheet containing an EditBase form. The sheet has a fixed footer with Save and Delete buttons.
+ * The open state is controlled externally via the open and onOpenChange props. The sheet will automatically
+ * close itself on successful submission (if redirect is false) or when the Delete/Close actions are triggered.
+ *
+ * @example
+ * ```tsx
+ * const [open, setOpen] = useState(false);
+ *
+ * return (
+ * <>
+ * <Button onClick={() => setOpen(true)}>Edit Contact</Button>
+ * <EditSheet
+ * resource="contacts"
+ * id={contactId}
+ * title="Edit Contact"
+ * open={open}
+ * onOpenChange={setOpen}
+ * >
+ * <TextInput source="first_name" />
+ * <TextInput source="last_name" />
+ * <TextInput source="email" />
+ * </EditSheet>
+ * </>
+ * );
+ * ```
+ */
+export const EditSheet = ({
+ children,
+ open,
+ onOpenChange,
+ title = "Edit",
+ redirect: redirectTo = "show",
+ mutationOptions,
+ defaultValues,
+ ...editBaseProps
+}: EditSheetProps) => {
+ const resource = useResourceContext(editBaseProps);
+ const translate = useTranslate();
+ const notify = useNotify();
+ const redirect = useRedirect();
+
+ // Handle success - close sheet in addition to default behavior
+ const handleSuccess = (...args: any[]) => {
+ if (mutationOptions?.onSuccess) {
+ return mutationOptions.onSuccess(
+ ...(args as Parameters<typeof mutationOptions.onSuccess>),
+ );
+ }
+ const [data] = args;
+ notify(`resources.${resource}.notifications.updated`, {
+ type: "info",
+ messageArgs: {
+ smart_count: 1,
+ _: translate(`ra.notification.updated`, {
+ smart_count: 1,
+ }),
+ },
+ undoable: editBaseProps.mutationMode === "undoable",
+ });
+ redirect(redirectTo, resource, data.id, data);
+ onOpenChange(false);
+ };
+
+ const enhancedMutationOptions = {
+ ...mutationOptions,
+ onSuccess: handleSuccess,
+ };
+
+ return (
+ <Sheet open={open} onOpenChange={onOpenChange}>
+ <SheetContent side="bottom" className="h-screen flex flex-col">
+ <EditBase
+ {...editBaseProps}
+ redirect={redirectTo}
+ mutationOptions={enhancedMutationOptions}
+ >
+ <Form
+ defaultValues={defaultValues}
+ className="h-screen flex-1 flex flex-col"
+ >
+ <SheetHeader className="border-b">
+ <SheetTitle>{title}</SheetTitle>
+ </SheetHeader>
+
+ <div className="flex-1 overflow-y-auto flex flex-col gap-3 p-4">
+ {children}
+ </div>
+
+ <SheetFooter className="border-t">
+ <div className="flex w-full gap-4">
+ <DeleteButton variant="destructive" className="flex-1" />
+ <SaveButton className="flex-1" />
+ </div>
+ </SheetFooter>
+ </Form>
+ </EditBase>
+ </SheetContent>
+ </Sheet>
+ );
+};