({
anchorRef,
onClose,
onSelect,
onTab,
fetchSuggestions,
className,
placeholderText,
children,
}: SuggestionControlInnerProps)
| 168 | type SuggestionControlInnerProps = Omit<SuggestionControlProps, "isOpen">; |
| 169 | |
| 170 | function SuggestionControlInner({ |
| 171 | anchorRef, |
| 172 | onClose, |
| 173 | onSelect, |
| 174 | onTab, |
| 175 | fetchSuggestions, |
| 176 | className, |
| 177 | placeholderText, |
| 178 | children, |
| 179 | }: SuggestionControlInnerProps) { |
| 180 | const widgetId = useId(); |
| 181 | const [query, setQuery] = useState(""); |
| 182 | const reqNumRef = useRef(0); |
| 183 | let [suggestions, setSuggestions] = useState<SuggestionType[]>([]); |
| 184 | const [selectedIndex, setSelectedIndex] = useState(0); |
| 185 | const [fetched, setFetched] = useState(false); |
| 186 | const inputRef = useRef<HTMLInputElement>(null); |
| 187 | const dropdownRef = useRef<HTMLDivElement>(null); |
| 188 | const { refs, floatingStyles, middlewareData } = useFloating({ |
| 189 | placement: "bottom", |
| 190 | strategy: "absolute", |
| 191 | middleware: [offset(-1)], |
| 192 | }); |
| 193 | const emptyStateChild = React.Children.toArray(children).find( |
| 194 | (child) => React.isValidElement(child) && child.type === SuggestionControlNoResults |
| 195 | ); |
| 196 | const noDataChild = React.Children.toArray(children).find( |
| 197 | (child) => React.isValidElement(child) && child.type === SuggestionControlNoData |
| 198 | ); |
| 199 | |
| 200 | useEffect(() => { |
| 201 | refs.setReference(anchorRef.current); |
| 202 | }, [anchorRef.current]); |
| 203 | |
| 204 | useEffect(() => { |
| 205 | reqNumRef.current++; |
| 206 | fetchSuggestions(query, { widgetid: widgetId, reqnum: reqNumRef.current }).then((results) => { |
| 207 | if (results.reqnum !== reqNumRef.current) { |
| 208 | return; |
| 209 | } |
| 210 | setSuggestions(results.suggestions ?? []); |
| 211 | setFetched(true); |
| 212 | }); |
| 213 | }, [query, fetchSuggestions]); |
| 214 | |
| 215 | useEffect(() => { |
| 216 | return () => { |
| 217 | reqNumRef.current++; |
| 218 | fetchSuggestions("", { widgetid: widgetId, reqnum: reqNumRef.current, dispose: true }); |
| 219 | }; |
| 220 | }, []); |
| 221 | |
| 222 | useEffect(() => { |
| 223 | inputRef.current?.focus(); |
| 224 | }, []); |
| 225 | |
| 226 | useEffect(() => { |
| 227 | const handleClickOutside = (event: MouseEvent) => { |
nothing calls this directly
no test coverage detected