Skip to content

Commit b4f6462

Browse files
committed
Merge pull request select2#643 from akinnee/master
Added an option: select highlighted option on blur, "selectOnBlur: false"
2 parents 8e9a231 + fa6313a commit b4f6462

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

select2.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,10 @@ the specific language governing permissions and limitations under the Apache Lic
13241324

13251325
// abstract
13261326
blur: function () {
1327+
// if selectOnBlur == true, select the currently highlighted option
1328+
if (this.opts.selectOnBlur)
1329+
this.selectHighlighted({noFocus: true});
1330+
13271331
this.close();
13281332
this.container.removeClass("select2-container-active");
13291333
this.dropdown.removeClass("select2-drop-active");
@@ -1351,14 +1355,14 @@ the specific language governing permissions and limitations under the Apache Lic
13511355
},
13521356

13531357
// abstract
1354-
selectHighlighted: function () {
1358+
selectHighlighted: function (options) {
13551359
var index=this.highlight(),
13561360
highlighted=this.results.find(".select2-highlighted").not(".select2-disabled"),
13571361
data = highlighted.closest('.select2-result-selectable').data("select2-data");
13581362
if (data) {
13591363
highlighted.addClass("select2-disabled");
13601364
this.highlight(index);
1361-
this.onSelect(data);
1365+
this.onSelect(data, options);
13621366
}
13631367
},
13641368

@@ -1708,13 +1712,15 @@ the specific language governing permissions and limitations under the Apache Lic
17081712
},
17091713

17101714
// single
1711-
onSelect: function (data) {
1715+
onSelect: function (data, options) {
17121716
var old = this.opts.element.val();
17131717

17141718
this.opts.element.val(this.id(data));
17151719
this.updateSelection(data);
17161720
this.close();
1717-
this.selection.focus();
1721+
1722+
if (!options || !options.noFocus)
1723+
this.selection.focus();
17181724

17191725
if (!equal(old, this.id(data))) { this.triggerChange(); }
17201726
},
@@ -2069,7 +2075,7 @@ the specific language governing permissions and limitations under the Apache Lic
20692075
},
20702076

20712077
// multi
2072-
onSelect: function (data) {
2078+
onSelect: function (data, options) {
20732079
this.addSelectedChoice(data);
20742080
if (this.select || !this.opts.closeOnSelect) this.postprocessResults();
20752081

@@ -2091,7 +2097,8 @@ the specific language governing permissions and limitations under the Apache Lic
20912097
// added we do not need to check if this is a new element before firing change
20922098
this.triggerChange({ added: data });
20932099

2094-
this.focusSearch();
2100+
if (!options || !options.noFocus)
2101+
this.focusSearch();
20952102
},
20962103

20972104
// multi
@@ -2445,7 +2452,8 @@ the specific language governing permissions and limitations under the Apache Lic
24452452
}
24462453
return markup;
24472454
},
2448-
blurOnChange: false
2455+
blurOnChange: false,
2456+
selectOnBlur: false
24492457
};
24502458

24512459
// exports

0 commit comments

Comments
 (0)