-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add option to have switch field for connection form
- Loading branch information
1 parent
1f17117
commit f098f91
Showing
4 changed files
with
164 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/components/Connections/FormikConnectionOptionsSwitchField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Record<string, any>>(); | ||
|
||
if (!field.options) { | ||
return null; | ||
} | ||
|
||
const selectedField = field.options.find( | ||
(option) => option.key === selectedGroup | ||
); | ||
|
||
return ( | ||
<div className="flex flex-col gap-4 overflow-y-auto"> | ||
<label className="font-semibold text-sm">{field.label}</label> | ||
<div className="flex flex-row"> | ||
<Switch | ||
options={[...field.options?.map((option) => 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 | ||
); | ||
}} | ||
/> | ||
</div> | ||
<div className="flex flex-col gap-4 overflow-y-auto px-4"> | ||
{selectedField?.fields?.map((field) => ( | ||
<RenderConnectionFormFields field={field} key={field.key} /> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<FormikTextInput | ||
name={field.key} | ||
label={field.label} | ||
required={field.required} | ||
hint={field.hint} | ||
defaultValue={field.default?.toString()} | ||
/> | ||
); | ||
case "numberInput": | ||
return ( | ||
<FormikTextInput | ||
type="number" | ||
name={field.key} | ||
label={field.label} | ||
required={field.required} | ||
hint={field.hint} | ||
defaultValue={field.default?.toString()} | ||
/> | ||
); | ||
case "checkbox": | ||
return ( | ||
<FormikCheckbox | ||
name={field.key} | ||
label={field.label} | ||
labelClassName="text-sm font-semibold text-gray-700" | ||
required={field.required} | ||
hint={field.hint} | ||
/> | ||
); | ||
case "EnvVarSource": | ||
return ( | ||
<FormikEnvVarSource | ||
name={field.key} | ||
label={field.label} | ||
variant={field.variant} | ||
hint={field.hint} | ||
required={field.required} | ||
/> | ||
); | ||
case "switch": | ||
return <FormikConnectionOptionsSwitchField field={field} />; | ||
default: | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters