Skip to content

Commit

Permalink
Fixes #82: Adds the disable-keyboard-search attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
bicknellr committed Nov 10, 2016
1 parent eb20ed8 commit 4c1fee6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
27 changes: 27 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ <h3>Multi-select menubar</h3>
</div>
</div>

<div>
<h3>Keyboard search</h3>
<div class="horizontal-section">
<simple-menu>
<a href="javascript:void(0)" role="menuitem">Alabama</a>
<a href="javascript:void(0)" role="menuitem">Alaska</a>
<a href="javascript:void(0)" role="menuitem">California</a>
<a href="javascript:void(0)" role="menuitem">Colorado</a>
<a href="javascript:void(0)" role="menuitem">New Mexico</a>
<a href="javascript:void(0)" role="menuitem">New York</a>
</simple-menu>
</div>
</div>

<div>
<h3>Keyboard search disabled</h3>
<div class="horizontal-section">
<simple-menu disable-keyboard-search>
<a href="javascript:void(0)" role="menuitem">Alabama</a>
<a href="javascript:void(0)" role="menuitem">Alaska</a>
<a href="javascript:void(0)" role="menuitem">California</a>
<a href="javascript:void(0)" role="menuitem">Colorado</a>
<a href="javascript:void(0)" role="menuitem">New Mexico</a>
<a href="javascript:void(0)" role="menuitem">New York</a>
</simple-menu>
</div>
</div>
</div>
</body>
</html>
25 changes: 21 additions & 4 deletions iron-menu-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@
*/
attrForItemTitle: {
type: String
},

/**
* Setting this attribute to true will prevent keyboard events from
* causing the menu items to be searched and will allow all keyboard
* events except those used for menu navigation to continue propagating.
*/
disableKeyboardSearch: {
type: Boolean,
value: false,
}
},

Expand Down Expand Up @@ -350,11 +360,18 @@
* @param {KeyboardEvent} event A keyboard event.
*/
_onKeydown: function(event) {
if (!this.keyboardEventMatchesKeys(event, 'up down esc')) {
// all other keys focus the menu item starting with that character
this._focusWithKeyboardEvent(event);
if (this.disableKeyboardSearch) {
// Stop only keyboard navigation events.
if (this.keyboardEventMatchesKeys(event, 'up down esc')) {
event.stopPropagation();
}
} else {
// Attempt to focus with any non-navigation keyboard event.
if (!this.keyboardEventMatchesKeys(event, 'up down esc')) {
this._focusWithKeyboardEvent(event);
}
event.stopPropagation();
}
event.stopPropagation();
},

// override _activateHandler
Expand Down
28 changes: 28 additions & 0 deletions test/iron-menu-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,34 @@
});
});

test('`disableKeyboardSearch` allows non-navigation keyboard events to propagate.', function(done) {
var menu = fixture('countries');
menu.disableKeyboardSearch = true;

MockInteractions.focus(menu);

// Wait for async focus
Polymer.Base.async(function() {
// Focus initially moves to the first item.
assert.equal(Polymer.dom(menu).node.focusedItem, menu.items[0], 'menu.items[0] is focused');

// Key press 'u'
MockInteractions.keyDownOn(menu, 85);

// Focus does not change for keyboard events.
assert.equal(Polymer.dom(menu).node.focusedItem, menu.items[0], 'menu.items[0] is focused');

// Key press 'g'
MockInteractions.keyDownOn(menu, 71);

Polymer.Base.async(function() {
// Focus does not change for keyboard events.
assert.equal(Polymer.dom(menu).node.focusedItem, menu.items[0], 'menu.items[0] is focused');
done();
});
});
});

test('focusing on item with bogus attr-for-item-title', function(done) {
var menu = fixture('bogus-attr-for-item-title');
MockInteractions.focus(menu);
Expand Down

0 comments on commit 4c1fee6

Please sign in to comment.