Skip to content

Commit

Permalink
feat: Add explanation at the end of disabled context menu item due to…
Browse files Browse the repository at this point in the history
… non stable id (#1972)

* chore: add changeset

* feat: add dynamic text to context menu item

---------

Co-authored-by: GDamyanov <georgi.damyanov@sap.com>
  • Loading branch information
nikmace and GDamyanov authored Jun 3, 2024
1 parent 82df5f2 commit 81026f9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/ten-snakes-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sap-ux-private/preview-middleware-client': patch
'@sap-ux/preview-middleware': patch
---

Add explanation at the end of disabled context menu item due to non stable ID
16 changes: 15 additions & 1 deletion packages/preview-middleware-client/src/adp/init-dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const initDialogs = (rta: RuntimeAuthoring, syncViewsIds: string[]): void

contextMenu.addMenuItem({
id: 'ADD_FRAGMENT',
text: 'Add: Fragment',
text: getAddFragmentItemText,
handler: async (overlays: UI5Element[]) => await handler(overlays[0], rta, DialogNames.ADD_FRAGMENT),
enabled: isFragmentCommandEnabled,
icon: 'sap-icon://attachment-html'
Expand Down Expand Up @@ -82,6 +82,20 @@ export const isFragmentCommandEnabled = (overlays: ElementOverlay[]): boolean =>
return hasStableId && overlays.length <= 1;
};

/**
* Determines the text that should be displayed for the Add Fragment context menu item.
*
* @param {ElementOverlay} overlay - An ElementOverlay object representing the UI overlay.
* @returns {string} The text of the Add Fragment context menu item.
*/
export const getAddFragmentItemText = (overlay: ElementOverlay) => {
if (!isFragmentCommandEnabled([overlay])) {
return 'Add: Fragment (Unavailable due to unstable ID of the control or its parent control)';
}

return 'Add: Fragment';
};

/**
* Handler for new context menu entry
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
handler,
initDialogs,
isFragmentCommandEnabled,
isControllerExtensionEnabled
isControllerExtensionEnabled,
getAddFragmentItemText
} from '../../../src/adp/init-dialogs';
import AddFragment from '../../../src/adp/controllers/AddFragment.controller';
import ControllerExtension from '../../../src/adp/controllers/ControllerExtension.controller';
Expand Down Expand Up @@ -110,6 +111,34 @@ describe('Dialogs', () => {
});
});

describe('getAddFragmentItemText', () => {
beforeEach(() => {
jest.restoreAllMocks();
});

const overlay = {
getElement: () => ({})
} as ElementOverlay;

it('should return simple text if the control is with a stable ID', () => {
Utils.checkControlId.mockReturnValue(true);

const result = getAddFragmentItemText(overlay);

expect(result).toBe('Add: Fragment');
expect(Utils.checkControlId).toHaveBeenCalledWith({});
});

it('should return extra text if the control is with a unstable ID', () => {
Utils.checkControlId.mockReturnValue(false);

const result = getAddFragmentItemText(overlay);

expect(result).toBe('Add: Fragment (Unavailable due to unstable ID of the control or its parent control)');
expect(Utils.checkControlId).toHaveBeenCalledWith({});
});
});

describe('isControllerExtensionEnabled', () => {
const syncViewsIds = ['syncViewId1', 'syncViewId2'];
const elementOverlayMock = { getElement: jest.fn() } as unknown as ElementOverlay;
Expand Down
5 changes: 3 additions & 2 deletions packages/preview-middleware-client/types/sap.ui.dt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ declare module 'sap/ui/dt/OverlayUtil' {
}

declare module 'sap/ui/dt/plugin/ContextMenu' {
import type ElementOverlay from 'sap/ui/dt/ElementOverlay';

export interface ContextMenuItem {
id: string;
text: string;
text: string | ((overlay: ElementOverlay) => string);
handler: Function;
icon?: string;
enabled?: Function;
Expand All @@ -93,4 +95,3 @@ declare module 'sap/ui/dt/Element' {

export default ElementExtended;
}

0 comments on commit 81026f9

Please sign in to comment.