From b952850679d97ae8f361ed101cad64ffb1711165 Mon Sep 17 00:00:00 2001 From: Isaac Way Date: Mon, 30 Jan 2023 21:46:43 -0600 Subject: [PATCH] fixes issue with object arrays and checkboxes --- .../ProcedureForm/ProcedureFormContext.tsx | 27 ++++ .../components/form/ProcedureForm/index.tsx | 115 +++++++++--------- .../components/form/fields/ObjectField.tsx | 18 ++- .../form/fields/base/BaseCheckboxField.tsx | 9 +- 4 files changed, 108 insertions(+), 61 deletions(-) create mode 100644 packages/trpc-panel/src/react-app/components/form/ProcedureForm/ProcedureFormContext.tsx diff --git a/packages/trpc-panel/src/react-app/components/form/ProcedureForm/ProcedureFormContext.tsx b/packages/trpc-panel/src/react-app/components/form/ProcedureForm/ProcedureFormContext.tsx new file mode 100644 index 0000000..3131024 --- /dev/null +++ b/packages/trpc-panel/src/react-app/components/form/ProcedureForm/ProcedureFormContext.tsx @@ -0,0 +1,27 @@ +import React, { useContext } from "react"; +import { createContext, ReactNode } from "react"; + +type ContextType = { + path: string; +}; + +const Context = createContext(null); + +export function ProcedureFormContextProvider({ + children, + path, +}: { + children: ReactNode; + path: string; +}) { + return {children}; +} + +export function useProcedureFormContext() { + const ctx = useContext(Context); + if (!ctx) + throw new Error( + "Procedure form context must be called within ProcedureFormContextProvider (this is a bug with trpc-panel, open an issue)." + ); + return ctx; +} diff --git a/packages/trpc-panel/src/react-app/components/form/ProcedureForm/index.tsx b/packages/trpc-panel/src/react-app/components/form/ProcedureForm/index.tsx index 15b3f3c..d96fecf 100644 --- a/packages/trpc-panel/src/react-app/components/form/ProcedureForm/index.tsx +++ b/packages/trpc-panel/src/react-app/components/form/ProcedureForm/index.tsx @@ -18,6 +18,7 @@ import { fullFormats } from "ajv-formats/dist/formats"; import type { ParsedInputNode } from "@src/parse/parseNodeTypes"; import { DocumentationSection } from "@src/react-app/components/form/ProcedureForm/DescriptionSection"; import { Field } from "@src/react-app/components/form/Field"; +import { ProcedureFormContextProvider } from "@src/react-app/components/form/ProcedureForm/ProcedureFormContext"; const TRPCErrorSchema = z.object({ shape: z.object({ @@ -147,66 +148,68 @@ export function ProcedureForm({ const fieldName = procedure.node.path.join("."); return ( - - {name} - - } - fullPath={procedure.pathFromRootRouter} - sectionType={procedure.procedureType} - focusOnScrollRef={formRef} - > -
+ + {name} + + } + fullPath={procedure.pathFromRootRouter} + sectionType={procedure.procedureType} + focusOnScrollRef={formRef} > -
- - - } - > - {procedure.node.type === "object" ? ( - Object.keys(procedure.node.children).length > 0 && ( - +
+ + + } + > + {procedure.node.type === "object" ? ( + Object.keys(procedure.node.children).length > 0 && ( + - ) + label={fieldName} + control={control} + topLevel + /> + ) + ) : ( + + )} + + + +
+ +
+ {data && } + {!data && data !== null && ( + Successful request but no data was returned + )} + {error && + (isTrpcError(error) ? ( + ) : ( - - )} - - - + {JSON.stringify(error)} + ))}
- -
- {data && } - {!data && data !== null && ( - Successful request but no data was returned - )} - {error && - (isTrpcError(error) ? ( - - ) : ( - {JSON.stringify(error)} - ))} -
- + + ); } diff --git a/packages/trpc-panel/src/react-app/components/form/fields/ObjectField.tsx b/packages/trpc-panel/src/react-app/components/form/fields/ObjectField.tsx index 4e5e99f..f647486 100644 --- a/packages/trpc-panel/src/react-app/components/form/fields/ObjectField.tsx +++ b/packages/trpc-panel/src/react-app/components/form/fields/ObjectField.tsx @@ -22,7 +22,14 @@ export function ObjectField({ return (
{Object.entries(node.children).map(([name, e]) => ( - + ))}
); @@ -33,7 +40,14 @@ export function ObjectField({ iconElement={overrideIconElement ?? } > {Object.entries(node.children).map(([childFieldName, e]) => ( - + ))} ); diff --git a/packages/trpc-panel/src/react-app/components/form/fields/base/BaseCheckboxField.tsx b/packages/trpc-panel/src/react-app/components/form/fields/base/BaseCheckboxField.tsx index c087ba8..444e880 100644 --- a/packages/trpc-panel/src/react-app/components/form/fields/base/BaseCheckboxField.tsx +++ b/packages/trpc-panel/src/react-app/components/form/fields/base/BaseCheckboxField.tsx @@ -1,6 +1,7 @@ import React, { useRef } from "react"; import { FieldError } from "@src/react-app/components/form/fields/FieldError"; import { useEnableInputGlobalHotkeys } from "@src/react-app/components/contexts/HotKeysContext"; +import { useProcedureFormContext } from "@src/react-app/components/form/ProcedureForm/ProcedureFormContext"; export function BaseCheckboxField({ value, @@ -17,17 +18,19 @@ export function BaseCheckboxField({ }) { const inputRef = useRef(null); useEnableInputGlobalHotkeys(inputRef, []); + const { path: procedurePath } = useProcedureFormContext(); + const id = procedurePath + "." + fieldId; return (
-