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: configurator form ui

Brad Barbin1c0086204f08
diff --git a/apps/carbon/app/components/Configurator/Configurator.tsx b/apps/carbon/app/components/Configurator/ConfigurationEditor/ConfigurationEditor.tsx
similarity index 99%
rename from apps/carbon/app/components/Configurator/Configurator.tsx
rename to apps/carbon/app/components/Configurator/ConfigurationEditor/ConfigurationEditor.tsx
index abdb8b2f96..b62c4454ba 100644
--- a/apps/carbon/app/components/Configurator/Configurator.tsx
+++ b/apps/carbon/app/components/Configurator/ConfigurationEditor/ConfigurationEditor.tsx
@@ -19,17 +19,17 @@ import { LuFunctionSquare, LuPlay, LuSave, LuTrash2 } from "react-icons/lu";
import { useMode } from "~/hooks/useMode";
import type { action } from "~/routes/x+/part+/$itemId.rule";
import { path } from "~/utils/path";
-import { ConfirmDelete } from "../Modals";
-import ParameterPanel from "./ParameterPanel";
-import type { Configuration, Parameter, ParameterInput } from "./types";
-import { typeMap } from "./types";
+import { ConfirmDelete } from "../../Modals";
+import type { Configuration, Parameter, ParameterInput } from "../types";
+import { typeMap } from "../types";
import {
configureMonaco,
convertTypescriptToJavaScript,
generateDefaultCode,
generateTypeDefinitions,
getDefaultValue,
-} from "./utils";
+} from "../utils";
+import ParameterPanel from "./ParameterPanel";
interface ConfiguratorProps {
configuration: Configuration;
diff --git a/apps/carbon/app/components/Configurator/ParameterPanel.tsx b/apps/carbon/app/components/Configurator/ConfigurationEditor/ParameterPanel.tsx
similarity index 96%
rename from apps/carbon/app/components/Configurator/ParameterPanel.tsx
rename to apps/carbon/app/components/Configurator/ConfigurationEditor/ParameterPanel.tsx
index d7d003df0e..9cb173d8da 100644
--- a/apps/carbon/app/components/Configurator/ParameterPanel.tsx
+++ b/apps/carbon/app/components/Configurator/ConfigurationEditor/ParameterPanel.tsx
@@ -1,5 +1,5 @@
-import { ConfiguratorDataTypeIcon } from "./Icons";
-import type { Parameter } from "./types";
+import { ConfiguratorDataTypeIcon } from "../Icons";
+import type { Parameter } from "../types";
interface ParameterPanelProps {
parameters: Parameter[];
diff --git a/apps/carbon/app/components/Configurator/ConfigurationEditor/index.ts b/apps/carbon/app/components/Configurator/ConfigurationEditor/index.ts
new file mode 100644
index 0000000000..32704d5fa4
--- /dev/null
+++ b/apps/carbon/app/components/Configurator/ConfigurationEditor/index.ts
@@ -0,0 +1,3 @@
+import ConfigurationEditor from "./ConfigurationEditor";
+
+export { ConfigurationEditor };
diff --git a/apps/carbon/app/components/Configurator/ConfiguratorForm/ConfiguratorForm.tsx b/apps/carbon/app/components/Configurator/ConfiguratorForm/ConfiguratorForm.tsx
new file mode 100644
index 0000000000..1948c09e59
--- /dev/null
+++ b/apps/carbon/app/components/Configurator/ConfiguratorForm/ConfiguratorForm.tsx
@@ -0,0 +1,406 @@
+import {
+ Button,
+ Input,
+ Label,
+ Modal,
+ ModalBody,
+ ModalContent,
+ ModalHeader,
+ ModalTitle,
+ NumberDecrementStepper,
+ NumberField,
+ NumberIncrementStepper,
+ NumberInput,
+ NumberInputGroup,
+ NumberInputStepper,
+ Progress,
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+ Switch,
+} from "@carbon/react";
+import { createContext, useContext, useEffect, useMemo, useState } from "react";
+import { LuChevronDown, LuChevronUp } from "react-icons/lu";
+import { z } from "zod";
+import { zfd } from "zod-form-data";
+import type {
+ ConfigurationParameter,
+ ConfigurationParameterGroup,
+} from "~/modules/items/types";
+
+interface FormData {
+ [key: string]: string | number | boolean;
+}
+
+interface ParameterFieldProps {
+ parameter: ConfigurationParameter;
+}
+
+function getParameterSchema(parameter: ConfigurationParameter) {
+ switch (parameter.dataType) {
+ case "numeric":
+ return zfd.numeric(
+ z.number({
+ required_error: `${parameter.label} is required`,
+ })
+ );
+ case "text":
+ return z.string({
+ required_error: `${parameter.label} is required`,
+ });
+ case "list":
+ return z.enum(parameter.listOptions as [string, ...string[]], {
+ required_error: `${parameter.label} is required`,
+ });
+ case "boolean":
+ return zfd.checkbox();
+ default:
+ return z.any();
+ }
+}
+
+function generateConfigurationSchema(parameters: ConfigurationParameter[]) {
+ const schemaFields = parameters.reduce((acc, parameter) => {
+ acc[parameter.key] = getParameterSchema(parameter);
+ return acc;
+ }, {} as Record<string, z.ZodType>);
+
+ return z.object(schemaFields);
+}
+
+function ParameterField({ parameter }: ParameterFieldProps) {
+ const { formData, setFormData } = useConfigurator();
+
+ const handleChange = (value: string | number | boolean) => {
+ setFormData({ ...formData, [parameter.key]: value });
+ };
+
+ switch (parameter.dataType) {
+ case "numeric":
+ return (
+ <div className="space-y-2">
+ <Label htmlFor={parameter.key}>{parameter.label}</Label>
+ <NumberField
+ onChange={(value) => handleChange(Number(value))}
+ value={formData[parameter.key] as number}
+ >
+ <NumberInputGroup className="relative">
+ <NumberInput id={parameter.key} />
+ <NumberInputStepper>
+ <NumberIncrementStepper>
+ <LuChevronUp size="1em" strokeWidth="3" />
+ </NumberIncrementStepper>
+ <NumberDecrementStepper>
+ <LuChevronDown size="1em" strokeWidth="3" />
+ </NumberDecrementStepper>
+ </NumberInputStepper>
+ </NumberInputGroup>
+ </NumberField>
+ </div>
+ );
+
+ case "text":
+ return (
+ <div className="space-y-2">
+ <Label htmlFor={parameter.key}>{parameter.label}</Label>
+ <Input
+ id={parameter.key}
+ type="text"
+ value={(formData[parameter.key] as string) || ""}
+ onChange={(e) => handleChange(e.target.value)}
+ className="w-full"
+ />
+ </div>
+ );
+
+ case "list":
+ return (
+ <div className="space-y-2">
+ <Label htmlFor={parameter.key}>{parameter.label}</Label>
+ <Select
+ value={formData[parameter.key] as string}
+ onValueChange={handleChange}
+ >
+ <SelectTrigger id={parameter.key}>
+ <SelectValue placeholder="Select an option" />
+ </SelectTrigger>
+ <SelectContent>
+ {parameter.listOptions?.map((option) => (
+ <SelectItem key={option} value={option}>
+ {option}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ );
+
+ case "boolean":
+ return (
+ <div className="flex flex-col items-start gap-2">
+ <Label htmlFor={parameter.key}>{parameter.label}</Label>
+ <Switch
+ id={parameter.key}
+ checked={(formData[parameter.key] as boolean) || false}
+ onCheckedChange={handleChange}
+ />
+ </div>
+ );
+
+ default:
+ return null;
+ }
+}
+
+type ConfiguratorContextType = {
+ currentStep: number;
+ totalSteps: number;
+ formData: FormData;
+ setFormData: (data: FormData) => void;
+ nextStep: () => void;
+ previousStep: () => void;
+ goToStep: (step: number) => void;
+};
+
+const ConfiguratorContext = createContext<ConfiguratorContextType | undefined>(
+ undefined
+);
+
+interface ConfiguratorProviderProps {
+ children: React.ReactNode;
+ totalSteps: number;
+ initialValues?: FormData;
+}
+
+function ConfiguratorProvider({
+ children,
+ totalSteps,
+ initialValues = {},
+}: ConfiguratorProviderProps) {
+ const [currentStep, setCurrentStep] = useState(0);
+ const [formData, setFormData] = useState<FormData>(initialValues);
+
+ const nextStep = () => {
+ if (currentStep < totalSteps - 1) {
+ setCurrentStep((prev) => prev + 1);
+ }
+ };
+
+ const previousStep = () => {
+ if (currentStep > 0) {
+ setCurrentStep((prev) => prev - 1);
+ }
+ };
+
+ const goToStep = (step: number) => {
+ if (step >= 0 && step < totalSteps) {
+ setCurrentStep(step);
+ }
+ };
+
+ return (
+ <ConfiguratorContext.Provider
+ value={{
+ currentStep,
+ totalSteps,
+ formData,
+ setFormData,
+ nextStep,
+ previousStep,
+ goToStep,
+ }}
+ >
+ {children}
+ </ConfiguratorContext.Provider>
+ );
+}
+
+function useConfigurator() {
+ const context = useContext(ConfiguratorContext);
+ if (!context) {
+ throw new Error(
+ "useConfigurator must be used within a ConfiguratorProvider"
+ );
+ }
+ return context;
+}
+
+interface ConfiguratorFormProps {
+ groups: ConfigurationParameterGroup[];
+ parameters: ConfigurationParameter[];
+ onSubmit: (data: Record<string, any>) => void;
+ onGroupChange: (group: ConfigurationParameterGroup) => void;
+ initialValues?: FormData;
+}
+
+function ConfiguratorFormContent({
+ groups,
+ parameters,
+ onSubmit,
+ onGroupChange,
+}: ConfiguratorFormProps) {
+ const { currentStep, totalSteps, formData, nextStep, previousStep } =
+ useConfigurator();
+
+ const sortedGroups = useMemo(
+ () => [...groups].sort((a, b) => a.sortOrder - b.sortOrder),
+ [groups]
+ );
+
+ const groupedParameters = useMemo(
+ () =>
+ sortedGroups.map((group) => ({
+ group,
+ parameters: parameters
+ .filter((p) => p.configurationParameterGroupId === group.id)
+ .sort((a, b) => a.sortOrder - b.sortOrder),
+ })),
+ [sortedGroups, parameters]
+ );
+
+ useEffect(() => {
+ if (groupedParameters[currentStep]) {
+ onGroupChange(groupedParameters[currentStep].group);
+ }
+ }, [currentStep, groupedParameters, onGroupChange]);
+
+ const isStepValid = useMemo(() => {
+ if (!groupedParameters[currentStep]) return false;
+
+ return groupedParameters[currentStep].parameters.every((parameter) => {
+ if (parameter.dataType === "boolean") return true;
+ return (
+ formData[parameter.key] !== undefined && formData[parameter.key] !== ""
+ );
+ });
+ }, [currentStep, groupedParameters, formData]);
+
+ const handleSubmit = (e: React.FormEvent) => {
+ e.preventDefault();
+ if (currentStep === totalSteps - 1) {
+ const schema = generateConfigurationSchema(parameters);
+ const result = schema.safeParse(formData);
+ if (result.success) {
+ onSubmit(result.data);
+ }
+ } else {
+ nextStep();
+ }
+ };
+
+ return (
+ <form onSubmit={handleSubmit} className="space-y-8">
+ <ConfiguratorProgress />
+
+ {groupedParameters[currentStep] && (
+ <ConfiguratorStep
+ group={groupedParameters[currentStep].group}
+ parameters={groupedParameters[currentStep].parameters}
+ />
+ )}
+
+ <div className="flex justify-between pt-4">
+ <Button
+ type="button"
+ variant="secondary"
+ onClick={previousStep}
+ disabled={currentStep === 0}
+ >
+ Previous
+ </Button>
+ <Button type="submit" disabled={!isStepValid}>
+ {currentStep === totalSteps - 1 ? "Submit" : "Next"}
+ </Button>
+ </div>
+ </form>
+ );
+}
+
+function ConfiguratorForm(props: ConfiguratorFormProps) {
+ return (
+ <ConfiguratorProvider
+ totalSteps={props.groups.length}
+ initialValues={props.initialValues}
+ >
+ <ConfiguratorFormContent {...props} />
+ </ConfiguratorProvider>
+ );
+}
+
+type ConfiguratorModalProps = {
+ groups: ConfigurationParameterGroup[];
+ parameters: ConfigurationParameter[];
+ open: boolean;
+ onClose: () => void;
+ onSubmit: (data: Record<string, any>) => void;
+ initialValues?: FormData;
+};
+
+function ConfiguratorModal({
+ groups,
+ parameters,
+ open,
+ onClose,
+ onSubmit,
+ initialValues,
+}: ConfiguratorModalProps) {
+ const [currentGroup, setCurrentGroup] = useState<ConfigurationParameterGroup>(
+ groups[0]
+ );
+
+ return (
+ <Modal
+ open={open}
+ onOpenChange={(open) => {
+ if (!open) {
+ onClose();
+ }
+ }}
+ >
+ <ModalContent size="large">
+ <ModalHeader>
+ <ModalTitle>{currentGroup.name}</ModalTitle>
+ </ModalHeader>
+ <ModalBody>
+ <ConfiguratorForm
+ groups={groups}
+ parameters={parameters}
+ onSubmit={onSubmit}
+ onGroupChange={setCurrentGroup}
+ initialValues={initialValues}
+ />
+ </ModalBody>
+ </ModalContent>
+ </Modal>
+ );
+}
+
+function ConfiguratorProgress() {
+ const { currentStep, totalSteps } = useConfigurator();
+ const progress = ((currentStep + 1) / totalSteps) * 100;
+
+ return (
+ <div className="w-full space-y-2">
+ <Progress value={progress} className="h-2" />
+ </div>
+ );
+}
+
+type ConfiguratorStepProps = {
+ group: ConfigurationParameterGroup;
+ parameters: ConfigurationParameter[];
+};
+
+function ConfiguratorStep({ group, parameters }: ConfiguratorStepProps) {
+ return (
+ <div className="flex flex-col gap-6">
+ {parameters.map((parameter) => (
+ <ParameterField key={parameter.id} parameter={parameter} />
+ ))}
+ </div>
+ );
+}
+
+export { ConfiguratorModal };
diff --git a/apps/carbon/app/components/Configurator/ConfiguratorForm/index.ts b/apps/carbon/app/components/Configurator/ConfiguratorForm/index.ts
new file mode 100644
index 0000000000..c8c7d79082
--- /dev/null
+++ b/apps/carbon/app/components/Configurator/ConfiguratorForm/index.ts
@@ -0,0 +1 @@
+export { ConfiguratorModal } from "./ConfiguratorForm";
diff --git a/apps/carbon/app/components/Configurator/index.ts b/apps/carbon/app/components/Configurator/index.ts
deleted file mode 100644
index 08c8236134..0000000000
--- a/apps/carbon/app/components/Configurator/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-import Configurator from "./Configurator";
-
-export { Configurator };
diff --git a/apps/carbon/app/modules/items/ui/Item/BillOfMaterial.tsx b/apps/carbon/app/modules/items/ui/Item/BillOfMaterial.tsx
index 4c0b47defb..fd5c1245a2 100644
--- a/apps/carbon/app/modules/items/ui/Item/BillOfMaterial.tsx
+++ b/apps/carbon/app/modules/items/ui/Item/BillOfMaterial.tsx
@@ -43,7 +43,7 @@ import type {
import { SortableList, SortableListItem } from "~/components/SortableList";
import { usePermissions, useUser } from "~/hooks";
-import { Configurator } from "~/components/Configurator";
+import { ConfigurationEditor } from "~/components/Configurator/ConfigurationEditor";
import { useUnitOfMeasure } from "~/components/Form/UnitOfMeasure";
import {
methodType,
@@ -465,7 +465,7 @@ const BillOfMaterial = ({
/>
</CardContent>
{configuratorDisclosure.isOpen && configuration && (
- <Configurator
+ <ConfigurationEditor
configuration={configuration}
open={configuratorDisclosure.isOpen}
parameters={parameters ?? []}
diff --git a/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx b/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
index fce9447b75..f163ae7230 100644
--- a/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
+++ b/apps/carbon/app/modules/items/ui/Item/BillOfProcess.tsx
@@ -63,7 +63,7 @@ import {
} from "~/components/Form";
import { flushSync } from "react-dom";
-import { Configurator } from "~/components/Configurator";
+import { ConfigurationEditor } from "~/components/Configurator/ConfigurationEditor";
import type { Configuration } from "~/components/Configurator/types";
import { getUnitHint } from "~/components/Form/UnitHint";
import { ConfirmDelete } from "~/components/Modals";
@@ -632,7 +632,7 @@ const BillOfProcess = ({
/>
</CardContent>
{configuratorDisclosure.isOpen && configuration && (
- <Configurator
+ <ConfigurationEditor
configuration={configuration}
open={configuratorDisclosure.isOpen}
parameters={parameters ?? []}
diff --git a/apps/carbon/app/routes/x+/part+/$itemId.manufacturing.method.tsx b/apps/carbon/app/routes/x+/part+/$itemId.manufacturing.method.tsx
index 9a8dea2f8e..bf89d5805b 100644
--- a/apps/carbon/app/routes/x+/part+/$itemId.manufacturing.method.tsx
+++ b/apps/carbon/app/routes/x+/part+/$itemId.manufacturing.method.tsx
@@ -6,6 +6,7 @@ import { VStack } from "@carbon/react";
import { useLoaderData, useParams } from "@remix-run/react";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "@vercel/remix";
import { json, redirect } from "@vercel/remix";
+import { ConfiguratorModal } from "~/components/Configurator/ConfiguratorForm";
import { useRouteData } from "~/hooks";
import type { MakeMethod, Material, MethodOperation } from "~/modules/items";
import {
@@ -163,6 +164,22 @@ export default function MakeMethodRoute() {
parameters={configurationParametersAndGroups.parameters}
configurationRules={configurationRules}
/>
+ <ConfiguratorModal
+ initialValues={{
+ material: "Oak",
+ checkbox: true,
+ height: 1,
+ width: 2,
+ a1: 1,
+ a2: 2,
+ a3: 3,
+ }}
+ open={true}
+ onClose={() => {}}
+ groups={configurationParametersAndGroups.groups}
+ parameters={configurationParametersAndGroups.parameters}
+ onSubmit={() => {}}
+ />
</VStack>
);
}