Skip to content

Commit

Permalink
Added closeAfterSelect setting (closes #344).
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreavis committed Jan 30, 2015
1 parent f158a7d commit f9902c2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ $(function() {
<td valign="top"><code>boolean</code></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>closeAfterSelect</code></td>
<td valign="top">If true, the dropdown will be closed after a selection is made.</td>
<td valign="top"><code>boolean</code></td>
<td valign="top"><code>false</code></td>
</tr>
<tr>
<td valign="top"><code>allowEmptyOption</code></td>
<td valign="top">If true, Selectize will treat any options with a "" value like normal. This defaults to false to accomodate the common &lt;select&gt; practice of having the first empty option act as a placeholder.</td>
Expand Down
1 change: 1 addition & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Selectize.defaults = {
selectOnTab: false,
preload: false,
allowEmptyOption: false,
closeAfterSelect: false,

scrollDuration: 60,
loadThrottle: 300,
Expand Down
10 changes: 8 additions & 2 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,20 @@ $.extend(Selectize.prototype, {

$target = $(e.currentTarget);
if ($target.hasClass('create')) {
self.createItem();
self.createItem(null, function() {
if (self.settings.closeAfterSelect) {
self.close();
}
});
} else {
value = $target.attr('data-value');
if (typeof value !== 'undefined') {
self.lastQuery = null;
self.setTextboxValue('');
self.addItem(value);
if (!self.settings.hideSelected && e.type && /mouse/.test(e.type)) {
if (self.settings.closeAfterSelect) {
self.close();
} else if (!self.settings.hideSelected && e.type && /mouse/.test(e.type)) {
self.setActiveOption(self.getOption(value));
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@

describe('Interaction', function() {

it('should keep dropdown open after selection made if closeAfterSelect: false', function(done) {
var test = setup_test('<select multiple>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {});

click(test.selectize.$control, function() {
click($('[data-value=a]', test.selectize.$dropdown_content), function() {
expect(test.selectize.isOpen).to.be.equal(true);
expect(test.selectize.isFocused).to.be.equal(true);
done();
});
});
});

it('should close dropdown after selection made if closeAfterSelect: true', function(done) {
var test = setup_test('<select multiple>' +
'<option value="a">A</option>' +
'<option value="b">B</option>' +
'</select>', {closeAfterSelect: true});

click(test.selectize.$control, function() {
click($('[data-value=a]', test.selectize.$dropdown_content), function() {
expect(test.selectize.isOpen).to.be.equal(false);
expect(test.selectize.isFocused).to.be.equal(true);
done();
});
});
});

describe('clicking control', function() {

it('should give it focus', function(done) {
Expand Down

0 comments on commit f9902c2

Please sign in to comment.