aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/apps/configurationApp/src/components/uiElementLeafList.tsx
blob: 5937ed7b3aa15c3e661f3b158cbcfc31f07d6e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt odlux
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 */

import * as React from "react"
import { FormControl, InputLabel, Paper, Chip, FormHelperText, Dialog, DialogTitle, DialogContentText, DialogActions, Button, DialogContent } from "@mui/material";
import makeStyles from '@mui/styles/makeStyles';
import AddIcon from '@mui/icons-material/Add';

import { Theme } from '@mui/material/styles';
import { ViewElement } from "../models/uiModels";

import { BaseProps } from "./baseProps";

type LeafListProps = BaseProps<any []> & {
  getEditorForViewElement:  (uiElement: ViewElement) => (null | React.ComponentType<BaseProps<any>>)  
};

const useStyles = makeStyles((theme: Theme) => {
  const light = theme.palette.mode === 'light';
  const bottomLineColor = light ? 'rgba(0, 0, 0, 0.42)' : 'rgba(255, 255, 255, 0.7)';

  return ({
    root: {
      display: 'flex',
      justifyContent: 'left',
      verticalAlign: 'bottom',
      flexWrap: 'wrap',
      listStyle: 'none',
      margin: 0,
      padding: 0,
      paddingTop: theme.spacing(0.5),
      marginTop: theme.spacing(1),
    },
    chip: {
      margin: theme.spacing(0.5),
    },
    underline: {
        '&:after': {
          borderBottom: `2px solid ${theme.palette.primary.main}`,
          left: 0,
          bottom: 0,
          // Doing the other way around crash on IE 11 "''" https://github.com/cssinjs/jss/issues/242
          content: '""',
          position: 'absolute',
          right: 0,
          transform: 'scaleX(0)',
          transition: theme.transitions.create('transform', {
            duration: theme.transitions.duration.shorter,
            easing: theme.transitions.easing.easeOut,
          }),
          pointerEvents: 'none', // Transparent to the hover style.
        },
        '&.Mui-focused:after': {
          transform: 'scaleX(1)',
        },
        '&.Mui-error:after': {
          borderBottomColor: theme.palette.error.main,
          transform: 'scaleX(1)', // error is always underlined in red
        },
        '&:before': {
          borderBottom: `1px solid ${bottomLineColor}`,
          left: 0,
          bottom: 0,
          // Doing the other way around crash on IE 11 "''" https://github.com/cssinjs/jss/issues/242
          content: '"\\00a0"',
          position: 'absolute',
          right: 0,
          transition: theme.transitions.create('border-bottom-color', {
            duration: theme.transitions.duration.shorter,
          }),
          pointerEvents: 'none', // Transparent to the hover style.
        },
        '&:hover:not($disabled):before': {
          borderBottom: `2px solid ${theme.palette.text.primary}`,
          // Reset on touch devices, it doesn't add specificity
          '@media (hover: none)': {
            borderBottom: `1px solid ${bottomLineColor}`,
          },
        },
        '&.Mui-disabled:before': {
          borderBottomStyle: 'dotted',
        },
      },
  })
});

export const UiElementLeafList = (props: LeafListProps) => {
  const { value: element, inputValue, onChange } = props;

  const classes = useStyles();

  const [open, setOpen] = React.useState(false);
  const [editorValue, setEditorValue] = React.useState("");
  const [editorValueIndex, setEditorValueIndex] = React.useState(-1);
  

  const handleClickOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  const onApplyButton = () => { 
     if (editorValue != null && editorValue != "" && editorValueIndex < 0) {
       props.onChange([
         ...inputValue,
         editorValue,
       ]);
     } else if (editorValue != null && editorValue != "") {
       props.onChange([
         ...inputValue.slice(0, editorValueIndex),
         editorValue,
         ...inputValue.slice(editorValueIndex+1),
       ]);
     }
     setOpen(false);
  };

  const onDelete = (index : number) => {
    const newValue : any[] = [
      ...inputValue.slice(0, index),
      ...inputValue.slice(index+1),
    ];
    onChange(newValue);
  };

  const ValueEditor = props.getEditorForViewElement(props.value); 

  return (
    <>
      <FormControl variant="standard" style={{ width: 485, marginLeft: 20, marginRight: 20 }}>
        <InputLabel htmlFor={`list-${element.id}`} shrink={!props.readOnly || !!(inputValue && inputValue.length)} >{element.label}</InputLabel>
        <ul className={`${classes.root} ${classes.underline}`} id={`list-${element.id}`}>
        { !props.readOnly ? <li>
          <Chip
            icon={<AddIcon />}
            label={"Add"}
            className={classes.chip}
            size="small"
            color="secondary"
            onClick={ () => { 
              setOpen(true); 
              setEditorValue("");
              setEditorValueIndex(-1);
             } 
            }
          />
        </li> : null }  
        { inputValue.map((val, ind) => (
          <li key={ind}>
            <Chip
              className={classes.chip}
              size="small"
              variant="outlined"
              label={String(val)}
              onDelete={ !props.readOnly ? () => { onDelete(ind); } : undefined }  
              onClick={ !props.readOnly ? () => { 
                  setOpen(true); 
                  setEditorValue(val);
                  setEditorValueIndex(ind);
                  } : undefined
              }   
            />
            </li>
          ))
        }
        </ul>
        {/* <FormHelperText>{ "Value is mandetory"}</FormHelperText> */}
        </FormControl>
        <Dialog open={open} onClose={handleClose} aria-labelledby="form-dialog-title">
          <DialogTitle id="form-dialog-title">{editorValueIndex < 0 ? "Add new value" : "Edit value" } </DialogTitle>
          <DialogContent>
            { ValueEditor && <ValueEditor 
                inputValue={ editorValue }
                value={{ ...element, isList: false}}
                disabled={false}
                readOnly={props.readOnly}
                onChange={ setEditorValue }
            /> || null }
          </DialogContent>
          <DialogActions>
            <Button color="inherit" onClick={ handleClose }> Cancel </Button>
            <Button disabled={editorValue == null || editorValue === "" } onClick={ onApplyButton } color="secondary"> {editorValueIndex < 0 ? "Add" : "Apply"} </Button>
          </DialogActions>
        </Dialog>
      </>
  );
};