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

mendersoftware / gui / 944676341

pending completion
944676341

Pull #3875

gitlab-ci

mzedel
chore: aligned snapshots with updated design

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

4469 of 6446 branches covered (69.33%)

230 of 266 new or added lines in 43 files covered. (86.47%)

1712 existing lines in 161 files now uncovered.

8406 of 10170 relevant lines covered (82.65%)

196.7 hits per line

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

86.9
/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, 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: '' };
16✔
21

22
export const KeyValueEditor = ({ disabled, errortext, input = {}, inputHelpTipsMap = {}, onInputChange, reset }) => {
16✔
23
  const theme = useTheme();
196✔
24
  const [inputs, setInputs] = useState([{ ...emptyInput }]);
196✔
25
  const [error, setError] = useState('');
196✔
26

27
  useEffect(() => {
196✔
28
    const newInputs = Object.keys(input).length
19✔
29
      ? Object.entries(input).map(([key, value]) => ({ helptip: inputHelpTipsMap[key.toLowerCase()], key, ref: createRef(), value }))
9✔
30
      : [{ ...emptyInput, ref: createRef() }];
31
    setInputs(newInputs);
19✔
32
  }, [reset]);
33

34
  const onClearClick = () => {
196✔
UNCOV
35
    const changedInputs = [{ ...emptyInput }];
×
UNCOV
36
    setInputs(changedInputs);
×
UNCOV
37
    const inputObject = reducePairs(changedInputs);
×
UNCOV
38
    onInputChange(inputObject);
×
39
  };
40

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

62
  const reducePairs = listOfPairs => listOfPairs.reduce((accu, item) => ({ ...accu, ...(item.value ? { [item.key]: item.value } : {}) }), {});
208✔
63

64
  const addKeyValue = () => {
196✔
65
    const changedInputs = [...inputs, { ...emptyInput, ref: createRef() }];
3✔
66
    setInputs(changedInputs);
3✔
67
    setError('');
3✔
68
  };
69

70
  const removeInput = index => {
196✔
UNCOV
71
    let changedInputs = [...inputs];
×
UNCOV
72
    changedInputs.splice(index, 1);
×
UNCOV
73
    setInputs(changedInputs);
×
UNCOV
74
    const inputObject = reducePairs(changedInputs);
×
UNCOV
75
    onInputChange(inputObject);
×
UNCOV
76
    setError('');
×
77
  };
78

79
  return (
196✔
80
    <div>
81
      {inputs.map((input, index) => {
82
        const hasError = Boolean(index === inputs.length - 1 && (errortext || error));
264✔
83
        const hasRemovalDisabled = !(inputs[index].key && inputs[index].value);
264✔
84
        const Helptip = inputs[index].helptip?.component;
264✔
85
        return (
264✔
86
          <div className="key-value-container relative" key={index}>
87
            <FormControl>
88
              <Input value={input.key} placeholder="Key" onChange={e => updateInputs('key', index, e)} type="text" />
64✔
89
              {hasError && <FormHelperText>{errortext || error}</FormHelperText>}
332✔
90
            </FormControl>
91
            <FormControl>
92
              <Input value={`${input.value}`} placeholder="Value" onChange={e => updateInputs('value', index, e)} type="text" />
84✔
93
            </FormControl>
94
            {inputs.length > 1 && !hasRemovalDisabled ? (
646✔
UNCOV
95
              <IconButton disabled={disabled || hasRemovalDisabled} onClick={() => removeInput(index)} size="large">
✔
96
                <ClearIcon fontSize="small" />
97
              </IconButton>
98
            ) : (
99
              <span />
100
            )}
101
            {Helptip && <Helptip anchor={{ left: -35, top: 5 }} {...inputs[index].helptip.props} />}
265✔
102
          </div>
103
        );
104
      })}
105
      <div className="key-value-container">
106
        <div style={{ minWidth: theme.spacing(30) }}>
107
          <Fab
108
            disabled={disabled || !inputs[inputs.length - 1].key || !inputs[inputs.length - 1].value}
532✔
109
            style={{ marginBottom: 10 }}
110
            color="secondary"
111
            size="small"
112
            onClick={addKeyValue}
113
          >
114
            <ContentAddIcon />
115
          </Fab>
116
        </div>
117
        <div style={{ minWidth: theme.spacing(30) }} />
118
        {inputs.length > 1 ? (
196✔
119
          <a className="margin-left-small" onClick={onClearClick}>
120
            clear all
121
          </a>
122
        ) : (
123
          <div />
124
        )}
125
      </div>
126
    </div>
127
  );
128
};
129

130
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