Merge "Update ODLUX"
[ccsdk/features.git] / sdnr / wt / odlux / apps / configurationApp / src / components / uiElementNumber.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 { ViewElementNumber } from "models/uiModels";
20 import { Tooltip, InputAdornment } from "@mui/material";
21 import * as React from 'react';
22 import { BaseProps } from "./baseProps";
23 import { IfWhenTextInput } from "./ifWhenTextInput";
24 import { checkRange } from "./verifyer";
25
26 type numberInputProps = BaseProps<number>;
27
28 export const UiElementNumber = (props: numberInputProps) => {
29
30
31   const [error, setError] = React.useState(false);
32   const [helperText, setHelperText] = React.useState("");
33   const [isTooltipVisible, setTooltipVisibility] = React.useState(true);
34
35   const element = props.value as ViewElementNumber;
36
37   const verifyValue = (data: string) => {
38     const num = Number(data);
39     if (!isNaN(num)) {
40       const result = checkRange(element, num);
41       if (result.length > 0) {
42         setError(true);
43         setHelperText(result);
44       } else {
45         setError(false);
46         setHelperText("");
47       }
48     } else {
49       setError(true);
50       setHelperText("Input is not a number.");
51     }
52     props.onChange(num);
53   }
54
55   return (
56     <Tooltip disableInteractive title={isTooltipVisible ? element.description || '' : ''}>
57       <IfWhenTextInput element={element} onChangeTooltipVisuability={setTooltipVisibility}
58         spellCheck={false} autoFocus margin="dense"
59         id={element.id} label={element.label} type="text" value={props.inputValue}
60         style={{ width: 485, marginLeft: 20, marginRight: 20 }}
61         onChange={(e) => { verifyValue(e.target.value) }}
62         error={error}
63         readOnly={props.readOnly}
64         disabled={props.disabled}
65         helperText={helperText}
66         startAdornment={element.units != null ? <InputAdornment position="start">{element.units}</InputAdornment> : undefined}
67       />
68     </Tooltip>
69   );
70 }