From 0e4d9e21c55021bb89722d332f6d20aa2d5c516f Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 20 Jul 2023 21:44:52 +0100 Subject: [PATCH] feat: autocomplete for account input --- .../social-platforms/page-component.tsx | 17 +-- src/components/ui/Input.tsx | 104 +++++++++++++++++- 2 files changed, 106 insertions(+), 15 deletions(-) diff --git a/src/app/dashboard/[subdomain]/settings/social-platforms/page-component.tsx b/src/app/dashboard/[subdomain]/settings/social-platforms/page-component.tsx index 66673984..8f89e36e 100644 --- a/src/app/dashboard/[subdomain]/settings/social-platforms/page-component.tsx +++ b/src/app/dashboard/[subdomain]/settings/social-platforms/page-component.tsx @@ -55,9 +55,10 @@ const SortableNavigationItem = ({ onChange={(e: ChangeEvent) => updateItem(item.id, { platform: e.target.value }) } + options={Object.keys(PlatformsSyncMap)} />

-

- - - You can also connect to Twitter, Telegram Channel, Medium, - Substack and more and automatically sync content on{" "} - - xSync - - . When you have set up the sync there, it will also show here. - - -

-
+
{items.length === 0 && (
No navigation items yet diff --git a/src/components/ui/Input.tsx b/src/components/ui/Input.tsx index e78f2c55..b03574ae 100644 --- a/src/components/ui/Input.tsx +++ b/src/components/ui/Input.tsx @@ -1,6 +1,9 @@ -import { forwardRef, useCallback, useMemo } from "react" +import { Fragment, forwardRef, useCallback, useMemo, useState } from "react" import type { ReactElement } from "react" +import { Combobox, Transition } from "@headlessui/react" +import { CheckIcon, ChevronUpDownIcon } from "@heroicons/react/20/solid" + import { cn } from "~/lib/utils" import { FieldLabel } from "./FieldLabel" @@ -13,6 +16,7 @@ type InputProps = { error?: string help?: React.ReactNode multiline?: TMultiline + options?: string[] renderInput?: (props: Omit, "renderInput">) => ReactElement } & React.ComponentPropsWithRef @@ -31,6 +35,7 @@ export const Input = forwardRef(function Input< help, multiline, renderInput, + options, ...inputProps }: InputProps, ref: TMultiline extends true @@ -56,9 +61,106 @@ export const Input = forwardRef(function Input< [className, hasAddon, hasPrefix, inputProps, isBlock, ref], ) + const [selected, setSelected] = useState() + const [query, setQuery] = useState("") + const filteredOptions = options + ? query === "" + ? options + : options.filter((option) => + option + .toLowerCase() + .replace(/\s+/g, "") + .includes(query.toLowerCase().replace(/\s+/g, "")), + ) + : [] + const renderInputComponent = useCallback(() => { if (renderInput) { return renderInput(inputComponentProps) + } else if (options) { + return ( + { + setSelected(value) + inputComponentProps.onChange?.({ + target: { + value, + }, + }) + }} + > +
+
+ option} + onChange={(event) => { + setQuery(event.target.value) + inputComponentProps.onChange?.(event) + }} + /> + + +
+ setQuery("")} + > + + {filteredOptions.length === 0 && query !== "" ? ( +
+ Nothing found. +
+ ) : ( + filteredOptions.map((option) => ( + + `relative cursor-default select-none py-2 pl-10 pr-4 ${ + active ? "bg-accent text-white" : "text-gray-900" + }` + } + value={option} + > + {({ selected, active }) => ( + <> + + {option} + + {selected ? ( + + + ) : null} + + )} + + )) + )} +
+
+
+
+ ) } else if (multiline) { return