What is the expected behavior?
When the Enter key is pressed, the entered message should be added as a tag, and the previously entered message should disappear.
What is happening instead?
When the Enter key is pressed, if the last character of the entered content is Korean, the last character is returned in onInput, followed by ',,,'.
Prerequisites
๐ฅ Demo Page
https://jsbin.com/jekuqap/edit?html,js,output
React issue template: https://codesandbox.io/s/tagify-react-issue-template-4ub1r?file=/src/index.js
Explanation
What is the expected behavior? When the Enter key is pressed, the entered message should be added as a tag, and the previously entered message should disappear.
What is happening instead? When the Enter key is pressed, if the last character of the entered content is Korean, the last character is returned in onInput, followed by ',,,'.
`import "./style.scss"; import { useRef, useEffect, useCallback, useState } from "react"; import Tagify from "@yaireo/tagify"; import "@yaireo/tagify/dist/tagify.css"; // Tagify CSS๋ฅผ ์ํฌํธ
interface IPropsType { onUpdateTag?: (value: string[]) => void; }
const TagInput = ({ onUpdateTag }: IPropsType) => { const [inputtingValue, setInputtingValue] = useState(""); const [isFocus, setIsFocus] = useState(false);
const inputRef = useRef<HTMLInputElement | null>(null); const tagifyRef = useRef<Tagify | null>(null);
useEffect(() => { if (inputRef.current) { tagifyRef.current = new Tagify(inputRef.current, { maxTags: 10, placeholder: "#ํ๊ทธ๋ฅผ ์ ๋ ฅํ์ธ์", addTagOnBlur: false, addTagOn: undefined, onChangeAfterBlur: false, });
}, []);
function onSelect(value: string) { if (!tagifyRef.current) return; setInputtingValue(""); tagifyRef.current.addTags(value, true); } return ( <> <div className='haycorn-tag-input-box' tabIndex={1} onFocus={() => setIsFocus(true)} onBlur={(e) => { if (e.relatedTarget) return; setIsFocus(false); onSelect(inputtingValue); }}
interface IRecommendTagProps { input: string; onSelect: (value: string) => void; } const TEMP_DATA = ["banana", "apple", "grape", "orange", "mango", "watermelon"]; function RecommendTag({ input, onSelect }: IRecommendTagProps) { const inputRef = useRef(input); useEffect(() => { inputRef.current = input; }, [input]);
const [activeIndex, setActiveIndex] = useState(-1); const activeIndexRef = useRef(-1);
useEffect(() => { activeIndexRef.current = activeIndex; }, [activeIndex]);
const [activeRecommend, setActiveRecommend] = useState<string[]>([]); const activeRecommendRef = useRef<string[]>([]);
useEffect(() => { activeRecommendRef.current = activeRecommend; }, [activeRecommend]);
useEffect(() => { setActiveRecommend(TEMP_DATA.filter((text) => text.includes(input))); }, [input]);
useEffect(() => { function keyEvent(e: KeyboardEvent) { switch (e.key) { case "ArrowDown": if (activeIndexRef.current >= activeRecommendRef.current.length - 1) setActiveIndex(0); else setActiveIndex((prev) => prev + 1); break; case "ArrowUp": if (activeIndexRef.current <= 0) setActiveIndex(activeRecommendRef.current.length - 1); else setActiveIndex((prev) => prev - 1); break; case "Enter": if (activeIndexRef.current === -1) onSelect(inputRef.current); onSelect(activeRecommendRef.current[activeIndexRef.current]); break; } } window.addEventListener("keydown", keyEvent); return () => { window.removeEventListener("keydown", keyEvent); }; }, []);
return (
{activeRecommend.map((word, idx) => (- {
setActiveIndex(idx);
}}
className={idx === activeIndex ? "active" : ""}
>
#
{word}
{idx === activeIndex && (
)}
))}
); }
export default TagInput; `