Giter Site home page Giter Site logo

Comments (2)

DanWebb avatar DanWebb commented on August 16, 2024 2

A workaround could be to wrap the component in another element, then watch the click event on it to focus the inner input...

    <div
      id={id}
      onClick={() => (document.querySelector(`#${id} .rti--input`) as HTMLInputElement).focus()}
   >
      <TagsInput
        value={selected}
        onChange={setSelected}
      />
   </div>

Ideally, this lib would expose the ref from the inner input for this kind of thing.

from react-tag-input-component.

bradley avatar bradley commented on August 16, 2024

Here is a fully working (as Id expect UX-wise, anyway) solution. It uses onMouseDown instead of onClick because onMouseDown is fired before any blur event, so will allow the input to always retain focus. YMMV but Ive logged the interaction events and it all seems to work as expected:

const EmailsInput = () => {
  const [selected, setSelected] = useState([]);

  const wrapperRef = useRef(null);

  const handleWrapperClick = e => {
    const wrapperEl = wrapperRef.current;

    if (!wrapperEl) {
      return;
    }

    const rtiContainerEl = wrapperEl.querySelector(".rti--container");
    const rtiInputEl = wrapperEl.querySelector(".rti--input");

    if (!rtiInputEl) {
      return;
    }

    // Prevent default so that the input doesn't lose focus, if the mousedown
    // event occured on the RTI container el wrapped by our outer wrapper el.
    if (e.target === rtiContainerEl) {
      e.preventDefault();
    }

    // Focus the input. If it was previously focused it will retain focus.
    rtiInputEl.focus();
  };

  return (
    <div ref={wrapperRef} onMouseDown={handleWrapperClick}>
      <TagsInput
        value={selected}
        onChange={setSelected}
        name="Email Addresses"
        placeHolder="Add Email Address..."
        isEditOnRemove={true}
        separators={
          [
            "Enter",
            "Tab",
            ",",
            " "
          ]
        }
      />
      <div className="flex flex-row justify-end text-xs text-zinc-500 mt-2">
        <em>Press "return" or "enter" to add new emails.</em>
      </div>
    </div>
  );
};

Bless.

from react-tag-input-component.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.