Skip to content

Commit 4637695

Browse files
committed
Autocomplete multiple demos: Don't leave the field when tabbing while the menu is open. Fixes #6661 - Autocomplete: Tab on multiple Autocomplete should not change focus after selecting.
1 parent ff4154b commit 4637695

File tree

2 files changed

+66
-50
lines changed

2 files changed

+66
-50
lines changed

demos/autocomplete/multiple-remote.html

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,43 @@
2222
return split( term ).pop();
2323
}
2424

25-
$( "#birds" ).autocomplete({
26-
source: function( request, response ) {
27-
$.getJSON( "search.php", {
28-
term: extractLast( request.term )
29-
}, response );
30-
},
31-
search: function() {
32-
// custom minLength
33-
var term = extractLast( this.value );
34-
if ( term.length < 2 ) {
25+
$( "#birds" )
26+
// don't navigate away from the field on tab when selecting an item
27+
.bind( "keydown", function( event ) {
28+
if ( event.keyCode === $.ui.keyCode.TAB &&
29+
$( this ).data( "autocomplete" ).menu.active ) {
30+
event.preventDefault();
31+
}
32+
})
33+
.autocomplete({
34+
source: function( request, response ) {
35+
$.getJSON( "search.php", {
36+
term: extractLast( request.term )
37+
}, response );
38+
},
39+
search: function() {
40+
// custom minLength
41+
var term = extractLast( this.value );
42+
if ( term.length < 2 ) {
43+
return false;
44+
}
45+
},
46+
focus: function() {
47+
// prevent value inserted on focus
48+
return false;
49+
},
50+
select: function( event, ui ) {
51+
var terms = split( this.value );
52+
// remove the current input
53+
terms.pop();
54+
// add the selected item
55+
terms.push( ui.item.value );
56+
// add placeholder to get the comma-and-space at the end
57+
terms.push( "" );
58+
this.value = terms.join( ", " );
3559
return false;
3660
}
37-
},
38-
focus: function() {
39-
// prevent value inserted on focus
40-
return false;
41-
},
42-
select: function( event, ui ) {
43-
var terms = split( this.value );
44-
// remove the current input
45-
terms.pop();
46-
// add the selected item
47-
terms.push( ui.item.value );
48-
// add placeholder to get the comma-and-space at the end
49-
terms.push( "" );
50-
this.value = terms.join( ", " );
51-
return false;
52-
}
53-
});
61+
});
5462
});
5563
</script>
5664
</head>

demos/autocomplete/multiple.html

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,37 @@
4343
return split( term ).pop();
4444
}
4545

46-
$( "#tags" ).autocomplete({
47-
minLength: 0,
48-
source: function( request, response ) {
49-
// delegate back to autocomplete, but extract the last term
50-
response( $.ui.autocomplete.filter(
51-
availableTags, extractLast( request.term ) ) );
52-
},
53-
focus: function() {
54-
// prevent value inserted on focus
55-
return false;
56-
},
57-
select: function( event, ui ) {
58-
var terms = split( this.value );
59-
// remove the current input
60-
terms.pop();
61-
// add the selected item
62-
terms.push( ui.item.value );
63-
// add placeholder to get the comma-and-space at the end
64-
terms.push( "" );
65-
this.value = terms.join( ", " );
66-
return false;
67-
}
68-
});
46+
$( "#tags" )
47+
// don't navigate away from the field on tab when selecting an item
48+
.bind( "keydown", function( event ) {
49+
if ( event.keyCode === $.ui.keyCode.TAB &&
50+
$( this ).data( "autocomplete" ).menu.active ) {
51+
event.preventDefault();
52+
}
53+
})
54+
.autocomplete({
55+
minLength: 0,
56+
source: function( request, response ) {
57+
// delegate back to autocomplete, but extract the last term
58+
response( $.ui.autocomplete.filter(
59+
availableTags, extractLast( request.term ) ) );
60+
},
61+
focus: function() {
62+
// prevent value inserted on focus
63+
return false;
64+
},
65+
select: function( event, ui ) {
66+
var terms = split( this.value );
67+
// remove the current input
68+
terms.pop();
69+
// add the selected item
70+
terms.push( ui.item.value );
71+
// add placeholder to get the comma-and-space at the end
72+
terms.push( "" );
73+
this.value = terms.join( ", " );
74+
return false;
75+
}
76+
});
6977
});
7078
</script>
7179
</head>

0 commit comments

Comments
 (0)