Skip to content
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

Add option to not close select on tab keypress #1218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion addon/components/power-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default Component.extend({
noMatchesMessage: fallbackIfUndefined('No results found'),
searchMessage: fallbackIfUndefined('Type to search'),
closeOnSelect: fallbackIfUndefined(true),
closeOnTab: fallbackIfUndefined(true),
defaultHighlighted: fallbackIfUndefined(defaultHighlighted),
typeAheadMatcher: fallbackIfUndefined(defaultTypeAheadMatcher),
highlightOnHover: fallbackIfUndefined(true),
Expand Down Expand Up @@ -669,7 +670,9 @@ export default Component.extend({
},

_handleKeyTab(e) {
this.get('publicAPI').actions.close(e);
if (this.get('closeOnTab')) {
this.get('publicAPI').actions.close(e);
}
},

_handleKeyESC(e) {
Expand Down
2 changes: 2 additions & 0 deletions addon/templates/components/power-select-multiple.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
calculatePosition=calculatePosition
class=class
closeOnSelect=closeOnSelect
closeOnTab=closeOnTab
defaultHighlighted=defaultHighlighted
destination=destination
dir=dir
Expand Down Expand Up @@ -77,6 +78,7 @@
calculatePosition=calculatePosition
class=class
closeOnSelect=closeOnSelect
closeOnTab=closeOnTab
defaultHighlighted=defaultHighlighted
destination=destination
dir=dir
Expand Down
8 changes: 8 additions & 0 deletions tests/dummy/app/templates/public-pages/docs/api-reference.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@
an option, either with the mouse/keyboard or using the `choose` action in the publicAPI.
</td>
</tr>
<tr>
<td>closeOnTab</td>
<td><code>boolean</code></td>
<td>
Defaults to true. When false, the component won't be closed if the user hits the "Tab" key
when the dropdown is open.
</td>
</tr>
<tr>
<td>defaultHighlighted</td>
<td><code>any or function</code></td>
Expand Down
22 changes: 20 additions & 2 deletions tests/integration/components/power-select/keyboard-control-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ module('Integration | Component | Ember Power Select (Keyboard control)', functi
assert.dom('.ember-power-select-trigger').isFocused('The trigger is focused');
});

test('Pressing TAB closes the select WITHOUT selecting the highlighed element and focuses the trigger', async function(assert) {
test('If closeOnTab=true, pressing TAB closes the select WITHOUT selecting the highlighted element and focuses the trigger', async function(assert) {
assert.expect(3);

this.numbers = numbers;
Expand All @@ -204,7 +204,25 @@ module('Integration | Component | Ember Power Select (Keyboard control)', functi
await triggerKeyEvent('.ember-power-select-search-input', 'keydown', 9);
assert.dom('.ember-power-select-trigger').hasText('', 'The highlighted element wasn\'t selected');
assert.dom('.ember-power-select-dropdown').doesNotExist('The dropdown is closed');
assert.dom('.ember-power-select-trigger').isFocused('The trigges is focused');
assert.dom('.ember-power-select-trigger').isFocused('The trigger is focused');
});

test('If closeOnTab=false, pressing TAB does not close the select, select the highlighted element, or focus the trigger', async function(assert) {
assert.expect(3);

this.numbers = numbers;
await render(hbs`
{{#power-select closeOnTab=false options=numbers onchange=(action (mut foo)) as |option|}}
{{option}}
{{/power-select}}
`);

await clickTrigger();
await triggerKeyEvent('.ember-power-select-search-input', 'keydown', 40);
await triggerKeyEvent('.ember-power-select-search-input', 'keydown', 9);
assert.dom('.ember-power-select-trigger').hasText('', 'The highlighted element wasn\'t selected');
assert.dom('.ember-power-select-dropdown').exists('The dropdown is open');
assert.dom('.ember-power-select-trigger').isNotFocused('The trigger is not focused');
});

test('The component is focusable using the TAB key as any other kind of input', async function(assert) {
Expand Down