feat: allow Menu.buildFromTemplate() to accept MenuItems (#16697)

* feat: allow Menu.buildFromTemplate to accept MenuItems

* add another spec

* fix linter error

* add submenu spec
This commit is contained in:
Shelley Vohr 2019-02-06 10:04:40 -08:00 committed by GitHub
parent 4211a9c69f
commit 858781ba83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 5 deletions

View file

@ -168,7 +168,13 @@ Menu.buildFromTemplate = function (template) {
const sorted = sortTemplate(filtered)
const menu = new Menu()
sorted.forEach((item) => menu.append(new MenuItem(item)))
sorted.forEach(item => {
if (item instanceof MenuItem) {
menu.append(item)
} else {
menu.append(new MenuItem(item))
}
})
return menu
}
@ -178,7 +184,11 @@ Menu.buildFromTemplate = function (template) {
// validate the template against having the wrong attribute
function areValidTemplateItems (template) {
return template.every(item =>
item != null && typeof item === 'object' && (item.hasOwnProperty('label') || item.hasOwnProperty('role') || item.type === 'separator'))
item != null &&
typeof item === 'object' &&
(item.hasOwnProperty('label') ||
item.hasOwnProperty('role') ||
item.type === 'separator'))
}
function sortTemplate (template) {