diff --git a/src/components/Connections/ConnectionForm.tsx b/src/components/Connections/ConnectionForm.tsx index 3efb6bc2d9..a4767f6400 100644 --- a/src/components/Connections/ConnectionForm.tsx +++ b/src/components/Connections/ConnectionForm.tsx @@ -1,21 +1,17 @@ import clsx from "clsx"; import { Form, Formik } from "formik"; -import FormikTextInput from "../Forms/Formik/FormikTextInput"; -import FormikCheckbox from "../Forms/Formik/FormikCheckbox"; +import { mapValues, method } from "lodash"; +import React, { useEffect, useState } from "react"; +import { FaTrash } from "react-icons/fa"; +import { Button } from "../Button"; +import { Icon } from "../Icon"; import { Modal } from "../Modal"; -import { useEffect, useState } from "react"; +import RenderConnectionFormFields from "./RenderConnectionFormFields"; import { ConnectionType, ConnectionValueType, - Field, connectionTypes } from "./connectionTypes"; -import { FormikEnvVarSource } from "../Forms/Formik/FormikEnvVarSource"; -import { Icon } from "../Icon"; -import React from "react"; -import { FaTrash } from "react-icons/fa"; -import { Button } from "../Button"; -import { mapValues, method } from "lodash"; export type Connection = { altID?: string; @@ -119,55 +115,6 @@ export default function ConnectionForm({ } as Connection; }; - const getFieldView = (field: Field) => { - const type = field.type ?? "input"; - switch (type) { - case "input": - return ( - - ); - case "numberInput": - return ( - - ); - case "checkbox": - return ( - - ); - case "EnvVarSource": - return ( - - ); - default: - return null; - } - }; - const getFormView = (connectionType: ConnectionType) => { return ( {connectionType.fields.map((field, index) => { return ( - - {getFieldView(field)} - + ); })} diff --git a/src/components/Connections/FormikConnectionOptionsSwitchField.tsx b/src/components/Connections/FormikConnectionOptionsSwitchField.tsx new file mode 100644 index 0000000000..bdb09cf13f --- /dev/null +++ b/src/components/Connections/FormikConnectionOptionsSwitchField.tsx @@ -0,0 +1,55 @@ +import { useState } from "react"; +import RenderConnectionFormFields from "./RenderConnectionFormFields"; +import { ConnectionFormFields } from "./connectionTypes"; +import { Switch } from "../Switch"; +import { useFormikContext } from "formik"; + +type Props = { + field: ConnectionFormFields; +}; + +export default function FormikConnectionOptionsSwitchField({ field }: Props) { + const [selectedGroup, setSelectedGroup] = useState(field.default); + + const { setFieldValue } = useFormikContext>(); + + if (!field.options) { + return null; + } + + const selectedField = field.options.find( + (option) => option.key === selectedGroup + ); + + return ( +
+ +
+ option.label)]} + defaultValue="None" + value={ + field.options?.find((option) => option.key === selectedGroup)?.label + } + onChange={(v) => { + // reset all other fields that are not selected + field.options?.forEach((option) => { + if (option.key === v) { + return; + } + setFieldValue(option.key, undefined); + }); + setSelectedGroup( + field.options?.find((option) => option.label === v)?.key + ); + }} + /> +
+
+ {selectedField?.fields?.map((field) => ( + + ))} +
+
+ ); +} diff --git a/src/components/Connections/RenderConnectionFormFields.tsx b/src/components/Connections/RenderConnectionFormFields.tsx new file mode 100644 index 0000000000..f3d9b3d628 --- /dev/null +++ b/src/components/Connections/RenderConnectionFormFields.tsx @@ -0,0 +1,60 @@ +import FormikConnectionOptionsSwitchField from "./FormikConnectionOptionsSwitchField"; +import FormikCheckbox from "../Forms/Formik/FormikCheckbox"; +import { FormikEnvVarSource } from "../Forms/Formik/FormikEnvVarSource"; +import FormikTextInput from "../Forms/Formik/FormikTextInput"; +import { ConnectionFormFields } from "./connectionTypes"; + +interface FieldViewProps { + field: ConnectionFormFields; +} + +export default function RenderConnectionFormFields({ field }: FieldViewProps) { + const type = field.type ?? "input"; + switch (type) { + case "input": + return ( + + ); + case "numberInput": + return ( + + ); + case "checkbox": + return ( + + ); + case "EnvVarSource": + return ( + + ); + case "switch": + return ; + default: + return null; + } +} diff --git a/src/components/Connections/connectionTypes.tsx b/src/components/Connections/connectionTypes.tsx index 03ca2d6e00..294f81ecd0 100644 --- a/src/components/Connections/connectionTypes.tsx +++ b/src/components/Connections/connectionTypes.tsx @@ -7,7 +7,8 @@ const enum ConnectionsFieldTypes { checkbox = "checkbox", input = "input", numberInput = "numberInput", - EnvVarSource = "EnvVarSource" + EnvVarSource = "EnvVarSource", + switch = "switch" } type Variant = "small" | "large"; @@ -17,7 +18,7 @@ const variants: { [key: string]: Variant } = { large: "large" }; -export type Field = { +export type ConnectionFormFields = { label: string; key: string; type: ConnectionsFieldTypes; @@ -25,6 +26,11 @@ export type Field = { required?: boolean; hint?: string; default?: boolean | number | string; + options?: { + label: string; + key: string; + fields: Omit[]; + }[]; }; export const enum ConnectionValueType { @@ -74,7 +80,7 @@ export type ConnectionType = { title: string; value: ConnectionValueType; icon?: React.ReactNode | string | null; - fields: Field[]; + fields: ConnectionFormFields[]; convertToFormSpecificValue?: (data: Record) => Connection; preSubmitConverter?: (data: Record) => object; hide?: boolean; @@ -1826,22 +1832,40 @@ export const connectionTypes: ConnectionType[] = [ required: true }, { - label: "Username", - key: "username", - type: ConnectionsFieldTypes.EnvVarSource, - variant: variants.large - }, - { - label: "Password", - key: "password", - type: ConnectionsFieldTypes.EnvVarSource, - variant: variants.large - }, - { - label: "SSH Key", - key: "certificate", - type: ConnectionsFieldTypes.EnvVarSource, - variant: variants.large + label: "Authentication", + key: "authentication", + type: ConnectionsFieldTypes.switch, + default: "certificate", + options: [ + { + label: "SSH Key", + key: "certificate", + fields: [ + { + label: "SSH Key", + key: "certificate", + type: ConnectionsFieldTypes.EnvVarSource, + variant: variants.large + } + ] + }, + { + label: "Password", + key: "password", + fields: [ + { + label: "Username", + key: "username", + type: ConnectionsFieldTypes.EnvVarSource + }, + { + label: "Password", + key: "password", + type: ConnectionsFieldTypes.EnvVarSource + } + ] + } + ] }, { label: "Ref",