import { FC, ReactElement, useMemo, useState } from 'react'; import CommonUpsInputGroup from './CommonUpsInputGroup'; import FlexBox from '../FlexBox'; import SelectWithLabel from '../SelectWithLabel'; import Spinner from '../Spinner'; import { BodyText } from '../Text'; const AddUpsInputGroup: FC = ({ loading: isExternalLoading, upsTemplate, }) => { const [inputUpsAgentValue, setInputUpsAgentValue] = useState(''); const upsAgentOptions = useMemo( () => upsTemplate ? Object.entries(upsTemplate).map( ([upsTypeId, { brand, description }]) => ({ displayValue: ( {brand} {description} ), value: upsTypeId, }), ) : [], [upsTemplate], ); const pickUpsAgentElement = useMemo( () => upsTemplate && ( { const newValue = String(rawNewValue); setInputUpsAgentValue(newValue); }} selectItems={upsAgentOptions} selectProps={{ onClearIndicatorClick: () => { setInputUpsAgentValue(''); }, renderValue: (rawValue) => { const upsTypeId = String(rawValue); const { brand } = upsTemplate[upsTypeId]; return brand; }, }} value={inputUpsAgentValue} /> ), [inputUpsAgentValue, upsAgentOptions, upsTemplate], ); const content = useMemo( () => isExternalLoading ? ( ) : ( {pickUpsAgentElement} {inputUpsAgentValue && } ), [inputUpsAgentValue, isExternalLoading, pickUpsAgentElement], ); return content; }; export default AddUpsInputGroup;