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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
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';
26 type UiElementUnionProps = { isKey: boolean } & BaseProps;
28 export const UIElementUnion = (props: UiElementUnionProps) => {
30 const [isError, setError] = React.useState(false);
31 const [helperText, setHelperText] = React.useState("");
32 const [isTooltipVisible, setTooltipVisibility] = React.useState(true);
34 const element = props.value as ViewElementUnion;
36 const verifyValues = (data: string) => {
38 let foundObjectElements = 0;
39 let errorMessage = "";
40 let isPatternCorrect = null;
42 for (let i = 0; i < element.elements.length; i++) {
43 const unionElement = element.elements[i];
45 if (isViewElementNumber(unionElement)) {
47 errorMessage = checkRange(unionElement, Number(data));
49 } else if (isViewElementString(unionElement)) {
50 errorMessage += checkRange(unionElement, data.length);
51 isPatternCorrect = checkPattern(unionElement.pattern, data).isValid;
54 } else if (isViewElementObject(unionElement)) {
55 foundObjectElements++;
58 if (isPatternCorrect || errorMessage.length === 0) {
63 if (errorMessage.length > 0 || isPatternCorrect !== null && !isPatternCorrect) {
65 setHelperText("Input is wrong.");
71 if (foundObjectElements > 0 && foundObjectElements != element.elements.length) {
72 throw new Error(`The union element ${element.id} can't be changed.`);
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) }}
85 style={{ width: 485, marginLeft: 20, marginRight: 20 }}
86 readOnly={props.readOnly}
87 disabled={props.disabled}
88 helperText={helperText}