Skip to content

Commit

Permalink
Merge pull request #1496 from flanksource/1361-connection-type-dropdo…
Browse files Browse the repository at this point in the history
…wn-add-icons

fix: add connection type icons to the dropdown
  • Loading branch information
moshloop authored Nov 10, 2023
2 parents 0ab5db7 + 128bdf0 commit aadebfa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/components/Forms/Formik/FormikConnectionField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useQuery } from "@tanstack/react-query";
import { getAll } from "../../../api/schemaResources";
import { toastError } from "../../Toast/toast";
import FormikSelectDropdown from "./FormikSelectDropdown";
import { Icon } from "../../Icon";
import { Connection } from "../../Connections/ConnectionForm";

type Props = {
name: string;
Expand All @@ -24,13 +26,14 @@ export default function FormikConnectionField({
api: "canary-checker",
name: "Connections"
});
return res.data ?? [];
return (res.data as Connection[]) ?? [];
},
select: (connections) => {
return connections.map((connection) => {
return {
label: connection.name,
value: connection.id
value: connection.id!,
icon: <Icon className="h-5" name={connection.type} />
};
});
},
Expand Down
30 changes: 29 additions & 1 deletion src/components/Forms/Formik/FormikSelectDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { useField } from "formik";
import {
import React, {
ComponentProps,
useCallback,
useEffect,
useMemo,
useState
} from "react";
import Select from "react-select";
import { components } from "react-select";

type Option = {
label: string;
value: string;
icon?: React.ReactNode;
};

type Props = {
Expand Down Expand Up @@ -89,6 +91,32 @@ export default function FormikSelectDropdown({
field.onBlur(event);
setIsTouched(true);
}}
components={{
Option: ({ children, ...props }) => {
return (
<components.Option {...props}>
<div className="flex flex-row gap-2 items-center">
{(props.data as any).icon && (
<div>{(props.data as any).icon}</div>
)}
<div className="flex-1">{children}</div>
</div>
</components.Option>
);
},
SingleValue: ({ children, ...props }) => {
return (
<components.SingleValue {...props}>
<div className="flex flex-row gap-2 items-center">
{(props.data as any).icon && (
<div>{(props.data as any).icon}</div>
)}
<div className="flex-1">{children}</div>
</div>
</components.SingleValue>
);
}
}}
{...props}
/>
{hint && <p className="text-sm text-gray-500">{hint}</p>}
Expand Down

0 comments on commit aadebfa

Please sign in to comment.