import type { Dispatch, SetStateAction } from "react"; import { forwardRef, useCallback, useEffect, useState } from "react"; import type { InputProps } from "./input"; import { Input } from "./input"; type BadgeInputProps = Omit & { value: string[]; onChange: (value: string[]) => void; setPendingKeyword?: Dispatch>; }; export const BadgeInput = forwardRef( ({ value, onChange, setPendingKeyword, ...props }, ref) => { const [label, setLabel] = useState(""); const processInput = useCallback(() => { const newLabels = label .split(",") .map((str) => str.trim()) .filter(Boolean) .filter((str) => !value.includes(str)); onChange([...new Set([...value, ...newLabels])]); setLabel(""); }, [label, value, onChange]); useEffect(() => { if (label.includes(",")) { processInput(); } }, [label, processInput]); useEffect(() => { if (setPendingKeyword) setPendingKeyword(label); }, [label, setPendingKeyword]); const onKeyDown = (event: React.KeyboardEvent) => { if (event.key === "Enter") { event.preventDefault(); event.stopPropagation(); processInput(); } }; return ( { setLabel(event.target.value); }} /> ); }, );