import type { ReactNode } from 'react' import React, { Fragment, type HTMLAttributes } from 'react' import { Icon, Link, type LinkElementType, type LinkProps } from '../..' function formatSearchTerm( indexSubstring: number, searchTerm: string, suggestion: string ) { if (indexSubstring === 0) { return searchTerm .split('') .map((char, idx) => idx === 0 && suggestion.indexOf(char.toUpperCase()) === 0 ? char.toUpperCase() : char.toLowerCase() ) .join('') } return searchTerm.toLowerCase() } export interface SearchAutoCompleteTermProps extends HTMLAttributes { /** * ID to find this component in testing tools (e.g.: cypress, testing library, and jest). */ testId?: string /** * Props for the link from term component. */ linkProps?: Partial> /** * A React component that will be rendered as an icon. */ icon?: ReactNode /** * Term researched. */ term: string /** * Suggestion proposed with auto complete. */ suggestion: string } const SearchAutoCompleteTerm = ({ testId = 'fs-search-auto-complete-term', suggestion, term, linkProps, icon = , }: SearchAutoCompleteTermProps) => { const suggestionSubstring = suggestion.toLowerCase().split(term.toLowerCase()) return (
  • {icon}

    {suggestionSubstring.map((substring, indexSubstring) => ( {substring.length > 0 && ( {indexSubstring === 0 ? substring.charAt(0).toUpperCase() + substring.slice(1) : substring} )} {indexSubstring !== suggestionSubstring.length - 1 && formatSearchTerm(indexSubstring, term, suggestion)} ))}

  • ) } export default SearchAutoCompleteTerm