import { Box } from '@mui/material'; import { FC, useMemo, useState } from 'react'; import Autocomplete from '../Autocomplete'; import CommonFenceInputGroup from './CommonFenceInputGroup'; import FlexBox from '../FlexBox'; import Spinner from '../Spinner'; import { BodyText } from '../Text'; const AddFenceInputGroup: FC = ({ fenceTemplate: externalFenceTemplate, loading: isExternalLoading, }) => { const [fenceTypeValue, setInputFenceTypeValue] = useState(null); const fenceTypeOptions = useMemo( () => externalFenceTemplate ? Object.entries(externalFenceTemplate) .sort(([a], [b]) => (a > b ? 1 : -1)) .map(([id, { description: rawDescription }]) => { const description = typeof rawDescription === 'string' ? rawDescription : 'No description.'; return { fenceDescription: description, fenceId: id, label: id, }; }) : [], [externalFenceTemplate], ); const fenceTypeElement = useMemo( () => ( option.fenceId === value.fenceId } label="Fence device type" onChange={(event, newFenceType) => { setInputFenceTypeValue(newFenceType); }} openOnFocus options={fenceTypeOptions} renderOption={(props, { fenceDescription, fenceId }, { selected }) => ( *': { width: '100%', }, }} {...props} > {fenceId} {fenceDescription} )} sx={{ marginTop: '.3em' }} value={fenceTypeValue} /> ), [fenceTypeOptions, fenceTypeValue], ); const fenceParameterElements = useMemo( () => ( ), [externalFenceTemplate, fenceTypeValue], ); const content = useMemo( () => isExternalLoading ? ( ) : ( {fenceTypeElement} {fenceParameterElements} ), [fenceTypeElement, fenceParameterElements, isExternalLoading], ); return <>{content}; }; export default AddFenceInputGroup;