Skip to content

Commit 226cc3e

Browse files
committed
Dialog: Honor preventDefault when managing focus
If event.isDefaultPrevented() is true, the focus management is completely skipped, assuming the user manages focus manually. Fixes #10103 Closes jquerygh-1265
1 parent a0b8476 commit 226cc3e

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

tests/unit/dialog/dialog_core.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test("widget method", function() {
4141
});
4242

4343
asyncTest( "focus tabbable", function() {
44-
expect( 6 );
44+
expect( 8 );
4545
var element,
4646
options = {
4747
buttons: [{
@@ -118,7 +118,30 @@ asyncTest( "focus tabbable", function() {
118118
setTimeout(function() {
119119
equal( document.activeElement, element.parent()[ 0 ], "6. the dialog itself" );
120120
element.remove();
121-
start();
121+
setTimeout( step7 );
122+
});
123+
}
124+
125+
function step7() {
126+
element = $( "<div><input name='0'><input name='1' autofocus></div>" ).dialog({
127+
open: function() {
128+
var inputs = $( this ).find( "input" );
129+
inputs.last().keydown(function( event ) {
130+
event.preventDefault();
131+
inputs.first().focus();
132+
});
133+
}
134+
});
135+
setTimeout(function() {
136+
var inputs = element.find( "input" );
137+
equal( document.activeElement, inputs[ 1 ], "Focus starts on second input" );
138+
inputs.last().simulate( "keydown", { keyCode: $.ui.keyCode.TAB });
139+
setTimeout(function() {
140+
equal( document.activeElement, inputs[ 0 ],
141+
"Honor preventDefault, allowing custom focus management" );
142+
element.remove();
143+
start();
144+
}, 50 );
122145
});
123146
}
124147

ui/dialog.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ return $.widget( "ui.dialog", {
334334
}
335335

336336
// prevent tabbing out of dialogs
337-
if ( event.keyCode !== $.ui.keyCode.TAB ) {
337+
if ( event.keyCode !== $.ui.keyCode.TAB || event.isDefaultPrevented() ) {
338338
return;
339339
}
340340
var tabbables = this.uiDialog.find( ":tabbable" ),

0 commit comments

Comments
 (0)