Skip to content

Commit

Permalink
[UiActions][Drilldowns] Fix actions sorting in context menu (#77162)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
Dosant and elasticmachine authored Sep 14, 2020
1 parent 4a45dad commit 0fd503e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/plugins/data/public/actions/apply_filter_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function createFilterAction(
return createAction<typeof ACTION_GLOBAL_APPLY_FILTER>({
type: ACTION_GLOBAL_APPLY_FILTER,
id: ACTION_GLOBAL_APPLY_FILTER,
order: 100,
getIconType: () => 'filter',
getDisplayName: () => {
return i18n.translate('data.filter.applyFilterActionTitle', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function createFilterAction(): ActionByType<typeof ACTION_APPLY_FILTER> {
return createAction<typeof ACTION_APPLY_FILTER>({
type: ACTION_APPLY_FILTER,
id: ACTION_APPLY_FILTER,
order: 100,
getIconType: () => 'filter',
getDisplayName: () => {
return i18n.translate('embeddableApi.actions.applyFilterActionTitle', {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { buildContextMenuForActions } from './build_eui_context_menu_panels';
import { Action, createAction } from '../actions';

const createTestAction = ({
type,
dispayName,
order,
}: {
type: string;
dispayName: string;
order: number;
}) =>
createAction({
type: type as any, // mapping doesn't matter for this test
getDisplayName: () => dispayName,
order,
execute: async () => {},
});

test('contextMenu actions sorting: order, type, displayName', async () => {
const actions: Action[] = [
createTestAction({
order: 100,
type: '1',
dispayName: 'a',
}),
createTestAction({
order: 100,
type: '1',
dispayName: 'b',
}),
createTestAction({
order: 0,
type: '2',
dispayName: 'c',
}),
createTestAction({
order: 0,
type: '2',
dispayName: 'd',
}),
createTestAction({
order: 0,
type: '3',
dispayName: 'aa',
}),
].sort(() => 0.5 - Math.random());

const result = await buildContextMenuForActions({
actions: actions.map((action) => ({ action, context: {}, trigger: '' as any })),
});

expect(result.items?.map((item) => item.name as string)).toMatchInlineSnapshot(`
Array [
"a",
"b",
"c",
"d",
"aa",
]
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import * as React from 'react';
import { EuiContextMenuPanelDescriptor, EuiContextMenuPanelItemDescriptor } from '@elastic/eui';
import _ from 'lodash';
import sortBy from 'lodash/sortBy';
import { i18n } from '@kbn/i18n';
import { uiToReactComponent } from '../../../kibana_react/public';
import { Action } from '../actions';
Expand All @@ -46,11 +47,11 @@ interface ActionWithContext<Context extends BaseContext = BaseContext> {
export async function buildContextMenuForActions({
actions,
title = defaultTitle,
closeMenu,
closeMenu = () => {},
}: {
actions: ActionWithContext[];
title?: string;
closeMenu: () => void;
closeMenu?: () => void;
}): Promise<EuiContextMenuPanelDescriptor> {
const menuItems = await buildEuiContextMenuPanelItems({
actions,
Expand All @@ -74,6 +75,13 @@ async function buildEuiContextMenuPanelItems({
actions: ActionWithContext[];
closeMenu: () => void;
}) {
actions = sortBy(
actions,
(a) => -1 * (a.action.order ?? 0),
(a) => a.action.type,
(a) => a.action.getDisplayName({ ...a.context, trigger: a.trigger })
);

const items: EuiContextMenuPanelItemDescriptor[] = new Array(actions.length);
const promises = actions.map(async ({ action, context, trigger }, index) => {
const isCompatible = await action.isCompatible({
Expand Down

0 comments on commit 0fd503e

Please sign in to comment.