Skip to content

Commit e865e72

Browse files
Merge pull request trentrichardson#725 from emlun/redirect-focus-option
Add altRedirectFocus option
2 parents 08b50b3 + 66ba807 commit e865e72

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

.jshintrc

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010
"unused": true,
1111
"boss": true,
1212
"eqnull": true,
13-
"node": true,
14-
"es5": true
13+
"node": true
1514
}

src/docs/options.html

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ <h3>Alt Field Options</h3>
6868

6969
<dt>altTimeFormat</dt>
7070
<dd><em>Default: (timeFormat option)</em> - The time format to use with the altField.</dd>
71+
72+
<dt>altRedirectFocus</dt>
73+
<dd><em>Default: true</em> - Whether to immediately focus the main field whenever the altField receives focus. Effective at construction time only, changing it later has no effect.</dd>
7174
</dl>
7275

7376
<h3>Timezone Options</h3>

src/jquery-ui-timepicker-addon.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
altTimeFormat: null,
100100
altSeparator: null,
101101
altTimeSuffix: null,
102+
altRedirectFocus: true,
102103
pickerTimeFormat: null,
103104
pickerTimeSuffix: null,
104105
showTimepicker: true,
@@ -271,11 +272,14 @@
271272
tp_inst.$input = $input;
272273

273274
if (tp_inst._defaults.altField) {
274-
tp_inst.$altInput = $(tp_inst._defaults.altField).css({
275-
cursor: 'pointer'
276-
}).focus(function () {
277-
$input.trigger("focus");
278-
});
275+
tp_inst.$altInput = $(tp_inst._defaults.altField);
276+
if (tp_inst._defaults.altRedirectFocus === true) {
277+
tp_inst.$altInput.css({
278+
cursor: 'pointer'
279+
}).focus(function () {
280+
$input.trigger("focus");
281+
});
282+
}
279283
}
280284

281285
if (tp_inst._defaults.minDate === 0 || tp_inst._defaults.minDateTime === 0) {

test/.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"jQuery",
1616
"$",
1717
"QUnit",
18+
"jasmine",
1819
"module",
1920
"test",
2021
"asyncTest",

test/jquery-ui-timepicker-addon_spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -661,4 +661,39 @@ describe('datetimepicker', function() {
661661
});
662662
});
663663
});
664+
665+
describe('altField', function() {
666+
var $input;
667+
var $altField;
668+
var inputFocusSpy;
669+
670+
beforeEach(function() {
671+
$input = affix('input');
672+
$altField = affix('input');
673+
674+
inputFocusSpy = jasmine.createSpy();
675+
$input.focus(inputFocusSpy);
676+
});
677+
678+
it('should redirect focus to main field', function() {
679+
$input.datetimepicker({
680+
showOn: 'button',
681+
altField: $altField,
682+
});
683+
684+
$altField.trigger('focus');
685+
expect(inputFocusSpy).toHaveBeenCalled();
686+
});
687+
688+
it('should not redirect focus to main field if altRedirectFocus is false', function() {
689+
$input.datetimepicker({
690+
showOn: 'button',
691+
altField: $altField,
692+
altRedirectFocus: false,
693+
});
694+
695+
$altField.trigger('focus');
696+
expect(inputFocusSpy).not.toHaveBeenCalled();
697+
});
698+
});
664699
});

0 commit comments

Comments
 (0)