Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat: add inline creation for supplier

Brad Barbin79cbb9b9d95d
diff --git a/apps/carbon/app/components/Form/Supplier.tsx b/apps/carbon/app/components/Form/Supplier.tsx
index 1c5c05fa86..95d4e3ed5c 100644
--- a/apps/carbon/app/components/Form/Supplier.tsx
+++ b/apps/carbon/app/components/Form/Supplier.tsx
@@ -1,12 +1,17 @@
-import { useMemo } from "react";
+import { useDisclosure } from "@carbon/react";
+import { useMemo, useRef, useState } from "react";
+import { SupplierForm } from "~/modules/purchasing";
import { useSuppliers } from "~/stores";
import type { ComboboxProps } from "./Combobox";
-import Combobox from "./Combobox";
+import CreatableCombobox from "./CreatableCombobox";
type SupplierSelectProps = Omit<ComboboxProps, "options">;
const Supplier = (props: SupplierSelectProps) => {
const [suppliers] = useSuppliers();
+ const newSuppliersModal = useDisclosure();
+ const [createdSupplier, setCreatedSupplier] = useState<string>("");
+ const triggerRef = useRef<HTMLButtonElement>(null);
const options = useMemo(
() =>
@@ -18,7 +23,31 @@ const Supplier = (props: SupplierSelectProps) => {
);
return (
- <Combobox options={options} {...props} label={props?.label ?? "Supplier"} />
+ <>
+ <CreatableCombobox
+ ref={triggerRef}
+ options={options}
+ {...props}
+ label={props?.label ?? "Supplier"}
+ onCreateOption={(option) => {
+ newSuppliersModal.onOpen();
+ setCreatedSupplier(option);
+ }}
+ />
+ {newSuppliersModal.isOpen && (
+ <SupplierForm
+ type="modal"
+ onClose={() => {
+ setCreatedSupplier("");
+ newSuppliersModal.onClose();
+ triggerRef.current?.click();
+ }}
+ initialValues={{
+ name: createdSupplier,
+ }}
+ />
+ )}
+ </>
);
};
diff --git a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierForm.tsx b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierForm.tsx
index 84f2535e41..e74818566e 100644
--- a/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierForm.tsx
+++ b/apps/carbon/app/modules/purchasing/ui/Supplier/SupplierForm.tsx
@@ -1,14 +1,17 @@
import {
- Card,
- CardContent,
- CardDescription,
- CardFooter,
- CardHeader,
- CardTitle,
HStack,
+ ModalCard,
+ ModalCardBody,
+ ModalCardContent,
+ ModalCardDescription,
+ ModalCardFooter,
+ ModalCardHeader,
+ ModalCardProvider,
+ ModalCardTitle,
VStack,
cn,
} from "@carbon/react";
+import { useFetcher } from "@remix-run/react";
import { ValidatedForm } from "remix-validated-form";
import { Employee, Hidden, Input, Select, Submit } from "~/components/Form";
import { usePermissions, useRouteData } from "~/hooks";
@@ -19,10 +22,17 @@ import { path } from "~/utils/path";
type SupplierFormProps = {
initialValues: TypeOfValidator<typeof supplierValidator>;
+ type?: "card" | "modal";
+ onClose?: () => void;
};
-const SupplierForm = ({ initialValues }: SupplierFormProps) => {
+const SupplierForm = ({
+ initialValues,
+ type = "card",
+ onClose,
+}: SupplierFormProps) => {
const permissions = usePermissions();
+ const fetcher = useFetcher();
const routeData = useRouteData<{
supplierTypes: SupplierType[];
@@ -47,65 +57,81 @@ const SupplierForm = ({ initialValues }: SupplierFormProps) => {
: !permissions.can("create", "purchasing");
return (
- <ValidatedForm
- method="post"
- validator={supplierValidator}
- defaultValues={initialValues}
- >
- <Card>
- <CardHeader>
- <CardTitle>
- {isEditing ? "Supplier Overview" : "New Supplier"}
- </CardTitle>
- {!isEditing && (
- <CardDescription>
- A supplier is a business or person who sells you parts or
- services.
- </CardDescription>
- )}
- </CardHeader>
- <CardContent>
- <Hidden name="id" />
- <div
- className={cn(
- "grid w-full gap-x-8 gap-y-2",
- isEditing ? "grid-cols-1 lg:grid-cols-3" : "grid-cols-1"
- )}
+ <ModalCardProvider type={type}>
+ <ModalCard onClose={onClose}>
+ <ModalCardContent>
+ <ValidatedForm
+ method="post"
+ action={
+ isEditing
+ ? undefined
+ : type === "card"
+ ? path.to.newSupplier
+ : path.to.api.newSupplier
+ }
+ validator={supplierValidator}
+ defaultValues={initialValues}
+ onSubmit={onClose}
+ fetcher={fetcher}
>
- <VStack>
- <Input autoFocus={!isEditing} name="name" label="Name" />
- <Input name="taxId" label="Tax ID" />
- </VStack>
- <VStack>
- <Select
- name="supplierTypeId"
- label="Supplier Type"
- options={supplierTypeOptions}
- placeholder="Select Supplier Type"
- />
- <Select
- name="supplierStatusId"
- label="Supplier Status"
- options={supplierStatusOptions}
- placeholder="Select Supplier Status"
- />
- </VStack>
- {isEditing && (
- <>
+ <ModalCardHeader>
+ <ModalCardTitle>
+ {isEditing ? "Supplier Overview" : "New Supplier"}
+ </ModalCardTitle>
+ {!isEditing && (
+ <ModalCardDescription>
+ A supplier is a business or person who sells you parts or
+ services.
+ </ModalCardDescription>
+ )}
+ </ModalCardHeader>
+ <ModalCardBody>
+ <Hidden name="id" />
+ <div
+ className={cn(
+ "grid w-full gap-x-8 gap-y-2",
+ isEditing ? "grid-cols-1 lg:grid-cols-3" : "grid-cols-1"
+ )}
+ >
<VStack>
- <Employee name="accountManagerId" label="Account Manager" />
+ <Input autoFocus={!isEditing} name="name" label="Name" />
+ <Input name="taxId" label="Tax ID" />
</VStack>
- </>
- )}
- </div>
- </CardContent>
- <CardFooter>
- <HStack>
- <Submit isDisabled={isDisabled}>Save</Submit>
- </HStack>
- </CardFooter>
- </Card>
- </ValidatedForm>
+ <VStack>
+ <Select
+ name="supplierTypeId"
+ label="Supplier Type"
+ options={supplierTypeOptions}
+ placeholder="Select Supplier Type"
+ />
+ <Select
+ name="supplierStatusId"
+ label="Supplier Status"
+ options={supplierStatusOptions}
+ placeholder="Select Supplier Status"
+ />
+ </VStack>
+ {isEditing && (
+ <>
+ <VStack>
+ <Employee
+ name="accountManagerId"
+ label="Account Manager"
+ />
+ </VStack>
+ </>
+ )}
+ </div>
+ </ModalCardBody>
+ <ModalCardFooter>
+ <HStack>
+ <Submit isDisabled={isDisabled}>Save</Submit>
+ </HStack>
+ </ModalCardFooter>
+ </ValidatedForm>
+ </ModalCardContent>
+ </ModalCard>
+ </ModalCardProvider>
);
};