Feat/tag selector (#485)
This commit is contained in:
+33
-13
@@ -1,4 +1,5 @@
|
||||
import { forwardRef } from "react"
|
||||
import { forwardRef, useCallback, useMemo } from "react"
|
||||
import type { ReactElement } from "react"
|
||||
|
||||
import { cn } from "~/lib/utils"
|
||||
|
||||
@@ -12,8 +13,11 @@ type InputProps<TMultiline extends boolean> = {
|
||||
error?: string
|
||||
help?: React.ReactNode
|
||||
multiline?: TMultiline
|
||||
renderInput?: (props: Omit<InputProps<false>, "renderInput">) => ReactElement
|
||||
} & React.ComponentPropsWithRef<TMultiline extends true ? "textarea" : "input">
|
||||
|
||||
export type CustomInputProps = Omit<InputProps<false>, "renderInput">
|
||||
|
||||
export const Input = forwardRef(function Input<
|
||||
TMultiline extends boolean = false,
|
||||
>(
|
||||
@@ -26,6 +30,7 @@ export const Input = forwardRef(function Input<
|
||||
error,
|
||||
help,
|
||||
multiline,
|
||||
renderInput,
|
||||
...inputProps
|
||||
}: InputProps<TMultiline>,
|
||||
ref: TMultiline extends true
|
||||
@@ -34,7 +39,32 @@ export const Input = forwardRef(function Input<
|
||||
) {
|
||||
const hasAddon = !!addon
|
||||
const hasPrefix = !!prefix
|
||||
const Component = (multiline ? "textarea" : "input") as any
|
||||
|
||||
const inputComponentProps = useMemo(
|
||||
() =>
|
||||
({
|
||||
...inputProps,
|
||||
ref,
|
||||
className: cn(
|
||||
"input",
|
||||
hasAddon && `has-addon`,
|
||||
hasPrefix && `has-prefix`,
|
||||
isBlock && `is-block`,
|
||||
className,
|
||||
),
|
||||
} as any),
|
||||
[className, hasAddon, hasPrefix, inputProps, isBlock, ref],
|
||||
)
|
||||
|
||||
const renderInputComponent = useCallback(() => {
|
||||
if (renderInput) {
|
||||
return renderInput(inputComponentProps)
|
||||
} else if (multiline) {
|
||||
return <textarea {...inputComponentProps} />
|
||||
} else {
|
||||
return <input {...inputComponentProps} />
|
||||
}
|
||||
}, [inputComponentProps, multiline, renderInput])
|
||||
|
||||
return (
|
||||
<div>
|
||||
@@ -45,17 +75,7 @@ export const Input = forwardRef(function Input<
|
||||
{prefix}
|
||||
</span>
|
||||
)}
|
||||
<Component
|
||||
{...inputProps}
|
||||
ref={ref as any}
|
||||
className={cn(
|
||||
"input",
|
||||
hasAddon && `has-addon`,
|
||||
hasPrefix && `has-prefix`,
|
||||
isBlock && `is-block`,
|
||||
className,
|
||||
)}
|
||||
/>
|
||||
{renderInputComponent()}
|
||||
{addon && (
|
||||
<span className="flex items-center px-3 text-gray-600 bg-gray-50 h-10 border border-l-0 rounded-r-lg relative -z-10">
|
||||
{addon}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import { Fragment, useState } from "react"
|
||||
import { toast } from "react-hot-toast"
|
||||
import { shallow } from "zustand/shallow"
|
||||
|
||||
import { Combobox, Transition } from "@headlessui/react"
|
||||
import { ChevronUpDownIcon, XMarkIcon } from "@heroicons/react/20/solid"
|
||||
|
||||
import { useEditorState } from "~/hooks/useEdtiorState"
|
||||
import { cn } from "~/lib/utils"
|
||||
|
||||
import { CustomInputProps } from "./Input"
|
||||
|
||||
type Props = CustomInputProps & {
|
||||
userTags: string[]
|
||||
onTagChange: (tags: string) => void
|
||||
}
|
||||
|
||||
export function TagInput({
|
||||
userTags = [],
|
||||
value,
|
||||
onTagChange,
|
||||
...restProps
|
||||
}: Props) {
|
||||
const [selected, setSelected] = useState("")
|
||||
const [query, setQuery] = useState("")
|
||||
|
||||
const { editorTags, setValues } = useEditorState(
|
||||
(state) => ({
|
||||
editorTags: state.tags === "" ? [] : state.tags?.split(","),
|
||||
setValues: state.setValues,
|
||||
}),
|
||||
shallow,
|
||||
)
|
||||
|
||||
const onChange = (type: "add" | "delete", value: string) => {
|
||||
let tags = ""
|
||||
if (type === "add") {
|
||||
if (editorTags.includes(value)) {
|
||||
toast.error("Duplicate tag")
|
||||
return
|
||||
}
|
||||
tags = [...editorTags, value].join(",")
|
||||
} else {
|
||||
tags = editorTags.filter((tag) => tag !== value).join(",")
|
||||
}
|
||||
|
||||
setValues({ tags })
|
||||
onTagChange(tags)
|
||||
}
|
||||
|
||||
const onComboboxChange = (value: string) => {
|
||||
onChange("add", value)
|
||||
}
|
||||
|
||||
const onDel = (value: string) => {
|
||||
onChange("delete", value)
|
||||
}
|
||||
|
||||
const filteredTags =
|
||||
query === ""
|
||||
? userTags
|
||||
: Array.from(new Set([query, ...userTags])).filter((tag) =>
|
||||
tag
|
||||
.toLowerCase()
|
||||
.replace(/\s+/g, "")
|
||||
.includes(query.toLowerCase().replace(/\s+/g, "")),
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="w-full">
|
||||
<div className="flex flex-wrap gap-2 mb-2">
|
||||
{editorTags.map((tag) => (
|
||||
<div
|
||||
className="text-xs flex items-center font-semibold p-1 rounded-md bg-accent text-white"
|
||||
key={tag}
|
||||
>
|
||||
<span>{tag}</span>
|
||||
<XMarkIcon className="h-5 w-5" onClick={() => onDel(tag)} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<Combobox value={selected} onChange={onComboboxChange}>
|
||||
<div className="relative mt-1">
|
||||
<div className="relative flex w-full cursor-default overflow-hidden rounded-lg bg-white text-left sm:text-sm">
|
||||
<Combobox.Input
|
||||
{...restProps}
|
||||
onChange={(event) => setQuery(event.target.value)}
|
||||
autoComplete="off"
|
||||
/>
|
||||
<Combobox.Button className="absolute inset-y-0 right-0 flex items-center pr-2">
|
||||
<ChevronUpDownIcon
|
||||
className="h-5 w-5 text-black"
|
||||
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 max-h-60 w-full overflow-auto rounded-md bg-white p-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 sm:text-sm">
|
||||
{filteredTags.map((tag) => (
|
||||
<Combobox.Option
|
||||
key={tag}
|
||||
className={({ active }) =>
|
||||
cn(
|
||||
"relative cursor-default select-none py-2 px-4",
|
||||
active ? "bg-accent text-white" : "text-black",
|
||||
)
|
||||
}
|
||||
value={tag}
|
||||
>
|
||||
<span className={cn("block truncate font-normal")}>
|
||||
{tag}
|
||||
</span>
|
||||
</Combobox.Option>
|
||||
))}
|
||||
</Combobox.Options>
|
||||
</Transition>
|
||||
</div>
|
||||
</Combobox>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user