Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / configurationApp / src / components / uiElementString.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 { Tooltip, TextField } from "@mui/material";
21 import { ViewElementString } from "../models/uiModels";
22 import { BaseProps } from "./baseProps";
23 import { IfWhenTextInput } from "./ifWhenTextInput";
24 import { checkRange, checkPattern } from "../utilities/verifyer";
25
26 type stringEntryProps = BaseProps ;
27
28 export const UiElementString = (props: stringEntryProps) => {
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 ViewElementString;
35
36     const verifyValues = (data: string) => {
37
38         if (data.trim().length > 0) {
39
40             let errorMessage = "";
41             const result = checkRange(element, data.length);
42
43             if (result.length > 0) {
44                 errorMessage += result;
45             }
46
47             const patternResult = checkPattern(element.pattern, data)
48
49             if (patternResult.error) {
50                 errorMessage += patternResult.error;
51             }
52
53             if (errorMessage.length > 0) {
54                 setError(true);
55                 setHelperText(errorMessage);
56             } else {
57                 setError(false);
58                 setHelperText("");
59             }
60         } else {
61             setError(false);
62             setHelperText("");
63         }
64
65
66         props.onChange(data);
67
68     }
69
70     return (
71         <Tooltip disableInteractive title={isTooltipVisible ? element.description || '' : ''}>
72             <IfWhenTextInput element={element} onChangeTooltipVisibility={setTooltipVisibility}
73                 spellCheck={false} autoFocus margin="dense"
74                 id={element.id} label={props?.isKey ? "🔑 " + element.label : element.label} type="text" value={props.inputValue}
75                 style={{ width: 485, marginLeft: 20, marginRight: 20 }}
76                 onChange={(e: any) => { verifyValues(e.target.value) }}
77                 error={isError}
78                 readOnly={props.readOnly}
79                 disabled={props.disabled}
80                 helperText={helperText}
81             />
82         </Tooltip>
83     );
84 }