From f356406608b353cd4f4ac2653753cc6dfda716d6 Mon Sep 17 00:00:00 2001 From: ahmedobaid23 <111188459+ahmedobaid23@users.noreply.github.com> Date: Wed, 31 Jul 2024 09:24:32 +0500 Subject: [PATCH] Adds prompt for ICD11 search field #8232 (#8233) * Adds prompt for ICD11 search field * Minor changes --- .../AddICD11Diagnosis.tsx | 1 + .../Form/FormFields/Autocomplete.tsx | 76 +++++++++++-------- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/Components/Diagnosis/ConsultationDiagnosisBuilder/AddICD11Diagnosis.tsx b/src/Components/Diagnosis/ConsultationDiagnosisBuilder/AddICD11Diagnosis.tsx index 2476fa57ab0..39f35a4fcf4 100644 --- a/src/Components/Diagnosis/ConsultationDiagnosisBuilder/AddICD11Diagnosis.tsx +++ b/src/Components/Diagnosis/ConsultationDiagnosisBuilder/AddICD11Diagnosis.tsx @@ -71,6 +71,7 @@ export default function AddICD11Diagnosis(props: AddICD11DiagnosisProps) { )} optionLabel={(option) => option.label} optionValue={(option) => option} + minQueryLength={2} onQuery={(query) => refetch({ query: { query } })} isLoading={loading} error={hasError ? t("diagnosis_already_added") : undefined} diff --git a/src/Components/Form/FormFields/Autocomplete.tsx b/src/Components/Form/FormFields/Autocomplete.tsx index 2fbfeacfda4..a891fc75d9a 100644 --- a/src/Components/Form/FormFields/Autocomplete.tsx +++ b/src/Components/Form/FormFields/Autocomplete.tsx @@ -18,6 +18,7 @@ type AutocompleteFormFieldProps = FormFieldBaseProps & { optionDescription?: OptionCallback; optionIcon?: OptionCallback; optionDisabled?: OptionCallback; + minQueryLength?: number; onQuery?: (query: string) => void; dropdownIcon?: React.ReactNode | undefined; isLoading?: boolean; @@ -45,6 +46,7 @@ const AutocompleteFormField = ( optionValue={props.optionValue} optionDescription={props.optionDescription} optionDisabled={props.optionDisabled} + minQueryLength={props.minQueryLength} onQuery={props.onQuery} isLoading={props.isLoading} allowRawInput={props.allowRawInput} @@ -69,6 +71,7 @@ type AutocompleteProps = { optionDescription?: OptionCallback; optionDisabled?: OptionCallback; className?: string; + minQueryLength?: number; onQuery?: (query: string) => void; requiredError?: boolean; isLoading?: boolean; @@ -95,6 +98,7 @@ type AutocompleteProps = { export const Autocomplete = (props: AutocompleteProps) => { const { t } = useTranslation(); const [query, setQuery] = useState(""); // Ensure lower case + useEffect(() => { props.onQuery?.(query); }, [query]); @@ -136,6 +140,7 @@ export const Autocomplete = (props: AutocompleteProps) => { const options = props.allowRawInput ? getOptions() : mappedOptions; const value = options.find((o) => props.value == o.value); + const filteredOptions = props.onQuery === undefined ? options.filter((o) => o.search.includes(query)) @@ -198,43 +203,48 @@ export const Autocomplete = (props: AutocompleteProps) => { - {filteredOptions.length === 0 && ( + {props.minQueryLength && query.length < props.minQueryLength ? ( +
+ {`Please enter at least ${props.minQueryLength} characters to search`} +
+ ) : filteredOptions.length === 0 ? (
No options found
- )} - {filteredOptions.map((option, index) => ( - - {({ active }) => ( -
-
- {option.label} - {option.icon} -
- {option.description && ( -
- {option.description} + ) : ( + filteredOptions.map((option, index) => ( + + {({ active }) => ( +
+
+ {option.label} + {option.icon}
- )} -
- )} -
- ))} + {option.description && ( +
+ {option.description} +
+ )} +
+ )} + + )) + )}