Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autocomplete unable to pre-fill in certain usages; Adds utility fn. to merge autocomplete options; Fixes facility select in resource form edge case #10646

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,12 @@ export const stringifyGeoOrganization = (org: Organization) => {

return levels.join(", ");
};

export const mergeAutocompleteOptions = (
options: { label: string; value: string }[],
value?: { label: string; value: string },
) => {
if (!value) return options;
if (options.find((o) => o.value === value.value)) return options;
return [value, ...options];
};
11 changes: 10 additions & 1 deletion src/components/Resource/ResourceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { RESOURCE_CATEGORY_CHOICES } from "@/common/constants";
import routes from "@/Utils/request/api";
import mutate from "@/Utils/request/mutate";
import query from "@/Utils/request/query";
import { mergeAutocompleteOptions } from "@/Utils/utils";
import validators from "@/Utils/validators";
import facilityApi from "@/types/facility/facilityApi";
import { ResourceRequest } from "@/types/resourceRequest/resourceRequest";
Expand Down Expand Up @@ -274,7 +275,15 @@ export default function ResourceForm({ facilityId, id }: ResourceProps) {
</FormLabel>
<FormControl>
<Autocomplete
options={facilityOptions ?? []}
options={mergeAutocompleteOptions(
facilityOptions ?? [],
field.value
? {
label: field.value.name,
value: field.value.id,
}
: undefined,
)}
value={field.value?.id ?? ""}
placeholder={t("start_typing_to_search")}
onSearch={setFacilitySearch}
Expand Down
5 changes: 3 additions & 2 deletions src/components/ui/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ export default function Autocomplete({
{options.map((option) => (
<CommandItem
key={option.value}
value={option.value}
value={`${option.label} - ${option.value}`}
onSelect={(v) => {
const currentValue =
options.find((option) => option.value === v)?.value || "";
options.find((o) => `${o.label} - ${o.value}` === v)?.value ||
"";
onChange(currentValue);
setOpen(false);
}}
Expand Down
Loading