import React from "react"; import { compute } from "compute-scroll-into-view"; import { CLASSPREFIX as eccgui } from "../../configuration/constants"; import { Highlighter, Menu, MenuItem, OverflowText, OverviewItem, OverviewItemDescription, OverviewItemLine, Spacing, Spinner, Tooltip, } from "./../../"; import { CodeAutocompleteFieldSuggestionWithReplacementInfo } from "./AutoSuggestion"; export interface AutoSuggestionListProps extends Omit, "children"> { // The options of the drop down options: Array; // Called when an item has been selected from the drop down onItemSelectionChange: (item: CodeAutocompleteFieldSuggestionWithReplacementInfo) => any; // If the drop down is visible isOpen: boolean; // If the drop down should show a loading state loading?: boolean; // The item from the drop down that is active currentlyFocusedIndex: number; // Callback indicating what item should currently being highlighted, i.e. is either active or is hovered over itemToHighlight: (item: CodeAutocompleteFieldSuggestionWithReplacementInfo | undefined) => any; /** horizontal and vertical offset values in relation to the cursor */ offsetValues?: { x: number; y: number }; } const ListItem = ({ item }: any, ref: any) => { const listItem = ( {item.label ? ( ) : null} {item.description ? ( ) : null} ); return (
{!!item.description && item.description.length > 50 ? ( {listItem} ) : ( <>{listItem} )}
); }; const Item = React.forwardRef(ListItem); /** A drop-down-like list that can be used in combination with other components to show and select items. */ export const AutoSuggestionList = ({ isOpen, options, loading, onItemSelectionChange, currentlyFocusedIndex, itemToHighlight, style, offsetValues, ...otherDivProps }: AutoSuggestionListProps) => { const [hoveredItem, setHoveredItem] = React.useState< CodeAutocompleteFieldSuggestionWithReplacementInfo | undefined >(undefined); // Refs of list items const [refs] = React.useState[]>([]); const dropdownRef = React.useRef(null); const generateRef = (index: number) => { if (!refs[index]) { refs[index] = React.createRef(); } return refs[index]; }; React.useEffect(() => { const listIndexNode = refs[currentlyFocusedIndex]; if (dropdownRef?.current && listIndexNode?.current) { const actions = compute(listIndexNode.current, { boundary: dropdownRef.current, block: "nearest", scrollMode: "if-needed", }); actions.forEach(({ el, top, left }) => { el.scrollTop = top; el.scrollLeft = left; }); } }, [currentlyFocusedIndex, refs]); const focusedItem = options[currentlyFocusedIndex]; // Decide which item to highlight React.useEffect(() => { itemToHighlight(!isOpen ? undefined : hoveredItem || focusedItem); }, [currentlyFocusedIndex, itemToHighlight, focusedItem, isOpen, hoveredItem]); const Loader = ( Fetching suggestions ); const loadingOrHasSuggestions = loading || options.length; if (!loadingOrHasSuggestions || !isOpen) return null; return (
{loading ? ( Loader ) : ( {options.map((item, index) => ( e.preventDefault()} onClick={() => onItemSelectionChange(item)} text={} onMouseEnter={() => setHoveredItem(item)} onMouseLeave={() => setHoveredItem(undefined)} onMouseOver={() => { if (item.value !== hoveredItem?.value) { setHoveredItem(item); } }} /> ))} )}
); };