From 98dd977fabd06a6ef650c3f2d8894fe428dee541 Mon Sep 17 00:00:00 2001
From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com>
Date: Thu, 27 Feb 2025 15:23:40 +0100
Subject: [PATCH] Alerting: Fix KeyValueMap input bug (#101367)
* fix KeyValueMap input bug
* add translations
---
.betterer.results | 6 +-
.../form/fields/KeyValueMapInput.tsx | 77 +++++++++++++------
public/locales/en-US/grafana.json | 4 +
public/locales/pseudo-LOCALE/grafana.json | 4 +
4 files changed, 62 insertions(+), 29 deletions(-)
diff --git a/.betterer.results b/.betterer.results
index 5e04d7cf375..f6c07dd6633 100644
--- a/.betterer.results
+++ b/.betterer.results
@@ -1982,10 +1982,8 @@ exports[`better eslint`] = {
[0, 0, 0, "No untranslated strings. Wrap text with ", "6"]
],
"public/app/features/alerting/unified/components/receivers/form/fields/KeyValueMapInput.tsx:5381": [
- [0, 0, 0, "No untranslated strings in text props. Wrap text with or use t()", "0"],
- [0, 0, 0, "No untranslated strings. Wrap text with ", "1"],
- [0, 0, 0, "No untranslated strings. Wrap text with ", "2"],
- [0, 0, 0, "No untranslated strings. Wrap text with ", "3"]
+ [0, 0, 0, "No untranslated strings. Wrap text with ", "0"],
+ [0, 0, 0, "No untranslated strings. Wrap text with ", "1"]
],
"public/app/features/alerting/unified/components/receivers/form/fields/OptionField.tsx:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
diff --git a/public/app/features/alerting/unified/components/receivers/form/fields/KeyValueMapInput.tsx b/public/app/features/alerting/unified/components/receivers/form/fields/KeyValueMapInput.tsx
index 5a1bccdd34e..b69a9dce091 100644
--- a/public/app/features/alerting/unified/components/receivers/form/fields/KeyValueMapInput.tsx
+++ b/public/app/features/alerting/unified/components/receivers/form/fields/KeyValueMapInput.tsx
@@ -1,8 +1,9 @@
import { css } from '@emotion/css';
-import { useEffect, useState } from 'react';
+import { useState } from 'react';
import { GrafanaTheme2 } from '@grafana/data';
-import { Button, Input, useStyles2 } from '@grafana/ui';
+import { Button, Input, Stack, useStyles2 } from '@grafana/ui';
+import { Trans, t } from 'app/core/internationalization';
import { ActionIcon } from '../../../rules/ActionIcon';
@@ -15,7 +16,7 @@ interface Props {
export const KeyValueMapInput = ({ value, onChange, readOnly = false }: Props) => {
const styles = useStyles2(getStyles);
const [pairs, setPairs] = useState(recordToPairs(value));
- useEffect(() => setPairs(recordToPairs(value)), [value]);
+ const [currentNewPair, setCurrentNewPair] = useState<[string, string] | undefined>(undefined);
const emitChange = (pairs: Array<[string, string]>) => {
onChange(pairsToRecord(pairs));
@@ -30,15 +31,6 @@ export const KeyValueMapInput = ({ value, onChange, readOnly = false }: Props) =
}
};
- const updatePair = (values: [string, string], index: number) => {
- const old = pairs[index];
- const newPairs = pairs.map((pair, i) => (i === index ? values : pair));
- setPairs(newPairs);
- if (values[0] || old[0]) {
- emitChange(newPairs);
- }
- };
-
return (
{!!pairs.length && (
@@ -54,22 +46,18 @@ export const KeyValueMapInput = ({ value, onChange, readOnly = false }: Props) =
{pairs.map(([key, value], index) => (
|
- updatePair([e.currentTarget.value, value], index)}
- />
+
|
- updatePair([key, e.currentTarget.value], index)}
- />
+
|
{!readOnly && (
- deleteItem(index)} />
+ deleteItem(index)}
+ />
|
)}
@@ -77,6 +65,44 @@ export const KeyValueMapInput = ({ value, onChange, readOnly = false }: Props) =
)}
+ {currentNewPair && (
+
+ )}
{!readOnly && (
)}
diff --git a/public/locales/en-US/grafana.json b/public/locales/en-US/grafana.json
index e014860e85c..a064ce993a6 100644
--- a/public/locales/en-US/grafana.json
+++ b/public/locales/en-US/grafana.json
@@ -315,6 +315,10 @@
"empty-state": {
"title": "You don't have any contact points yet"
},
+ "key-value-map": {
+ "add": "Add",
+ "confirm-add": "Confirm to add"
+ },
"last-delivery-attempt": "Last delivery attempt",
"last-delivery-failed": "Last delivery attempt failed",
"no-contact-points-found": "No contact points found",
diff --git a/public/locales/pseudo-LOCALE/grafana.json b/public/locales/pseudo-LOCALE/grafana.json
index cb3c713ad28..317a107f796 100644
--- a/public/locales/pseudo-LOCALE/grafana.json
+++ b/public/locales/pseudo-LOCALE/grafana.json
@@ -315,6 +315,10 @@
"empty-state": {
"title": "Ÿőū đőʼn'ŧ ĥävę äʼny čőʼnŧäčŧ pőįʼnŧş yęŧ"
},
+ "key-value-map": {
+ "add": "Åđđ",
+ "confirm-add": "Cőʼnƒįřm ŧő äđđ"
+ },
"last-delivery-attempt": "Ŀäşŧ đęľįvęřy äŧŧęmpŧ",
"last-delivery-failed": "Ŀäşŧ đęľįvęřy äŧŧęmpŧ ƒäįľęđ",
"no-contact-points-found": "Ńő čőʼnŧäčŧ pőįʼnŧş ƒőūʼnđ",