Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / configurationApp / src / components / uiElementUnion.tsx
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 import * as React from 'react'
20 import { BaseProps } from './baseProps';
21 import { Tooltip } from '@mui/material';
22 import { IfWhenTextInput } from './ifWhenTextInput';
23 import { ViewElementUnion, isViewElementString, isViewElementNumber, isViewElementObject, ViewElementNumber } from '../models/uiModels';
24 import { checkRange, checkPattern } from '../utilities/verifyer';
25
26 type UiElementUnionProps = { isKey: boolean } & BaseProps;
27
28 export const UIElementUnion = (props: UiElementUnionProps) => {
29
30   const [isError, setError] = React.useState(false);
31   const [helperText, setHelperText] = React.useState("");
32   const [isTooltipVisible, setTooltipVisibility] = React.useState(true);
33
34   const element = props.value as ViewElementUnion;
35
36   const verifyValues = (data: string) => {
37
38     let foundObjectElements = 0;
39     let errorMessage = "";
40     let isPatternCorrect = null;
41
42     for (let i = 0; i < element.elements.length; i++) {
43       const unionElement = element.elements[i];
44
45       if (isViewElementNumber(unionElement)) {
46
47         errorMessage = checkRange(unionElement, Number(data));
48
49       } else if (isViewElementString(unionElement)) {
50         errorMessage += checkRange(unionElement, data.length);
51         isPatternCorrect = checkPattern(unionElement.pattern, data).isValid;
52
53
54       } else if (isViewElementObject(unionElement)) {
55         foundObjectElements++;
56       }
57
58       if (isPatternCorrect || errorMessage.length === 0) {
59         break;
60       }
61     }
62
63     if (errorMessage.length > 0 || isPatternCorrect !== null && !isPatternCorrect) {
64       setError(true);
65       setHelperText("Input is wrong.");
66     } else {
67       setError(false);
68       setHelperText("");
69     }
70
71     if (foundObjectElements > 0 && foundObjectElements != element.elements.length) {
72       throw new Error(`The union element ${element.id} can't be changed.`);
73
74     } else {
75       props.onChange(data);
76     }
77   };
78
79   return <Tooltip disableInteractive title={isTooltipVisible ? element.description || '' : ''}>
80     <IfWhenTextInput element={element} onChangeTooltipVisibility={setTooltipVisibility}
81       spellCheck={false} autoFocus margin="dense"
82       id={element.id} label={props.isKey ? "🔑 " + element.label : element.label} type="text" value={props.inputValue}
83       onChange={(e: any) => { verifyValues(e.target.value) }}
84       error={isError}
85       style={{ width: 485, marginLeft: 20, marginRight: 20 }}
86       readOnly={props.readOnly}
87       disabled={props.disabled}
88       helperText={helperText}
89     />
90   </Tooltip>;
91 }