From 3b401d0d4db77341f8ae65da2170d3e6ce2a7a2e Mon Sep 17 00:00:00 2001 From: Ivan Ortega Alba Date: Wed, 17 Jan 2024 18:14:01 +0100 Subject: [PATCH] CustomVariable: Be able to edit them using scenes (#80193) --------- Co-authored-by: Alexandra Vargas --- .betterer.results | 3 +- .../new-query-variable.spec.ts | 4 +- .../src/selectors/pages.ts | 9 +- .../settings/variables/VariableEditorForm.tsx | 59 ++++----- .../components/CustomVariableForm.test.tsx | 116 ++++++++++++++++++ .../components/CustomVariableForm.tsx | 57 +++++++++ .../components/SelectionOptionsForm.tsx | 52 ++++++++ .../components/TextBoxVariableForm.tsx | 10 +- .../components/VariableCheckboxField.tsx | 5 +- .../components/VariableTextAreaField.tsx | 5 +- .../components/VariableTextField.tsx | 5 +- .../editors/CustomVariableEditor.test.tsx | 115 +++++++++++++++++ .../editors/CustomVariableEditor.tsx | 36 +++++- .../editors/TextBoxVariableEditor.tsx | 13 +- .../variables/custom/CustomVariableEditor.tsx | 50 +++----- .../editor/SelectionOptionsEditor.tsx | 36 ++---- .../variables/editor/VariableEditorEditor.tsx | 2 +- .../textbox/TextBoxVariableEditor.tsx | 19 ++- 18 files changed, 474 insertions(+), 122 deletions(-) create mode 100644 public/app/features/dashboard-scene/settings/variables/components/CustomVariableForm.test.tsx create mode 100644 public/app/features/dashboard-scene/settings/variables/components/CustomVariableForm.tsx create mode 100644 public/app/features/dashboard-scene/settings/variables/components/SelectionOptionsForm.tsx create mode 100644 public/app/features/dashboard-scene/settings/variables/editors/CustomVariableEditor.test.tsx diff --git a/.betterer.results b/.betterer.results index bb23979e018..659e04ad857 100644 --- a/.betterer.results +++ b/.betterer.results @@ -4392,8 +4392,7 @@ exports[`better eslint`] = { [0, 0, 0, "Do not use any type assertions.", "1"] ], "public/app/features/variables/editor/VariableEditorEditor.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Use data-testid for E2E selectors instead of aria-label", "1"] + [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], "public/app/features/variables/editor/VariableEditorList.tsx:5381": [ [0, 0, 0, "Use data-testid for E2E selectors instead of aria-label", "0"], diff --git a/e2e/dashboards-suite/new-query-variable.spec.ts b/e2e/dashboards-suite/new-query-variable.spec.ts index 3ffdda49310..5239060c2d7 100644 --- a/e2e/dashboards-suite/new-query-variable.spec.ts +++ b/e2e/dashboards-suite/new-query-variable.spec.ts @@ -69,7 +69,7 @@ describe('Variables - Query - Add variable', () => { }); e2e.pages.Dashboard.Settings.Variables.Edit.General.previewOfValuesOption().should('not.exist'); - e2e.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsCustomAllInputV2().should('not.exist'); + e2e.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsCustomAllInput().should('not.exist'); }); it('adding a single value query variable', () => { @@ -152,7 +152,7 @@ describe('Variables - Query - Add variable', () => { cy.get('input[type="checkbox"]').click({ force: true }).should('be.checked'); }); - e2e.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsCustomAllInputV2().within((input) => { + e2e.pages.Dashboard.Settings.Variables.Edit.General.selectionOptionsCustomAllInput().within((input) => { expect(input.attr('placeholder')).equals('blank = auto'); expect(input.val()).equals(''); }); diff --git a/packages/grafana-e2e-selectors/src/selectors/pages.ts b/packages/grafana-e2e-selectors/src/selectors/pages.ts index 27371fa1522..eac5b447914 100644 --- a/packages/grafana-e2e-selectors/src/selectors/pages.ts +++ b/packages/grafana-e2e-selectors/src/selectors/pages.ts @@ -137,12 +137,11 @@ export const Pages = { generalLabelInputV2: 'data-testid Variable editor Form Label field', generalHideSelect: 'Variable editor Form Hide select', generalHideSelectV2: 'data-testid Variable editor Form Hide select', - selectionOptionsMultiSwitch: 'Variable editor Form Multi switch', - selectionOptionsIncludeAllSwitch: 'Variable editor Form IncludeAll switch', - selectionOptionsCustomAllInput: 'Variable editor Form IncludeAll field', - selectionOptionsCustomAllInputV2: 'data-testid Variable editor Form IncludeAll field', + selectionOptionsMultiSwitch: 'data-testid Variable editor Form Multi switch', + selectionOptionsIncludeAllSwitch: 'data-testid Variable editor Form IncludeAll switch', + selectionOptionsCustomAllInput: 'data-testid Variable editor Form IncludeAll field', previewOfValuesOption: 'data-testid Variable editor Preview of Values option', - submitButton: 'Variable editor Submit button', + submitButton: 'data-testid Variable editor Run Query button', applyButton: 'data-testid Variable editor Apply button', }, QueryVariable: { diff --git a/public/app/features/dashboard-scene/settings/variables/VariableEditorForm.tsx b/public/app/features/dashboard-scene/settings/variables/VariableEditorForm.tsx index 02b2565c096..df52aea0273 100644 --- a/public/app/features/dashboard-scene/settings/variables/VariableEditorForm.tsx +++ b/public/app/features/dashboard-scene/settings/variables/VariableEditorForm.tsx @@ -1,10 +1,12 @@ -import React from 'react'; +import React, { FormEvent } from 'react'; +import { useAsyncFn } from 'react-use'; +import { lastValueFrom } from 'rxjs'; import { SelectableValue } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { SceneVariable } from '@grafana/scenes'; import { VariableHide, defaultVariableModel } from '@grafana/schema'; -import { HorizontalGroup, Button } from '@grafana/ui'; +import { HorizontalGroup, Button, LoadingPlaceholder } from '@grafana/ui'; import { VariableHideSelect } from 'app/features/dashboard-scene/settings/variables/components/VariableHideSelect'; import { VariableLegend } from 'app/features/dashboard-scene/settings/variables/components/VariableLegend'; import { VariableTextAreaField } from 'app/features/dashboard-scene/settings/variables/components/VariableTextAreaField'; @@ -24,11 +26,11 @@ interface VariableEditorFormProps { } export function VariableEditorForm({ variable, onTypeChange, onGoBack, onDiscardChanges }: VariableEditorFormProps) { - const { name: initialName, type, label: initialLabel, description: initialDescription, hide } = variable.useState(); + const { name, type, label, description, hide } = variable.useState(); const EditorToRender = isEditableVariableType(type) ? getVariableEditor(type) : undefined; - const [name, setName] = React.useState(initialName ?? ''); - const [label, setLabel] = React.useState(initialLabel ?? ''); - const [description, setDescription] = React.useState(initialDescription ?? ''); + const [runQueryState, onRunQuery] = useAsyncFn(async () => { + await lastValueFrom(variable.validateAndUpdate!()); + }, [variable]); const onVariableTypeChange = (option: SelectableValue) => { if (option.value) { @@ -36,13 +38,10 @@ export function VariableEditorForm({ variable, onTypeChange, onGoBack, onDiscard } }; - const onNameChange = (e: React.FormEvent) => setName(e.currentTarget.value); - const onLabelChange = (e: React.FormEvent) => setLabel(e.currentTarget.value); - const onDescriptionChange = (e: React.FormEvent) => setDescription(e.currentTarget.value); - - const onNameBlur = () => variable.setState({ name }); - const onLabelBlur = () => variable.setState({ label }); - const onDescriptionBlur = () => variable.setState({ description }); + const onNameBlur = (e: FormEvent) => variable.setState({ name: e.currentTarget.value }); + const onLabelBlur = (e: FormEvent) => variable.setState({ label: e.currentTarget.value }); + const onDescriptionBlur = (e: FormEvent) => + variable.setState({ description: e.currentTarget.value }); const onHideChange = (hide: VariableHide) => variable.setState({ hide }); return ( @@ -52,12 +51,11 @@ export function VariableEditorForm({ variable, onTypeChange, onGoBack, onDiscard General - {EditorToRender && } + {EditorToRender && } - {hasVariableOptions(variable) && } + {hasVariableOptions(variable) && }
{/* */} - {/* */} +