Skip to content

Commit

Permalink
(fix) selectedItem logic
Browse files Browse the repository at this point in the history
  • Loading branch information
usamaidrsk committed Sep 23, 2024
1 parent 4f12e7b commit 014ab66
Showing 1 changed file with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useCallback, useMemo, useState, useEffect } from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import classNames from 'classnames';
import { useField } from 'formik';
import { Field, useField } from 'formik';
import { type PersonAttributeTypeResponse } from '../../patient-registration.types';
import styles from './../field.scss';
import { useLocations } from './location-person-attribute-field.resource';
import { ComboBox, Layer } from '@carbon/react';
import { useTranslation } from 'react-i18next';

export interface LocationPersonAttributeFieldProps {
id: string;
personAttributeType: PersonAttributeTypeResponse;
Expand All @@ -23,19 +24,24 @@ export function LocationPersonAttributeField({
required,
}: LocationPersonAttributeFieldProps) {
const { t } = useTranslation();
const fieldName = `attributes.${personAttributeType.uuid}`;
const [field, meta, { setValue }] = useField(`attributes.${personAttributeType.uuid}`);
const [searchQuery, setSearchQuery] = useState<string>('');
const { locations } = useLocations(locationTag || null, 5, searchQuery);
const { locations } = useLocations(locationTag || null, searchQuery);

const locationOptions = useMemo(() => {
return locations.map(({ resource: { id, name } }) => ({ value: id, label: name }));
}, [locations]);

useEffect(() => {
if (meta?.value?.uuid) {
setValue(meta.value.uuid);
const selectedItem = useMemo(() => {
if (typeof meta.value === 'string') {
return locationOptions.find(({ value }) => value === meta.value) || null;
}
if (typeof meta.value === 'object' && meta.value) {
return locationOptions.find(({ value }) => value === meta.value.uuid) || null;
}
}, [meta, setValue]);
return null;
}, [locationOptions, meta.value]);

const onInputChange = useCallback(
(value: string | null) => {
Expand All @@ -60,16 +66,24 @@ export function LocationPersonAttributeField({
return (
<div className={classNames(styles.customField, styles.halfWidthInDesktopView)}>
<Layer>
<ComboBox
titleText={label}
items={locationOptions}
id={id}
placeholder={t('searchLocationPersonAttribute', 'Search location')}
onInputChange={onInputChange}
required={required}
onChange={handleSelect}
selectedItem={locationOptions.find(({ value }) => value === field.value) || null}
/>
<Field name={fieldName}>
{({ field, form: { touched, errors } }) => {
return (
<ComboBox
id={id}
name={`person-attribute-${personAttributeType.uuid}`}
titleText={label}
items={locationOptions}
placeholder={t('searchLocationPersonAttribute', 'Search location')}
onInputChange={onInputChange}
required={required}
onChange={handleSelect}
selectedItem={selectedItem}
invalid={errors[fieldName] && touched[fieldName]}
/>
);
}}
</Field>
</Layer>
</div>
);
Expand Down

0 comments on commit 014ab66

Please sign in to comment.