Skip to content

How to clear the input value on blur? #855

Closed Answered by ericgio
omermecitoglu asked this question in Q&A
Discussion options

You must be logged in to vote

Using a ref is the right way to achieve this behavior. Use onBlur to check whether there are any selections, and clear the component if not:

const Example = () => {
  const typeaheadRef = useRef(null);
  const [selected, setSelected] = useState([]);

  return (
    <Typeahead
      id="clear-on-blur"
      onBlur={() => {
        if (!selected.length) {
          typeaheadRef.current.clear();
        }
      }}
      onChange={setSelected}
      options={options}
      ref={typeaheadRef}
      selected={selected}
    />
  );
};

Working sandbox

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ericgio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants