diff --git a/.changeset/nervous-tables-shake.md b/.changeset/nervous-tables-shake.md new file mode 100644 index 0000000000..e0e4c3502c --- /dev/null +++ b/.changeset/nervous-tables-shake.md @@ -0,0 +1,6 @@ +--- +'@sap-ux-private/preview-middleware-client': patch +'@sap-ux/preview-middleware': patch +--- + +fix: "Add Table Custom Action" quick action not being working in some V2 apps diff --git a/packages/preview-middleware-client/src/adp/quick-actions/fe-v2/create-table-action.ts b/packages/preview-middleware-client/src/adp/quick-actions/fe-v2/create-table-action.ts index 9214da0032..b8f20f3244 100644 --- a/packages/preview-middleware-client/src/adp/quick-actions/fe-v2/create-table-action.ts +++ b/packages/preview-middleware-client/src/adp/quick-actions/fe-v2/create-table-action.ts @@ -10,6 +10,8 @@ import { getControlById, isA } from '../../../utils/core'; import { DialogFactory, DialogNames } from '../../dialog-factory'; import { TableQuickActionDefinitionBase } from '../table-quick-action-base'; import { DIALOG_ENABLEMENT_VALIDATOR } from '../dialog-enablement-validator'; +import type OverflowToolbar from 'sap/m/OverflowToolbar'; +import { NestedQuickActionChild } from '@sap-ux-private/control-property-editor-common'; export const CREATE_TABLE_ACTION = 'create-table-action'; const SMART_TABLE_TYPE = 'sap.ui.comp.smarttable.SmartTable'; @@ -25,6 +27,25 @@ export class AddTableActionQuickAction extends TableQuickActionDefinitionBase im ]); } + async initialize(): Promise { + const processChild = (child: NestedQuickActionChild, mapKey: string) => { + const table = this.tableMap[mapKey]?.table; + if (table) { + const headerToolbar = this.getHeaderToolbar(table); + if (!headerToolbar) { + child.enabled = false; + child.tooltip = this.context.resourceBundle.getText('NO_TABLE_HEADER_TOOLBAR'); + } + } + + child.children.forEach((nestedChild, idx) => processChild(nestedChild, `${mapKey}/${idx.toFixed(0)}`)); + }; + + await super.initialize(); + + // disable nested actions based on conditions + this.children.forEach((nestedChild, idx) => processChild(nestedChild, `${idx.toFixed(0)}`)); + } async execute(path: string): Promise { const { table, iconTabBarFilterKey, sectionInfo } = this.tableMap[path]; if (!table) { @@ -45,12 +66,7 @@ export class AddTableActionQuickAction extends TableQuickActionDefinitionBase im this.iconTabBar.setSelectedKey(iconTabBarFilterKey); } - let headerToolbar; - if (isA(SMART_TABLE_TYPE, table)) { - headerToolbar = (table.getAggregation('items') as ManagedObject[])[0].getAggregation('headerToolbar'); - } else if (isA(M_TABLE_TYPE, table)) { - headerToolbar = table.getAggregation('headerToolbar'); - } + const headerToolbar = this.getHeaderToolbar(table); // open dialogBox to add, and content is selected ByDefault if (headerToolbar) { @@ -62,4 +78,19 @@ export class AddTableActionQuickAction extends TableQuickActionDefinitionBase im } return []; } + + getHeaderToolbar(table: UI5Element): ManagedObject | ManagedObject[] | OverflowToolbar | null | undefined { + let headerToolbar; + if (isA(SMART_TABLE_TYPE, table)) { + headerToolbar = (table.getAggregation('items') as ManagedObject[]).find((item) => { + if (item.getAggregation('headerToolbar')) { + return true; + } + return isA('sap.m.OverflowToolbar', item); + }); + } else if (isA
(M_TABLE_TYPE, table)) { + headerToolbar = table.getAggregation('headerToolbar'); + } + return headerToolbar; + } } diff --git a/packages/preview-middleware-client/src/messagebundle.properties b/packages/preview-middleware-client/src/messagebundle.properties index e96c5a06da..ddaa90640b 100644 --- a/packages/preview-middleware-client/src/messagebundle.properties +++ b/packages/preview-middleware-client/src/messagebundle.properties @@ -50,6 +50,7 @@ EMPTY_ROW_MODE_IS_NOT_SUPPORTED=This action is disabled because empty row mode i VARIANT_MANAGEMENT_FOR_PAGE_CONTROLS_IS_ALREADY_ENABLED=This option has been disabled because variant management is already enabled for tables and charts VARIANT_MANAGEMENT_FOR_TABLE_CONTROLS_IS_ALREADY_ENABLED=This option has been disabled because variant management is already enabled for the ''{0}'' VARIANT_MANAGEMENT_FOR_CUSTOM_TABLES_NOT_SUPPORTED=Variant management cannot be set for custom table ''{0}'' +NO_TABLE_HEADER_TOOLBAR=This option has been disabled because the table does not have a header toolbar. ADD_ANNOTATION_FILE=Add Annotation File for ''{0}'' SHOW_ANNOTATION_FILE=Show Annotation File for ''{0}'' diff --git a/packages/preview-middleware-client/test/unit/adp/quick-actions/fe-v2.test.ts b/packages/preview-middleware-client/test/unit/adp/quick-actions/fe-v2.test.ts index 3c507c536c..dc19c5dbd7 100644 --- a/packages/preview-middleware-client/test/unit/adp/quick-actions/fe-v2.test.ts +++ b/packages/preview-middleware-client/test/unit/adp/quick-actions/fe-v2.test.ts @@ -547,7 +547,19 @@ describe('FE V2 quick actions', () => { }); describe('create table action', () => { - test('initialize and execute', async () => { + const testCases = [ + { + tableType: M_TABLE_TYPE + }, + { + tableType: SMART_TABLE_TYPE, + overflowTable: true, + headerToolbar: false + }, + { tableType: SMART_TABLE_TYPE, headerToolbar: true }, + { tableType: SMART_TABLE_TYPE, isTableWithoutToolbar: true } + ]; + test.each(testCases)('initialize and execute action (%s)', async (testCase) => { const pageView = new XMLView(); const scrollIntoView = jest.fn(); sapCoreMock.byId.mockImplementation((id) => { @@ -571,6 +583,51 @@ describe('FE V2 quick actions', () => { } }; } + if (id == 'smartTable') { + return { + isA: (type: string) => type === SMART_TABLE_TYPE, + getHeader: () => 'MyTable', + getId: () => id, + getDomRef: () => ({ + scrollIntoView + }), + + getAggregation: (aggregationName: string) => { + if (aggregationName === 'items') { + if (testCase.headerToolbar) { + return [ + { + isA: (type: string) => type === testCase.tableType, + getAggregation: (aggregationName: string) => { + if (aggregationName === 'headerToolbar') { + return 'headerToolbar'; // Return a simple string for headerToolbar + } + } + } + ]; + } else if (testCase.overflowTable) { + return [ + { + getAggregation: () => null, + isA: (type: string) => type === 'sap.m.OverflowToolbar' + } + ]; + } else { + return [ + { + getAggregation: () => null, + isA: (type: string) => type === 'test' + } + ]; + } + } + return []; + }, + getParent: () => pageView, + getBusy: () => false, + selectOverlay: () => ({}) + }; + } if (id == 'NavContainer') { const container = new NavContainer(); const component = new UIComponentMock(); @@ -615,18 +672,35 @@ describe('FE V2 quick actions', () => { ); await service.init(sendActionMock, subscribeMock); - await service.reloadQuickActions({ - 'sap.m.Table': [ - { - controlId: 'mTable' - } as any - ], - 'sap.m.NavContainer': [ - { - controlId: 'NavContainer' - } as any - ] - }); + if (testCase.tableType === M_TABLE_TYPE) { + await service.reloadQuickActions({ + 'sap.m.Table': [ + { + controlId: 'mTable' + } as any + ], + + 'sap.m.NavContainer': [ + { + controlId: 'NavContainer' + } as any + ] + }); + } else if (testCase.tableType === SMART_TABLE_TYPE) { + await service.reloadQuickActions({ + 'sap.ui.comp.smarttable.SmartTable': [ + { + controlId: 'smartTable' + } as any + ], + + 'sap.m.NavContainer': [ + { + controlId: 'NavContainer' + } as any + ] + }); + } expect(sendActionMock).toHaveBeenCalledWith( quickActionListChanged([ @@ -634,50 +708,56 @@ describe('FE V2 quick actions', () => { title: 'LIST REPORT', actions: [ { - 'kind': 'nested', + kind: 'nested', id: 'listReport0-create-table-action', title: 'Add Custom Table Action', enabled: true, children: [ { children: [], - enabled: true, - label: `'MyTable' table` + enabled: !testCase.isTableWithoutToolbar, + label: `'MyTable' table`, + ...(testCase.isTableWithoutToolbar && { + tooltip: + 'This option has been disabled because the table does not have a header toolbar.' + }) } ] }, { - 'children': [ + children: [ { - 'children': [], + children: [], enabled: true, - 'label': `'MyTable' table` + label: `'MyTable' table` } ], - 'enabled': true, - 'id': 'listReport0-create-table-custom-column', - 'kind': 'nested', - 'title': 'Add Custom Table Column' + enabled: true, + id: 'listReport0-create-table-custom-column', + kind: 'nested', + title: 'Add Custom Table Column' } ] } ]) ); - await subscribeMock.mock.calls[0][0]( executeQuickAction({ id: 'listReport0-create-table-action', kind: 'nested', path: '0' }) ); + expect(DialogFactory.createDialog).toHaveBeenCalledTimes(testCase.isTableWithoutToolbar ? 0 : 1); - expect(DialogFactory.createDialog).toHaveBeenCalledWith( - mockOverlay, - rtaMock, - 'AddFragment', - undefined, - { - aggregation: 'content', - title: 'QUICK_ACTION_ADD_CUSTOM_TABLE_ACTION' - } - ); + if (!testCase.isTableWithoutToolbar) { + expect(DialogFactory.createDialog).toHaveBeenCalledWith( + mockOverlay, + rtaMock, + 'AddFragment', + undefined, + { + aggregation: 'content', + title: 'QUICK_ACTION_ADD_CUSTOM_TABLE_ACTION' + } + ); + } }); });