RowItemEditor: Add row title edit undo action (#110398)

* add undo/redo for row title change

* refactor with useRef
This commit is contained in:
Sergej-Vlasov
2025-09-02 10:09:16 +03:00
committed by GitHub
parent ceec2340b3
commit fb7b69e8b5
2 changed files with 19 additions and 1 deletions
@@ -1,4 +1,4 @@
import { useId, useMemo } from 'react';
import { useId, useMemo, useRef } from 'react';
import { selectors } from '@grafana/e2e-selectors';
import { Trans, t } from '@grafana/i18n';
@@ -10,6 +10,7 @@ import { SHARED_DASHBOARD_QUERY } from 'app/plugins/datasource/dashboard/constan
import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource';
import { useConditionalRenderingEditor } from '../../conditional-rendering/ConditionalRenderingEditor';
import { dashboardEditActions } from '../../edit-pane/shared';
import { getQueryRunnerFor, useDashboard } from '../../utils/utils';
import { useLayoutCategory } from '../layouts-shared/DashboardLayoutSelector';
import { useEditPaneInputAutoFocus } from '../layouts-shared/utils';
@@ -85,6 +86,7 @@ export function useEditOptions(model: RowItem, isNewElement: boolean): OptionsPa
function RowTitleInput({ row, isNewElement }: { row: RowItem; isNewElement: boolean }) {
const { title } = row.useState();
const prevTitle = useRef('');
const ref = useEditPaneInputAutoFocus({ autoFocus: isNewElement });
const hasUniqueTitle = row.hasUniqueTitle();
@@ -102,6 +104,8 @@ function RowTitleInput({ row, isNewElement }: { row: RowItem; isNewElement: bool
ref={ref}
title={t('dashboard.rows-layout.row-options.title-option', 'Title')}
value={title}
onFocus={() => (prevTitle.current = title || '')}
onBlur={() => editRowTitleAction(row, title || '', prevTitle.current || '')}
onChange={(e) => row.onChangeTitle(e.currentTarget.value)}
/>
</Field>
@@ -168,3 +172,16 @@ function RowRepeatSelect({ row, id }: { row: RowItem; id?: string }) {
</>
);
}
function editRowTitleAction(row: RowItem, title: string, prevTitle: string) {
if (title === prevTitle) {
return;
}
dashboardEditActions.edit({
description: t('dashboard.edit-actions.row-title', 'Change row title'),
source: row,
perform: () => row.onChangeTitle(title),
undo: () => row.onChangeTitle(prevTitle),
});
}
+1
View File
@@ -4691,6 +4691,7 @@
"panel-title": "Change panel title",
"paste-panel": "Paste panel",
"remove": "Remove {{typeName}}",
"row-title": "Change row title",
"switch-layout": "Switch layout"
},
"edit-pane": {