import React, { useId } from 'react'; import { useTranslation } from 'react-i18next'; import classNames from 'classnames'; import { InlineLoading, InlineNotification, Layer, Search, Tile } from '@carbon/react'; import { ResponsiveWrapper } from '@openmrs/esm-framework'; import type { ConceptReference } from '../../types'; import type { useConceptSearchField } from '../../procedures.resource'; import styles from './concept-search-field.scss'; type ConceptSearchResultsProps = { isSearching: boolean; onSelect: (result: ConceptReference) => void; searchResults: Array; hasSelection: boolean; searchTerm: string; }; export const ConceptSearchField = ({ label, placeholder, field, selectedConcept, onChange, invalid, invalidText, }: { label: string; placeholder: string; field: ReturnType; selectedConcept: ConceptReference | null; onChange: (selectedConcept: ConceptReference | null) => void; invalid?: boolean; invalidText?: string; }) => { const { t } = useTranslation(); const invalidTextId = useId(); const showInvalidText = Boolean(invalid && invalidText); return ( <>
{ field.setSearchTerm(e.target.value); if (selectedConcept) { onChange(null); } }} onClear={() => { field.clear(); onChange(null); }} value={selectedConcept ? selectedConcept.display : field.searchTerm} />
{showInvalidText && (

{invalidText}

)} {field.error ? ( ) : ( { field.setSearchTerm(''); onChange(result); }} /> )} ); }; const ConceptSearchResults = ({ isSearching, onSelect, searchResults, hasSelection, searchTerm, }: ConceptSearchResultsProps) => { const { t } = useTranslation(); if (!searchTerm || hasSelection) { return null; } if (isSearching) { return ; } if (searchResults?.length > 0) { return (
    {searchResults.map((result) => (
  • onSelect(result)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); onSelect(result); } }} role="option" tabIndex={0} > {result.display}
  • ))}
); } return ( {t('noResultsFor', 'No results for')} "{searchTerm}" ); };