hc-oss / react-tag-input-component

lightweight react component for tag(s) input
https://codesandbox.io/s/react-tag-input-component-rgf97
MIT License
80 stars 36 forks source link

No way to create tags on mobile #46

Open ibrahimfarooq17 opened 1 year ago

ibrahimfarooq17 commented 1 year ago

The component doesn't allow any way to create tags using a mobile browser. I tried adding different separators as well, still doesn't work. Tested on Chrome.

mo7medhasan commented 1 year ago

to create tags on mobile

you must add button

codesandbox


export default function App() {
  const [selected, setSelected] = useState(["papaya"]);
  const handleButtonClick = () => {
    const InputElement = document.querySelector(`#id .rti--input`);
    if (InputElement) {
      const value = InputElement.value;
      if (!selected.includes(value) && value !== "") {
        setSelected([...selected, value]);
        InputElement.value = "";
      }
    }
  };
  return (
    <div>
      <h1>Add Fruits</h1>

      <pre>{JSON.stringify(selected)}</pre>
      <div id={"id"}>
        <TagsInput value={selected} onChange={setSelected} />

        <button
          type="button"
          onClick={handleButtonClick}
          className="px-6 py-2 text-white bg-primary rounded-md"
        >
          add tage
        </button>
      </div>

    </div>
  );
}
ComradePashka commented 7 months ago

seems you also can simply add same logic to onBlur handler:

            onBlur={(e) => {
                const InputElement = document.querySelector(`[name=${name}]`);
                if (InputElement) {
                    const newValue = InputElement.value;
                    if (!value.includes(newValue) && newValue !== "") {
                        setValue([...value, newValue]);
                        InputElement.value = "";
                    }
                }
            }}

in this case you must pass name to TagInput, but then you don't need extra div-wrapper around TagInput and can simplify selector to find element by name attribute. this is working in Chrome dev-console with device mode toggled, not yet tested on real mobiles.