From 5a5520b5dafa08d139f60a3dda852f246e0d2791 Mon Sep 17 00:00:00 2001 From: Sergej-Vlasov <37613182+Sergej-Vlasov@users.noreply.github.com> Date: Fri, 9 Feb 2024 12:01:58 +0000 Subject: [PATCH] Dashboards: add delete variable flow to `VariableEditorForm` (#82149) * add delete variable flow to VariableEditorForm * adjust modal logic and replace HorizontalGroup with Stack * revert onDelete prop name --- .../settings/VariablesEditView.tsx | 5 +- .../settings/variables/VariableEditorForm.tsx | 155 ++++++++++-------- 2 files changed, 94 insertions(+), 66 deletions(-) diff --git a/public/app/features/dashboard-scene/settings/VariablesEditView.tsx b/public/app/features/dashboard-scene/settings/VariablesEditView.tsx index 2f79d02a8df..d574cbdd5c5 100644 --- a/public/app/features/dashboard-scene/settings/VariablesEditView.tsx +++ b/public/app/features/dashboard-scene/settings/VariablesEditView.tsx @@ -207,6 +207,7 @@ function VariableEditorSettingsListView({ model }: SceneComponentProps ); } @@ -234,6 +235,7 @@ interface VariableEditorSettingsEditViewProps { dashboard: DashboardScene; onTypeChange: (variableType: EditableVariableType) => void; onGoBack: () => void; + onDelete: (variableName: string) => void; } function VariableEditorSettingsView({ @@ -243,6 +245,7 @@ function VariableEditorSettingsView({ dashboard, onTypeChange, onGoBack, + onDelete, }: VariableEditorSettingsEditViewProps) { const parentTab = pageNav.children!.find((p) => p.active)!; parentTab.parentItem = pageNav; @@ -255,7 +258,7 @@ function VariableEditorSettingsView({ return ( - + ); } diff --git a/public/app/features/dashboard-scene/settings/variables/VariableEditorForm.tsx b/public/app/features/dashboard-scene/settings/variables/VariableEditorForm.tsx index 3038cb6fe04..4f0ab76a584 100644 --- a/public/app/features/dashboard-scene/settings/variables/VariableEditorForm.tsx +++ b/public/app/features/dashboard-scene/settings/variables/VariableEditorForm.tsx @@ -1,18 +1,19 @@ +import { css } from '@emotion/css'; import React, { FormEvent } from 'react'; import { useAsyncFn } from 'react-use'; import { lastValueFrom } from 'rxjs'; -import { SelectableValue } from '@grafana/data'; +import { GrafanaTheme2, SelectableValue } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; +import { reportInteraction } from '@grafana/runtime'; import { SceneVariable } from '@grafana/scenes'; import { VariableHide, defaultVariableModel } from '@grafana/schema'; -import { HorizontalGroup, Button, LoadingPlaceholder } from '@grafana/ui'; +import { Button, LoadingPlaceholder, ConfirmModal, ModalsController, Stack, useStyles2 } 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'; import { VariableTextField } from 'app/features/dashboard-scene/settings/variables/components/VariableTextField'; import { VariableValuesPreview } from 'app/features/dashboard-scene/settings/variables/components/VariableValuesPreview'; -import { ConfirmDeleteModal } from 'app/features/variables/editor/ConfirmDeleteModal'; import { VariableNameConstraints } from 'app/features/variables/editor/types'; import { VariableTypeSelect } from './components/VariableTypeSelect'; @@ -22,9 +23,11 @@ interface VariableEditorFormProps { variable: SceneVariable; onTypeChange: (type: EditableVariableType) => void; onGoBack: () => void; + onDelete: (variableName: string) => void; } -export function VariableEditorForm({ variable, onTypeChange, onGoBack }: VariableEditorFormProps) { +export function VariableEditorForm({ variable, onTypeChange, onGoBack, onDelete }: VariableEditorFormProps) { + const styles = useStyles2(getStyles); const { name, type, label, description, hide } = variable.useState(); const EditorToRender = isEditableVariableType(type) ? getVariableEditor(type) : undefined; const [runQueryState, onRunQuery] = useAsyncFn(async () => { @@ -42,78 +45,100 @@ export function VariableEditorForm({ variable, onTypeChange, onGoBack }: Variabl const onDescriptionBlur = (e: FormEvent) => variable.setState({ description: e.currentTarget.value }); const onHideChange = (hide: VariableHide) => variable.setState({ hide }); + const isHasVariableOptions = hasVariableOptions(variable); + const onDeleteVariable = (hideModal: () => void) => () => { + reportInteraction('Delete variable'); + onDelete(name); + hideModal(); + }; + return ( - <> -
- + + - General - - - + General + + + - + - {EditorToRender && } + {EditorToRender && } - {isHasVariableOptions && } + {isHasVariableOptions && } -
- - {/* */} - - - {isHasVariableOptions && ( +
+ + + {({ showModal, hideModal }) => ( )} - -
- - console.log('needs implementation')} - onDismiss={() => console.log('needs implementation')} - /> - + + + + {isHasVariableOptions && ( + + )} + +
+ ); } + +const getStyles = (theme: GrafanaTheme2) => ({ + buttonContainer: css({ + marginTop: theme.spacing(2), + }), +});