yairEO / tagify

๐Ÿ”– lightweight, efficient Tags input component in Vanilla JS / React / Angular / Vue
https://yaireo.github.io/tagify/
Other
3.55k stars 436 forks source link

on input value strange #1382

Closed jooyoni closed 1 month ago

jooyoni commented 1 month ago

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

`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, });

  tagifyRef.current.on("add", () => {
    if (!tagifyRef.current) return;
    if (tagifyRef.current?.value.length === 10) {
      (tagifyRef.current as any).setContentEditable(false);
      tagifyRef.current?.setPlaceholder("");
    }
    if (onUpdateTag) onUpdateTag(tagifyRef.current.value.map((datum) => datum.value));
  });
  tagifyRef.current.on("remove", () => {
    if (!tagifyRef.current) return;
    if (tagifyRef.current && tagifyRef.current?.value.length !== 10) {
      (tagifyRef.current as any).setContentEditable(true);
      tagifyRef.current?.setPlaceholder("#ํƒœ๊ทธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
    }
    if (onUpdateTag) onUpdateTag(tagifyRef.current.value.map((datum) => datum.value));
  });
  tagifyRef.current.on("input", (e) => {
    const value = (e.detail as { value: string }).value;
    setInputtingValue(value);
  });
}

return () => {
  tagifyRef.current?.destroy();
};

}, []);

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); }}

{isFocus && 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 (

# {input}
{!!activeRecommend.length && (
์ถ”์ฒœ ํƒœ๊ทธ
    {activeRecommend.map((word, idx) => (
  • { setActiveIndex(idx); }} className={idx === activeIndex ? "active" : ""} > # {word} {idx === activeIndex && ( )}
  • ))}
)}

); }

export default TagInput; `

jooyoni commented 1 month ago

sorry it's my mistake