• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 963002358

pending completion
963002358

Pull #3870

gitlab-ci

mzedel
chore: cleaned up left over onboarding tooltips & aligned with updated design

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3870: MEN-5413

4348 of 6319 branches covered (68.81%)

95 of 122 new or added lines in 24 files covered. (77.87%)

1734 existing lines in 160 files now uncovered.

8174 of 9951 relevant lines covered (82.14%)

178.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.0
/src/js/components/common/forms/keyvalueeditor.js
1
// Copyright 2021 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { createRef, useEffect, useRef, useState } from 'react';
15

16
import { Clear as ClearIcon, Add as ContentAddIcon } from '@mui/icons-material';
17
import { Fab, FormControl, FormHelperText, IconButton, Input } from '@mui/material';
18
import { useTheme } from '@mui/material/styles';
19

20
const emptyInput = { helptip: null, key: '', value: '' };
18✔
21

22
export const KeyValueEditor = ({ disabled, errortext, initialInput = {}, inputHelpTipsMap = {}, onInputChange, reset, showHelptips }) => {
18✔
23
  const theme = useTheme();
181✔
24
  const [inputs, setInputs] = useState([{ ...emptyInput }]);
181✔
25
  const [error, setError] = useState('');
181✔
26
  // need to useRef here to get positioning of
27
  let inputRefs = useRef([]);
181✔
28

29
  useEffect(() => {
181✔
30
    const newInputs = Object.keys(initialInput).length
13!
UNCOV
31
      ? Object.entries(initialInput).map(([key, value]) => ({ helptip: inputHelpTipsMap[key.toLowerCase()], key, ref: createRef(), value }))
×
32
      : [{ ...emptyInput, ref: createRef() }];
33
    inputRefs.current = newInputs.map((_, i) => inputRefs.current[i] ?? createRef());
13✔
34
    setInputs(newInputs);
13✔
35
    // eslint-disable-next-line react-hooks/exhaustive-deps
36
  }, [JSON.stringify(initialInput), JSON.stringify(inputHelpTipsMap), reset]);
37

38
  const onClearClick = () => {
181✔
UNCOV
39
    const changedInputs = [{ ...emptyInput }];
×
UNCOV
40
    setInputs(changedInputs);
×
UNCOV
41
    const inputObject = reducePairs(changedInputs);
×
UNCOV
42
    onInputChange(inputObject);
×
43
  };
44

45
  const updateInputs = (key, index, event) => {
181✔
46
    let changedInputs = [...inputs];
142✔
47
    const {
48
      target: { value }
49
    } = event;
142✔
50
    changedInputs[index][key] = value;
142✔
51
    changedInputs[index].helptip = null;
142✔
52
    const normalizedKey = changedInputs[index].key.toLowerCase();
142✔
53
    if (inputHelpTipsMap[normalizedKey]) {
142✔
54
      changedInputs[index].helptip = inputHelpTipsMap[normalizedKey];
1✔
55
    }
56
    setInputs(changedInputs);
142✔
57
    const inputObject = reducePairs(changedInputs);
142✔
58
    if (changedInputs.every(item => item.key && item.value) && changedInputs.length !== Object.keys(inputObject).length) {
170✔
59
      setError('Duplicate keys exist, only the last set value will be submitted');
10✔
60
    } else {
61
      setError('');
132✔
62
    }
63
    onInputChange(inputObject);
142✔
64
  };
65

66
  const reducePairs = listOfPairs => listOfPairs.reduce((accu, item) => ({ ...accu, ...(item.value ? { [item.key]: item.value } : {}) }), {});
181✔
67

68
  const addKeyValue = () => {
181✔
69
    const changedInputs = [...inputs, { ...emptyInput, ref: createRef() }];
3✔
70
    inputRefs.current = changedInputs.map((_, i) => inputRefs.current[i] ?? createRef());
6✔
71
    setInputs(changedInputs);
3✔
72
    setError('');
3✔
73
  };
74

75
  const removeInput = index => {
181✔
UNCOV
76
    let changedInputs = [...inputs];
×
UNCOV
77
    changedInputs.splice(index, 1);
×
UNCOV
78
    inputRefs.current = changedInputs.map((_, i) => inputRefs.current[i] ?? createRef());
×
UNCOV
79
    setInputs(changedInputs);
×
UNCOV
80
    const inputObject = reducePairs(changedInputs);
×
UNCOV
81
    onInputChange(inputObject);
×
UNCOV
82
    setError('');
×
83
  };
84

85
  const getHelptipPosition = ref => {
181✔
86
    const { offsetHeight, offsetLeft, offsetTop } = ref.current.closest('.key-value-container');
1✔
87
    const top = offsetTop + offsetHeight / 3;
1✔
88
    const left = offsetLeft - 15;
1✔
89
    return { left, top };
1✔
90
  };
91

92
  return (
181✔
93
    <div>
94
      {inputs.map((input, index) => {
95
        const hasError = Boolean(index === inputs.length - 1 && (errortext || error));
213✔
96
        const hasRemovalDisabled = !(inputs[index].key && inputs[index].value);
213✔
97
        const Helptip = inputs[index].helptip?.component;
213✔
98
        const ref = inputRefs.current[index];
213✔
99
        return (
213✔
100
          <div className="key-value-container relative" key={index}>
101
            <FormControl>
102
              <Input disabled={disabled} value={input.key} placeholder="Key" inputRef={ref} onChange={e => updateInputs('key', index, e)} type="text" />
57✔
103
              {hasError && <FormHelperText>{errortext || error}</FormHelperText>}
281✔
104
            </FormControl>
105
            <FormControl>
106
              <Input disabled={disabled} value={`${input.value}`} placeholder="Value" onChange={e => updateInputs('value', index, e)} type="text" />
85✔
107
            </FormControl>
108
            {inputs.length > 1 && !hasRemovalDisabled ? (
490✔
UNCOV
109
              <IconButton disabled={disabled} onClick={() => removeInput(index)} size="large">
×
110
                <ClearIcon fontSize="small" />
111
              </IconButton>
112
            ) : (
113
              <span />
114
            )}
115
            {showHelptips && Helptip && ref.current && <Helptip anchor={getHelptipPosition(ref)} {...inputs[index].helptip.props} />}
231✔
116
          </div>
117
        );
118
      })}
119
      <div className="key-value-container">
120
        <div style={{ minWidth: theme.spacing(30) }}>
121
          <Fab
122
            disabled={disabled || !inputs[inputs.length - 1].key || !inputs[inputs.length - 1].value}
493✔
123
            style={{ marginBottom: 10 }}
124
            color="secondary"
125
            size="small"
126
            onClick={addKeyValue}
127
          >
128
            <ContentAddIcon />
129
          </Fab>
130
        </div>
131
        <div style={{ minWidth: theme.spacing(30) }} />
132
        {inputs.length > 1 ? (
181✔
133
          <a className="margin-left-small" onClick={onClearClick}>
134
            clear all
135
          </a>
136
        ) : (
137
          <div />
138
        )}
139
      </div>
140
    </div>
141
  );
142
};
143

144
export default KeyValueEditor;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc