Ivan Galakhov 4 лет назад
Родитель
Сommit
b67bdf120d

+ 38 - 1
dashboard/src/components/form-refactor/PorterForm.tsx

@@ -6,11 +6,12 @@ import Helper from "../values-form/Helper";
 import StringInput from "./field-components/StringInput";
 import { PorterFormContext } from "./PorterFormContextProvider";
 import Checkbox from "./field-components/Checkbox";
+import { ShowIf, ShowIfAnd, ShowIfNot, ShowIfOr } from "../../shared/types";
 
 interface Props {}
 
 const PorterForm: React.FC<Props> = () => {
-  const { formData } = useContext(PorterFormContext);
+  const { formData, formState } = useContext(PorterFormContext);
 
   const [currentTab, setCurrentTab] = useState(
     formData.tabs.length > 0 ? formData.tabs[0].name : ""
@@ -30,7 +31,43 @@ const PorterForm: React.FC<Props> = () => {
     return <p>Not Implemented: {(field as any).type}</p>;
   };
 
+  const evalShowIf = (vals: ShowIf): boolean => {
+    if (!vals) {
+      return false;
+    }
+    if (typeof vals == "string") {
+      return !!formState.variables[vals];
+    }
+    if ((vals as ShowIfOr).or) {
+      vals = vals as ShowIfOr;
+      for (let i = 0; i < vals.or.length; i++) {
+        if (evalShowIf(vals.or[i])) {
+          return true;
+        }
+      }
+      return false;
+    }
+    if ((vals as ShowIfAnd).and) {
+      vals = vals as ShowIfAnd;
+      for (let i = 0; i < vals.and.length; i++) {
+        if (!evalShowIf(vals.and[i])) {
+          return false;
+        }
+      }
+      return true;
+    }
+    if ((vals as ShowIfNot).not) {
+      vals = vals as ShowIfNot;
+      return !evalShowIf(vals.not);
+    }
+
+    return false;
+  };
+
   const renderSection = (section: Section): JSX.Element => {
+    if (section.show_if && !evalShowIf(section.show_if)) {
+      return null;
+    }
     return (
       <>
         {section.contents.map((field, i) => {

+ 15 - 0
dashboard/src/components/form-refactor/types.ts

@@ -33,8 +33,23 @@ export interface CheckboxField {
 
 export type FormField = HeadingField|SubtitleField|StringInputField|CheckboxField;
 
+export interface ShowIfAnd {
+  and: ShowIf[];
+}
+
+export interface ShowIfOr {
+  or: ShowIf[];
+}
+
+export interface ShowIfNot {
+  not: ShowIf;
+}
+
+export type ShowIf = string | ShowIfAnd | ShowIfOr | ShowIfNot;
+
 export interface Section {
   name: string;
+  show_if?: ShowIf;
   contents: FormField[];
 }
 

+ 2 - 2
dashboard/src/components/values-form/FormDebugger.tsx

@@ -286,8 +286,8 @@ tabs:
       variable: field_b
     - type: checkbox
       required: true
-      label: Checkbox C
-      variable: checkbox_c
+      label: Checkbox A alternatiev
+      variable: checkbox_a
     - type: subtitle
       label: "Note: Hidden required fields aren't supported yet (global only)"
   - name: controlled-by-external