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 INPUT_ID_UPS_TYPE_ID = 'add-ups-select-ups-type-id'; const AddUpsInputGroup: FC = ({ loading: isExternalLoading, previous = {}, upsTemplate, }) => { const { upsTypeId: previousUpsTypeId = '' } = previous; const [inputUpsTypeIdValue, setInputUpsTypeIdValue] = useState(previousUpsTypeId); const upsTypeOptions = useMemo( () => upsTemplate ? Object.entries(upsTemplate).map( ([upsTypeId, { brand, description }]) => ({ displayValue: ( {brand} {description} ), value: upsTypeId, }), ) : [], [upsTemplate], ); const pickUpsTypeElement = useMemo( () => upsTemplate && ( { const newValue = String(rawNewValue); setInputUpsTypeIdValue(newValue); }} selectItems={upsTypeOptions} selectProps={{ onClearIndicatorClick: () => { setInputUpsTypeIdValue(''); }, renderValue: (rawValue) => { const upsTypeId = String(rawValue); const { brand } = upsTemplate[upsTypeId]; return brand; }, }} value={inputUpsTypeIdValue} /> ), [inputUpsTypeIdValue, upsTypeOptions, upsTemplate], ); const content = useMemo( () => isExternalLoading ? ( ) : ( {pickUpsTypeElement} {inputUpsTypeIdValue && } ), [inputUpsTypeIdValue, isExternalLoading, pickUpsTypeElement, previous], ); return content; }; export { INPUT_ID_UPS_TYPE_ID }; export default AddUpsInputGroup;