Skip to content

Commit

Permalink
feat(menu): allow item element to be a component
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Aug 27, 2021
1 parent d26fce7 commit 83796b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion addon/components/menu/item-element.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{{#let (element (or @tagName 'a')) as |Tag|}}
{{#let
(if this.tagNameIsComponent @tagName (element (or @tagName 'a')))
as |Tag|
}}
<Tag
id={{@guid}}
role='menuitem'
Expand Down
7 changes: 7 additions & 0 deletions addon/components/menu/item-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Component from '@glimmer/component';

export default class MenuItemElement extends Component {
get tagNameIsComponent() {
return typeof this.args.tagName === 'object';
}
}
22 changes: 22 additions & 0 deletions tests/integration/components/menu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ module('Integration | Component | <Menu>', (hooks) => {
.hasText('Item A [isActive:false] [isDisabled:false]');
});
});

test('it should be possible to use a custom component as a menu item', async function (assert) {
await render(hbs`
<Menu as |menu|>
<menu.Button data-test-menu-button>Trigger</menu.Button>
<menu.Items data-test-menu-items as |items|>
{{#let (component 'link-to' route="menu") as |Link|}}
<items.Item as |item|>
<item.Element @tagName={{Link}} data-test-item-a>
Item A
</item.Element>
</items.Item>
{{/let}}
</menu.Items>
</Menu>
`);

await triggerKeyEvent('[data-test-menu-button]', 'keydown', Keys.Enter);

assert.dom('[data-test-item-a]').hasTagName('a');
assert.dom('[data-test-item-a]').hasAttribute('href', '/menu');
});
});

module('Keyboard interactions', function () {
Expand Down

0 comments on commit 83796b3

Please sign in to comment.