Skip to content

Commit fa6313a

Browse files
author
Alex Kinnee
committed
Added the option to select highlighted upon blur, false by default. Original functionality is maintained by default.
1 parent ac4ce8b commit fa6313a

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
@@ -1308,6 +1308,10 @@ the specific language governing permissions and limitations under the Apache Lic
13081308

13091309
// abstract
13101310
blur: function () {
1311+
// if selectOnBlur == true, select the currently highlighted option
1312+
if (this.opts.selectOnBlur)
1313+
this.selectHighlighted({noFocus: true});
1314+
13111315
this.close();
13121316
this.container.removeClass("select2-container-active");
13131317
this.dropdown.removeClass("select2-drop-active");
@@ -1335,14 +1339,14 @@ the specific language governing permissions and limitations under the Apache Lic
13351339
},
13361340

13371341
// abstract
1338-
selectHighlighted: function () {
1342+
selectHighlighted: function (options) {
13391343
var index=this.highlight(),
13401344
highlighted=this.results.find(".select2-highlighted").not(".select2-disabled"),
13411345
data = highlighted.closest('.select2-result-selectable').data("select2-data");
13421346
if (data) {
13431347
highlighted.addClass("select2-disabled");
13441348
this.highlight(index);
1345-
this.onSelect(data);
1349+
this.onSelect(data, options);
13461350
}
13471351
},
13481352

@@ -1692,13 +1696,15 @@ the specific language governing permissions and limitations under the Apache Lic
16921696
},
16931697

16941698
// single
1695-
onSelect: function (data) {
1699+
onSelect: function (data, options) {
16961700
var old = this.opts.element.val();
16971701

16981702
this.opts.element.val(this.id(data));
16991703
this.updateSelection(data);
17001704
this.close();
1701-
this.selection.focus();
1705+
1706+
if (!options || !options.noFocus)
1707+
this.selection.focus();
17021708

17031709
if (!equal(old, this.id(data))) { this.triggerChange(); }
17041710
},
@@ -2053,7 +2059,7 @@ the specific language governing permissions and limitations under the Apache Lic
20532059
},
20542060

20552061
// multi
2056-
onSelect: function (data) {
2062+
onSelect: function (data, options) {
20572063
this.addSelectedChoice(data);
20582064
if (this.select || !this.opts.closeOnSelect) this.postprocessResults();
20592065

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

2078-
this.focusSearch();
2084+
if (!options || !options.noFocus)
2085+
this.focusSearch();
20792086
},
20802087

20812088
// multi
@@ -2424,7 +2431,8 @@ the specific language governing permissions and limitations under the Apache Lic
24242431
}
24252432
return markup;
24262433
},
2427-
blurOnChange: false
2434+
blurOnChange: false,
2435+
selectOnBlur: false
24282436
};
24292437

24302438
// exports

0 commit comments

Comments
 (0)