🐛 fix(keypairListComponent): fix bug where changing key in input field would overwrite existing keys

✨ feat(keypairListComponent): add logic to append a counter to new key if it already exists in the list to ensure uniqueness
This commit is contained in:
Cristhian Zanforlin Lousa 2023-08-30 22:10:50 -03:00
commit 646fd7f1e5

View file

@ -23,11 +23,16 @@ export default function KeypairListComponent({
const handleChangeKey = (event, idx) => { const handleChangeKey = (event, idx) => {
const newInputList = _.cloneDeep(value); const newInputList = _.cloneDeep(value);
const oldKey = Object.keys(newInputList[idx])[0]; const oldKey = Object.keys(newInputList[idx])[0];
const updatedObj = { [event.target.value]: newInputList[idx][oldKey] }; let counter = 1;
newInputList[idx] = updatedObj; let newKey = event.target.value;
while (newInputList.some(obj => Object.keys(obj)[0] === newKey)) {
newKey = `${event.target.value}_${counter}`;
counter++;
}
newInputList[idx] = { [newKey]: newInputList[idx][oldKey] };
onChange(newInputList); onChange(newInputList);
}; };
const handleChangeValue = (newValue, idx) => { const handleChangeValue = (newValue, idx) => {
const newInputList = _.cloneDeep(value); const newInputList = _.cloneDeep(value);
const key = Object.keys(newInputList[idx])[0]; const key = Object.keys(newInputList[idx])[0];