import React from "react"; import { ValidIconName } from "../Icon/canonicalIconNames"; import MenuItem from "../Menu/MenuItem"; import OverflowText from "../Typography/OverflowText"; import { TestIconProps } from "./../Icon/TestIcon"; import { SuggestFieldItemRendererModifierProps } from "./interfaces"; /** * Returns a function to be used in an AutoComplete widget for rendering custom elements based on the query string. * * @param itemTextRenderer The text or element that should be displayed for the new custom item suggestion. * @param iconName Optional icon to show left to the text. */ export const createNewItemRendererFactory = ( itemTextRenderer: (query: string) => string | React.JSX.Element, iconName?: ValidIconName | React.ReactElement, ) => { // Return custom render function return ( query: string, modifiers: SuggestFieldItemRendererModifierProps, handleClick: React.MouseEventHandler, ) => { let textElement = itemTextRenderer(query); if (typeof textElement === "string") { textElement = ( {textElement.trim() !== "" ? textElement : `Create option '${query}'`} ); } return ( ); }; };