From 14329b5101a261bc92c11b5e546197311a95eb57 Mon Sep 17 00:00:00 2001 From: Daniel Belcher Date: Thu, 18 Jan 2024 07:09:48 -0800 Subject: [PATCH 1/8] feat: disabled and readonly set for rhf comps --- src/packages/shared-types/forms.ts | 3 ++ .../ui/src/components/RHF/Document.tsx | 32 +++++++++++-------- .../ui/src/components/RHF/FieldArray.tsx | 14 +++++--- .../ui/src/components/RHF/FieldGroup.tsx | 18 +++++++---- .../ui/src/components/RHF/FormGroup.tsx | 6 ++++ src/services/ui/src/components/RHF/Slot.tsx | 25 ++++++++++++--- .../ui/src/components/RHF/utils/index.ts | 1 + .../components/RHF/utils/readonlyContext.tsx | 22 +++++++++++++ 8 files changed, 90 insertions(+), 31 deletions(-) create mode 100644 src/services/ui/src/components/RHF/utils/readonlyContext.tsx diff --git a/src/packages/shared-types/forms.ts b/src/packages/shared-types/forms.ts index 8d28ddf1ea..0aad6b9af4 100644 --- a/src/packages/shared-types/forms.ts +++ b/src/packages/shared-types/forms.ts @@ -20,6 +20,7 @@ export interface FormSchema { export type RHFSlotProps = { name: string; + readonly?: boolean; label?: string; labelStyling?: string; formItemStyling?: string; @@ -97,6 +98,7 @@ export type FieldArrayProps< > = { control: Control; name: TFieldArrayName; + readonly?: boolean; fields: RHFSlotProps[]; groupNamePrefix?: string; appendText?: string; @@ -107,6 +109,7 @@ export type FieldGroupProps< TFieldArrayName extends FieldArrayPath = FieldArrayPath > = { control: Control; + readonly?: boolean; name: TFieldArrayName; fields: RHFSlotProps[]; appendText?: string; diff --git a/src/services/ui/src/components/RHF/Document.tsx b/src/services/ui/src/components/RHF/Document.tsx index 237b54e0a0..4e753381ee 100644 --- a/src/services/ui/src/components/RHF/Document.tsx +++ b/src/services/ui/src/components/RHF/Document.tsx @@ -3,28 +3,32 @@ import { Control, FieldValues } from "react-hook-form"; import { FormLabel } from "../Inputs"; import { RHFSection } from "./Section"; import { FormSchema } from "shared-types"; +import { ReadOnlyContextProvider } from "./utils"; export const RHFDocument = (props: { document: FormSchema; control: Control; + readonly?: boolean; }) => { return (
-
-
-
- - {props.document.header} - + +
+
+
+ + {props.document.header} + +
+ {props.document.sections.map((SEC, index) => ( + + ))}
- {props.document.sections.map((SEC, index) => ( - - ))} -
+
); }; diff --git a/src/services/ui/src/components/RHF/FieldArray.tsx b/src/services/ui/src/components/RHF/FieldArray.tsx index 61a594d7d8..0994cf0d33 100644 --- a/src/services/ui/src/components/RHF/FieldArray.tsx +++ b/src/services/ui/src/components/RHF/FieldArray.tsx @@ -38,18 +38,20 @@ export const RHFFieldArray = ( ); })} - {index >= 1 && ( + {!props.readonly && index >= 1 && ( fieldArr.remove(index)} @@ -59,10 +61,12 @@ export const RHFFieldArray = ( ); })}
- + {!props.readonly && ( + + )}
); diff --git a/src/services/ui/src/components/RHF/FieldGroup.tsx b/src/services/ui/src/components/RHF/FieldGroup.tsx index fcebaea3dd..95bfcd40dd 100644 --- a/src/services/ui/src/components/RHF/FieldGroup.tsx +++ b/src/services/ui/src/components/RHF/FieldGroup.tsx @@ -39,17 +39,19 @@ export const FieldGroup = ( key={adjustedSlotName} control={props.control} name={adjustedSlotName as never} + disabled={!!props.readonly} {...(SLOT.rules && { rules: SLOT.rules })} render={RHFSlot({ ...SLOT, control: props.control, + readonly: props.readonly, name: adjustedSlotName, groupNamePrefix: adjustedPrefix, })} /> ); })} - {index >= 1 && ( + {!props.readonly && index >= 1 && ( - + {!props.readonly && ( +
+ +
+ )} ); }; diff --git a/src/services/ui/src/components/RHF/FormGroup.tsx b/src/services/ui/src/components/RHF/FormGroup.tsx index 10efe5ea9b..db08fbb2ff 100644 --- a/src/services/ui/src/components/RHF/FormGroup.tsx +++ b/src/services/ui/src/components/RHF/FormGroup.tsx @@ -3,12 +3,16 @@ import { FormLabel, FormField } from "../Inputs"; import { DependencyWrapper } from "./dependencyWrapper"; import { RHFSlot } from "./Slot"; import * as TRhf from "shared-types"; +import { useReadOnlyContext } from "./utils"; export const RHFFormGroup = (props: { form: TRhf.FormGroup; control: Control; groupNamePrefix?: string; + childReadonly?: boolean; }) => { + const { readonly } = useReadOnlyContext(); + return (
@@ -24,10 +28,12 @@ export const RHFFormGroup = (props: { = FieldPath >({ control, + readonly, rhf, label, description, @@ -77,21 +78,21 @@ export const RHFSlot = < {rhf === "Input" && (() => { const hops = props as RHFComponentMap["Input"]; - return ; + return ; })()} {/* ----------------------------------------------------------------------------- */} {rhf === "Textarea" && (() => { const hops = props as RHFComponentMap["Textarea"]; - return