i-like-robots / react-tag-autocomplete

⚛️ A simple, accessible, tagging component ready to drop into your React projects (new repo)
https://i-like-robots.github.io/react-tag-autocomplete/
ISC License
178 stars 12 forks source link

Collapse the list box if user is removing a selected tag #61

Closed rjtormis closed 8 months ago

rjtormis commented 11 months ago

Is there any way to disable the listbox when the user wants to remove the tags? Whenever a user (lets say its me) want to remove a particular tag the listbox automatically appears.

i-like-robots commented 11 months ago

When a tag is removed the cursor is shifted to the input automatically so that focus remains within the context of the component rather than being lost and defaulting to the document. I can see how this behaviour may not always be ideal, do you have any suggestions for how this could be improved?

rjtormis commented 11 months ago

I managed to find a way by adding onShouldCollapse. I'm not sure if this is the right way but temporarily this is my implementation

const handleShouldExpand = useCallback(() => {
    if (hidden) {
      setHidden(false);
      return false;
    }
    return true;
  }, [hidden]);

utilize some useState here which is triggered by onDelete callback

const handleRemoveDocsLink = useCallback(
    (tagIndex: number) => {
      setHidden(true);
      setSelected(selected.filter((_: any, i: number) => i !== tagIndex));
    },
    [selected]
  );