-
Notifications
You must be signed in to change notification settings - Fork 893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhance grouping for context menu options #3924
Changes from all commits
b98934b
232fc03
867978f
41daddd
c5ce04d
967a8f1
575bae7
39f0f1b
649b21a
a25cbc4
d9bf726
4c7fece
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,7 @@ export class ActionInternal<A extends ActionDefinition = ActionDefinition> | |
return this.definition.getIconType(context); | ||
} | ||
|
||
public getDisplayName(context: Context<A>): string { | ||
public getDisplayName(context: Context<A>): JSX.Element | string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rename this since Display name is misleading here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed that name would be better, but I'd recommend against it at this point, because it would mean a lot of downstream changes as well (such as other plugins not in this repository). |
||
if (!this.definition.getDisplayName) return `Action: ${this.id}`; | ||
return this.definition.getDisplayName(context); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,20 +36,28 @@ const createTestAction = ({ | |
type, | ||
dispayName, | ||
order, | ||
grouping, | ||
}: { | ||
type?: string; | ||
dispayName: string; | ||
order?: number; | ||
grouping?: any[]; | ||
}) => | ||
createAction({ | ||
type: type as any, // mapping doesn't matter for this test | ||
getDisplayName: () => dispayName, | ||
order, | ||
execute: async () => {}, | ||
grouping, | ||
}); | ||
|
||
const resultMapper = (panel: EuiContextMenuPanelDescriptor) => ({ | ||
items: panel.items ? panel.items.map((item) => ({ name: item.name })) : [], | ||
items: panel.items | ||
? panel.items.map((item) => ({ | ||
...(item.name ? { name: item.name } : {}), | ||
...(item.isSeparator ? { isSeparator: true } : {}), | ||
})) | ||
: [], | ||
}); | ||
|
||
test('sorts items in DESC order by "order" field first, then by display name', async () => { | ||
|
@@ -248,3 +256,195 @@ test('hides items behind in "More" submenu if there are more than 4 actions', as | |
] | ||
`); | ||
}); | ||
|
||
test('flattening of group with only one action', async () => { | ||
const grouping1 = [ | ||
{ | ||
id: 'test-group', | ||
getDisplayName: () => 'Test group', | ||
getIconType: () => 'bell', | ||
}, | ||
]; | ||
const actions = [ | ||
createTestAction({ | ||
dispayName: 'Foo 1', | ||
}), | ||
createTestAction({ | ||
dispayName: 'Bar 1', | ||
grouping: grouping1, | ||
}), | ||
]; | ||
const menu = await buildContextMenuForActions({ | ||
actions: actions.map((action) => ({ action, context: {}, trigger: 'TEST' as any })), | ||
}); | ||
|
||
expect(menu.map(resultMapper)).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"items": Array [ | ||
Object { | ||
"name": "Foo 1", | ||
}, | ||
Object { | ||
"isSeparator": true, | ||
}, | ||
Object { | ||
"name": "Bar 1", | ||
}, | ||
], | ||
}, | ||
Object { | ||
"items": Array [ | ||
Object { | ||
"name": "Bar 1", | ||
}, | ||
], | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test('grouping with only two actions', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for all the tests! |
||
const grouping1 = [ | ||
{ | ||
id: 'test-group', | ||
getDisplayName: () => 'Test group', | ||
getIconType: () => 'bell', | ||
}, | ||
]; | ||
const actions = [ | ||
createTestAction({ | ||
dispayName: 'Foo 1', | ||
}), | ||
createTestAction({ | ||
dispayName: 'Bar 1', | ||
grouping: grouping1, | ||
}), | ||
createTestAction({ | ||
dispayName: 'Bar 2', | ||
grouping: grouping1, | ||
}), | ||
]; | ||
const menu = await buildContextMenuForActions({ | ||
actions: actions.map((action) => ({ action, context: {}, trigger: 'TEST' as any })), | ||
}); | ||
|
||
expect(menu.map(resultMapper)).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"items": Array [ | ||
Object { | ||
"name": "Foo 1", | ||
}, | ||
Object { | ||
"isSeparator": true, | ||
}, | ||
Object { | ||
"name": "Test group", | ||
}, | ||
], | ||
}, | ||
Object { | ||
"items": Array [ | ||
Object { | ||
"name": "Bar 1", | ||
}, | ||
Object { | ||
"name": "Bar 2", | ||
}, | ||
], | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
test('groups with deep nesting', async () => { | ||
const grouping1 = [ | ||
{ | ||
id: 'test-group', | ||
getDisplayName: () => 'Test group', | ||
getIconType: () => 'bell', | ||
}, | ||
]; | ||
const grouping2 = [ | ||
{ | ||
id: 'test-group-2', | ||
getDisplayName: () => 'Test group 2', | ||
getIconType: () => 'bell', | ||
}, | ||
{ | ||
id: 'test-group-3', | ||
getDisplayName: () => 'Test group 3', | ||
getIconType: () => 'bell', | ||
}, | ||
]; | ||
|
||
const actions = [ | ||
createTestAction({ | ||
dispayName: 'Foo 1', | ||
}), | ||
createTestAction({ | ||
dispayName: 'Bar 1', | ||
grouping: grouping1, | ||
}), | ||
createTestAction({ | ||
dispayName: 'Bar 2', | ||
grouping: grouping1, | ||
}), | ||
createTestAction({ | ||
dispayName: 'Qux 1', | ||
grouping: grouping2, | ||
}), | ||
]; | ||
const menu = await buildContextMenuForActions({ | ||
actions: actions.map((action) => ({ action, context: {}, trigger: 'TEST' as any })), | ||
}); | ||
|
||
expect(menu.map(resultMapper)).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"items": Array [ | ||
Object { | ||
"name": "Foo 1", | ||
}, | ||
Object { | ||
"isSeparator": true, | ||
}, | ||
Object { | ||
"name": "Test group", | ||
}, | ||
Object { | ||
"isSeparator": true, | ||
}, | ||
Object { | ||
"name": "Test group 3", | ||
}, | ||
], | ||
}, | ||
Object { | ||
"items": Array [ | ||
Object { | ||
"name": "Bar 1", | ||
}, | ||
Object { | ||
"name": "Bar 2", | ||
}, | ||
], | ||
}, | ||
Object { | ||
"items": Array [ | ||
Object { | ||
"name": "Test group 3", | ||
}, | ||
], | ||
}, | ||
Object { | ||
"items": Array [ | ||
Object { | ||
"name": "Qux 1", | ||
}, | ||
], | ||
}, | ||
] | ||
`); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it helps, I can change this comment to inform users that a React element is also supported here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that'd be helpful.