Skip to content

Commit 4b1204c

Browse files
committed
improve keyboard handling. fixes select2#389
1 parent 908c358 commit 4b1204c

1 file changed

Lines changed: 7 additions & 38 deletions

File tree

select2.js

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,56 +1537,25 @@
15371537
selection.bind("keydown", this.bind(function(e) {
15381538
if (!this.enabled) return;
15391539

1540-
if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
1541-
// prevent the page from scrolling
1540+
if (e.which == KEY.DOWN || e.which == KEY.UP
1541+
|| (e.which == KEY.ENTER && this.opts.openOnEnter)) {
1542+
this.open();
15421543
killEvent(e);
15431544
return;
15441545
}
15451546

1546-
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e)
1547-
|| e.which === KEY.ESC) {
1548-
return;
1549-
}
1550-
1551-
if (this.opts.openOnEnter === false && e.which === KEY.ENTER) {
1552-
return;
1553-
}
1554-
15551547
if (e.which == KEY.DELETE || e.which == KEY.BACKSPACE) {
15561548
if (this.opts.allowClear) {
15571549
this.clear();
15581550
}
15591551
killEvent(e);
15601552
return;
15611553
}
1562-
1554+
}));
1555+
selection.bind("keypress", this.bind(function(e) {
1556+
var key = String.fromCharCode(e.which);
1557+
this.search.val(key);
15631558
this.open();
1564-
1565-
if (e.which === KEY.ENTER) {
1566-
// do not propagate the event otherwise we open, and propagate enter which closes
1567-
killEvent(e);
1568-
return;
1569-
}
1570-
1571-
// do not set the search input value for non-alpha-numeric keys
1572-
// otherwise pressing down results in a '(' being set in the search field
1573-
if (e.which < 48 ) { // '0' == 48
1574-
killEvent(e);
1575-
return;
1576-
}
1577-
1578-
var keyWritten = String.fromCharCode(e.which).toLowerCase();
1579-
1580-
if (e.shiftKey) {
1581-
keyWritten = keyWritten.toUpperCase();
1582-
}
1583-
1584-
// focus the field before calling val so the cursor ends up after the value instead of before
1585-
this.search.focus();
1586-
this.search.val(keyWritten);
1587-
1588-
// prevent event propagation so it doesnt replay on the now focussed search field and result in double key entry
1589-
killEvent(e);
15901559
}));
15911560

15921561
this.setPlaceholder();

0 commit comments

Comments
 (0)