You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.9 KiB
109 lines
2.9 KiB
2 years ago
|
import { Box } from '@mui/material';
|
||
|
import { FC, useMemo, useState } from 'react';
|
||
2 years ago
|
|
||
2 years ago
|
import Autocomplete from '../Autocomplete';
|
||
2 years ago
|
import CommonFenceInputGroup from './CommonFenceInputGroup';
|
||
2 years ago
|
import FlexBox from '../FlexBox';
|
||
|
import Spinner from '../Spinner';
|
||
|
import { BodyText } from '../Text';
|
||
2 years ago
|
|
||
2 years ago
|
const AddFenceInputGroup: FC<AddFenceInputGroupProps> = ({
|
||
|
fenceTemplate: externalFenceTemplate,
|
||
|
loading: isExternalLoading,
|
||
|
}) => {
|
||
2 years ago
|
const [fenceTypeValue, setInputFenceTypeValue] =
|
||
|
useState<FenceAutocompleteOption | null>(null);
|
||
2 years ago
|
|
||
2 years ago
|
const fenceTypeOptions = useMemo<FenceAutocompleteOption[]>(
|
||
2 years ago
|
() =>
|
||
2 years ago
|
externalFenceTemplate
|
||
|
? Object.entries(externalFenceTemplate)
|
||
|
.sort(([a], [b]) => (a > b ? 1 : -1))
|
||
|
.map(([id, { description: rawDescription }]) => {
|
||
2 years ago
|
const description =
|
||
|
typeof rawDescription === 'string'
|
||
|
? rawDescription
|
||
|
: 'No description.';
|
||
|
|
||
|
return {
|
||
2 years ago
|
fenceDescription: description,
|
||
|
fenceId: id,
|
||
2 years ago
|
label: id,
|
||
|
};
|
||
2 years ago
|
})
|
||
2 years ago
|
: [],
|
||
2 years ago
|
[externalFenceTemplate],
|
||
2 years ago
|
);
|
||
|
|
||
2 years ago
|
const fenceTypeElement = useMemo(
|
||
2 years ago
|
() => (
|
||
|
<Autocomplete
|
||
2 years ago
|
id="add-fence-select-type"
|
||
2 years ago
|
isOptionEqualToValue={(option, value) =>
|
||
2 years ago
|
option.fenceId === value.fenceId
|
||
2 years ago
|
}
|
||
|
label="Fence device type"
|
||
2 years ago
|
onChange={(event, newFenceType) => {
|
||
|
setInputFenceTypeValue(newFenceType);
|
||
2 years ago
|
}}
|
||
|
openOnFocus
|
||
2 years ago
|
options={fenceTypeOptions}
|
||
|
renderOption={(props, { fenceDescription, fenceId }, { selected }) => (
|
||
2 years ago
|
<Box
|
||
|
component="li"
|
||
|
sx={{
|
||
|
display: 'flex',
|
||
|
flexDirection: 'column',
|
||
|
|
||
|
'& > *': {
|
||
|
width: '100%',
|
||
|
},
|
||
|
}}
|
||
|
{...props}
|
||
|
>
|
||
|
<BodyText
|
||
|
inverted
|
||
|
sx={{
|
||
|
fontSize: '1.2em',
|
||
|
fontWeight: selected ? 400 : undefined,
|
||
|
}}
|
||
|
>
|
||
2 years ago
|
{fenceId}
|
||
2 years ago
|
</BodyText>
|
||
2 years ago
|
<BodyText selected={false}>{fenceDescription}</BodyText>
|
||
2 years ago
|
</Box>
|
||
|
)}
|
||
2 years ago
|
sx={{ marginTop: '.3em' }}
|
||
2 years ago
|
value={fenceTypeValue}
|
||
2 years ago
|
/>
|
||
|
),
|
||
2 years ago
|
[fenceTypeOptions, fenceTypeValue],
|
||
2 years ago
|
);
|
||
2 years ago
|
const fenceParameterElements = useMemo(
|
||
|
() => (
|
||
2 years ago
|
<CommonFenceInputGroup
|
||
|
fenceId={fenceTypeValue?.fenceId}
|
||
2 years ago
|
fenceTemplate={externalFenceTemplate}
|
||
2 years ago
|
/>
|
||
|
),
|
||
2 years ago
|
[externalFenceTemplate, fenceTypeValue],
|
||
2 years ago
|
);
|
||
2 years ago
|
|
||
2 years ago
|
const content = useMemo(
|
||
2 years ago
|
() =>
|
||
2 years ago
|
isExternalLoading ? (
|
||
|
<Spinner />
|
||
2 years ago
|
) : (
|
||
2 years ago
|
<FlexBox>
|
||
2 years ago
|
{fenceTypeElement}
|
||
2 years ago
|
{fenceParameterElements}
|
||
|
</FlexBox>
|
||
2 years ago
|
),
|
||
2 years ago
|
[fenceTypeElement, fenceParameterElements, isExternalLoading],
|
||
2 years ago
|
);
|
||
|
|
||
2 years ago
|
return <>{content}</>;
|
||
2 years ago
|
};
|
||
|
|
||
2 years ago
|
export default AddFenceInputGroup;
|