Skip to content

Commit

Permalink
Merge pull request #290 from Hunter275/disabled-dynamicform
Browse files Browse the repository at this point in the history
Rework disabled in DynamicForms
  • Loading branch information
thebentern authored Sep 5, 2024
2 parents cd00567 + 6375187 commit 530d33d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/components/Form/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import {
} from "react-hook-form";

interface DisabledBy<T> {
fieldName: Path<T> | "always";
fieldName: Path<T>;
selector?: number;
invert?: boolean;
}

export interface BaseFormBuilderProps<T> {
name: Path<T>;
disabled?: boolean;
disabledBy?: DisabledBy<T>[];
label: string;
description?: string;
Expand Down Expand Up @@ -62,11 +63,14 @@ export function DynamicForm<T extends FieldValues>({
defaultValues: defaultValues,
});

const isDisabled = (disabledBy?: DisabledBy<T>[]): boolean => {
const isDisabled = (
disabledBy?: DisabledBy<T>[],
disabled?: boolean,
): boolean => {
if (disabled) return true;
if (!disabledBy) return false;

return disabledBy.some((field) => {
if (field.fieldName === "always") return true;
const value = getValues(field.fieldName);
if (value === "always") return true;
if (typeof value === "boolean") return field.invert ? value : !value;
Expand Down Expand Up @@ -111,7 +115,7 @@ export function DynamicForm<T extends FieldValues>({
<DynamicFormField
field={field}
control={control}
disabled={isDisabled(field.disabledBy)}
disabled={isDisabled(field.disabledBy, field.disabled)}
/>
</FieldWrapper>
))}
Expand Down
2 changes: 1 addition & 1 deletion src/components/PageComponents/Config/Security.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ export const Security = (): JSX.Element => {
type: "text",
name: "publicKey",
label: "Public Key",
disabled: true,
description:
"Sent out to other nodes on the mesh to allow them to compute a shared secret key",
disabledBy: [{ fieldName: "always" }],
},
],
},
Expand Down

0 comments on commit 530d33d

Please sign in to comment.