diff --git a/src/Components/Facility/DischargeModal.tsx b/src/Components/Facility/DischargeModal.tsx index 8376a0ad299..b069b371a72 100644 --- a/src/Components/Facility/DischargeModal.tsx +++ b/src/Components/Facility/DischargeModal.tsx @@ -75,7 +75,7 @@ const DischargeModal = ({ const [latestClaim, setLatestClaim] = useState(); const [isCreateClaimLoading, setIsCreateClaimLoading] = useState(false); const [isSendingDischargeApi, setIsSendingDischargeApi] = useState(false); - const [facility, setFacility] = useState({ id: 0, name: "" }); // for referred to external + const [facility, setFacility] = useState(); const [errors, setErrors] = useState({}); const fetchLatestClaim = useCallback(async () => { @@ -186,12 +186,13 @@ const DischargeModal = ({ const prescriptionActions = PrescriptionActions(consultationData.id ?? ""); const handleFacilitySelect = (selected: FacilityModel) => { - setFacility(selected ? selected : facility); - const { id, name } = selected; + setFacility(selected); + const { id, name } = selected || {}; const isExternal = id === -1; setPreDischargeForm((prev) => ({ ...prev, - ...(isExternal ? { referred_to_external: name } : { referred_to: id }), + referred_to: isExternal ? null : id, + referred_to_external: isExternal ? name : null, })); }; @@ -238,8 +239,8 @@ const DischargeModal = ({ handleFacilitySelect(selected as FacilityModel) } selected={facility} - showAll={true} - freeText={true} + showAll + freeText multiple={false} errors={errors?.referred_to} className="mb-4" diff --git a/src/Components/Form/AutoCompleteAsync.tsx b/src/Components/Form/AutoCompleteAsync.tsx index d06067af957..5f33c6388c5 100644 --- a/src/Components/Form/AutoCompleteAsync.tsx +++ b/src/Components/Form/AutoCompleteAsync.tsx @@ -7,6 +7,7 @@ import { MultiSelectOptionChip, dropdownOptionClassNames, } from "./MultiSelectMenuV2"; +import { useTranslation } from "react-i18next"; interface Props { name?: string; @@ -23,6 +24,7 @@ interface Props { placeholder?: string; disabled?: boolean; error?: string; + required?: boolean; onBlur?: () => void; onFocus?: () => void; } @@ -42,11 +44,13 @@ const AutoCompleteAsync = (props: Props) => { className = "", placeholder, disabled = false, + required = false, error, } = props; const [data, setData] = useState([]); const [query, setQuery] = useState(""); const [loading, setLoading] = useState(false); + const { t } = useTranslation(); const hasSelection = (!multiple && selected) || (multiple && selected?.length > 0); @@ -86,9 +90,7 @@ const AutoCompleteAsync = (props: Props) => { : placeholder || "Start typing to search..." } displayValue={() => - hasSelection && !multiple - ? optionLabel && optionLabel(selected) - : "" + hasSelection && !multiple ? optionLabel?.(selected) : "" } onChange={({ target }) => setQuery(target.value)} onFocus={props.onFocus} @@ -100,6 +102,20 @@ const AutoCompleteAsync = (props: Props) => { />
+ {hasSelection && !loading && !required && ( +
+ { + e.preventDefault(); + onChange(null); + }} + /> + + {t("clear_selection")} + +
+ )} {loading ? ( ) : (