import React, { FormEvent, forwardRef, RefObject, useEffect } from "react"; import { Box } from "../../Box"; import { ICON_TYPE } from "../../Icon"; import { Input, INPUT_ICON_POSITION } from "../../Input"; interface SelectSearchProps { value: string | undefined; onChange: (e: FormEvent) => void; setInputHasFocus: (newVal: boolean) => void; popperRef: RefObject; } export const SelectSearch = forwardRef( ( { value, onChange, setInputHasFocus, popperRef }: SelectSearchProps, ref, ) => { useEffect(() => { // make copies of refs for use in the callback const inputRef = ref; const popperRefCopy = popperRef; return () => { // Set focus to false since onBlur might not get called when unmounting setInputHasFocus(false); // If the input was still focused when unmounting this component, then we // should redirect focus to the menu (aka popper). This is what downshift // would have done if this were a regular select item. if ( inputRef && "current" in inputRef && popperRefCopy && popperRefCopy.current ) { if (inputRef.current === document.activeElement) { popperRefCopy.current.focus(); } } }; }, [setInputHasFocus, ref, popperRef]); return ( { setInputHasFocus(true); }} onBlur={() => { setInputHasFocus(false); }} /> ); }, );