feat: autocomplete for account input

This commit is contained in:
DIYgod
2023-07-20 21:44:52 +01:00
parent 8b1e945317
commit 0e4d9e21c5
2 changed files with 106 additions and 15 deletions
@@ -55,9 +55,10 @@ const SortableNavigationItem = ({
onChange={(e: ChangeEvent<HTMLInputElement>) =>
updateItem(item.id, { platform: e.target.value })
}
options={Object.keys(PlatformsSyncMap)}
/>
<Input
label="Identity"
label={t("ID")}
required
id={`${item.id}-identity`}
type="text"
@@ -182,21 +183,9 @@ export default function SiteSettingsNavigationPage() {
</Trans>
</span>
</p>
<p>
<span className="text-zinc-800">
<Trans i18n={i18n} ns="dashboard" i18nKey="social tips.p3">
You can also connect to Twitter, Telegram Channel, Medium,
Substack and more and automatically sync content on{" "}
<UniLink href="https://xsync.app/" className="underline">
xSync
</UniLink>
. When you have set up the sync there, it will also show here.
</Trans>
</span>
</p>
</div>
<form onSubmit={handleSubmit}>
<div className="bg-zinc-50 rounded-lg overflow-auto">
<div className="bg-zinc-50 rounded-lg">
{items.length === 0 && (
<div className="text-center text-zinc-500 p-5">
No navigation items yet
+103 -1
View File
@@ -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<TMultiline extends boolean> = {
error?: string
help?: React.ReactNode
multiline?: TMultiline
options?: string[]
renderInput?: (props: Omit<InputProps<false>, "renderInput">) => ReactElement
} & React.ComponentPropsWithRef<TMultiline extends true ? "textarea" : "input">
@@ -31,6 +35,7 @@ export const Input = forwardRef(function Input<
help,
multiline,
renderInput,
options,
...inputProps
}: InputProps<TMultiline>,
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 (
<Combobox
value={selected}
onChange={(value) => {
setSelected(value)
inputComponentProps.onChange?.({
target: {
value,
},
})
}}
>
<div className="relative">
<div className="relative w-full">
<Combobox.Input
{...inputComponentProps}
displayValue={(option: string) => option}
onChange={(event) => {
setQuery(event.target.value)
inputComponentProps.onChange?.(event)
}}
/>
<Combobox.Button className="absolute inset-y-0 right-0 flex items-center pr-2">
<ChevronUpDownIcon
className="h-5 w-5 text-gray-400"
aria-hidden="true"
/>
</Combobox.Button>
</div>
<Transition
as={Fragment}
leave="transition ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
afterLeave={() => setQuery("")}
>
<Combobox.Options className="absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-sm shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none z-10">
{filteredOptions.length === 0 && query !== "" ? (
<div className="relative cursor-default select-none py-2 px-4 text-gray-700">
Nothing found.
</div>
) : (
filteredOptions.map((option) => (
<Combobox.Option
key={option}
className={({ active }) =>
`relative cursor-default select-none py-2 pl-10 pr-4 ${
active ? "bg-accent text-white" : "text-gray-900"
}`
}
value={option}
>
{({ selected, active }) => (
<>
<span
className={`block truncate ${
selected ? "font-medium" : "font-normal"
}`}
>
{option}
</span>
{selected ? (
<span
className={`absolute inset-y-0 left-0 flex items-center pl-3 ${
active ? "text-white" : "text-teal-600"
}`}
>
<CheckIcon
className="h-5 w-5"
aria-hidden="true"
/>
</span>
) : null}
</>
)}
</Combobox.Option>
))
)}
</Combobox.Options>
</Transition>
</div>
</Combobox>
)
} else if (multiline) {
return <textarea {...inputComponentProps} />
} else {