From 783f630c778c0a26d6090939dfb486a6291557c2 Mon Sep 17 00:00:00 2001 From: vitPinchuk Date: Fri, 23 Aug 2024 14:17:12 +0300 Subject: [PATCH 1/3] update values to multiselect elements --- src/components/FormPanel/FormPanel.test.tsx | 77 +++++++++++++++++++++ src/components/FormPanel/FormPanel.tsx | 3 + 2 files changed, 80 insertions(+) diff --git a/src/components/FormPanel/FormPanel.test.tsx b/src/components/FormPanel/FormPanel.test.tsx index 91d383cf..b69c0540 100644 --- a/src/components/FormPanel/FormPanel.test.tsx +++ b/src/components/FormPanel/FormPanel.test.tsx @@ -632,6 +632,83 @@ describe('Panel', () => { ); }); + it('Should update elements with query result for multiselect elements', async () => { + /** + * Render + */ + await act(async () => { + render( + getComponent({ + options: { + initial: { + method: RequestMethod.QUERY, + }, + elements: [ + { + ...FORM_ELEMENT_DEFAULT, + type: FormElementType.CHECKBOX_LIST, + id: 'mapped', + queryField: { + refId: 'A', + value: 'metric', + }, + }, + { + ...FORM_ELEMENT_DEFAULT, + id: 'unmapped', + queryField: undefined, + }, + ], + }, + props: { + data: { + state: LoadingState.Done, + series: [ + toDataFrame({ + fields: [ + { + name: 'metric', + values: ['metricA1', 'metricA2'], + }, + ], + refId: 'A', + }), + toDataFrame({ + fields: [ + { + name: 'metric', + values: ['metricB1', 'metricB2'], + }, + ], + refId: 'B', + }), + ], + }, + }, + }) + ); + + await waitFor(() => expect(selectors.loadingBar(true)).not.toBeInTheDocument()); + }); + + expect(FormElements).toHaveBeenNthCalledWith( + 2, + expect.objectContaining({ + elements: expect.arrayContaining([ + expect.objectContaining({ + id: 'mapped', + value: ['metricA1', 'metricA2'], + }), + expect.objectContaining({ + id: 'unmapped', + value: '', + }), + ]), + }), + expect.anything() + ); + }); + it('Should not update elements if no query result', async () => { /** * Render diff --git a/src/components/FormPanel/FormPanel.tsx b/src/components/FormPanel/FormPanel.tsx index f33abbd9..e2e7d554 100644 --- a/src/components/FormPanel/FormPanel.tsx +++ b/src/components/FormPanel/FormPanel.tsx @@ -201,6 +201,9 @@ export const FormPanel: React.FC = ({ */ const values = getFieldValues(field); + if (element.type === FormElementType.CHECKBOX_LIST || element.type === FormElementType.MULTISELECT) { + return convertToElementValue(element, values); + } return convertToElementValue(element, values[values.length - 1]); } From 6524767883f53869be44cb9678846a533bed5778 Mon Sep 17 00:00:00 2001 From: Mikhail Volkov Date: Mon, 26 Aug 2024 21:42:24 -0400 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0c90595..f8c9714a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Features / Enhancements - Added Expandable Editors (#472) +- Updated Pre-Selection for multi selection elements (#474) ## 4.3.1 (2024-08-16) From db16749720278249508dd0ffb80e321641f6b60f Mon Sep 17 00:00:00 2001 From: Mikhail Volkov Date: Mon, 26 Aug 2024 21:44:20 -0400 Subject: [PATCH 3/3] Update FormPanel.tsx --- src/components/FormPanel/FormPanel.tsx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/components/FormPanel/FormPanel.tsx b/src/components/FormPanel/FormPanel.tsx index e2e7d554..3511d80a 100644 --- a/src/components/FormPanel/FormPanel.tsx +++ b/src/components/FormPanel/FormPanel.tsx @@ -201,9 +201,13 @@ export const FormPanel: React.FC = ({ */ const values = getFieldValues(field); + /** + * Supports multi values + */ if (element.type === FormElementType.CHECKBOX_LIST || element.type === FormElementType.MULTISELECT) { return convertToElementValue(element, values); } + return convertToElementValue(element, values[values.length - 1]); }