Skip to content

Commit ef4000d

Browse files
committed
Datepicker: Fixed #3891 Autosize input field
1 parent d2bd01a commit ef4000d

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

tests/unit/datepicker/datepicker_options.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,44 @@ test('altField', function() {
509509
equals(alt.val(), '2008-06-04', 'Alt field - manual entry - not updated');
510510
});
511511

512+
test('autoSize', function() {
513+
var inp = init('#inp');
514+
equals(inp.attr('size'), 0, 'Auto size - default');
515+
inp.datepicker('option', 'autoSize', true);
516+
equals(inp.attr('size'), 10, 'Auto size - mm/dd/yy');
517+
inp.datepicker('option', 'dateFormat', 'm/d/yy');
518+
equals(inp.attr('size'), 10, 'Auto size - m/d/yy');
519+
inp.datepicker('option', 'dateFormat', 'D M d yy');
520+
equals(inp.attr('size'), 15, 'Auto size - D M d yy');
521+
inp.datepicker('option', 'dateFormat', 'DD, MM dd, yy');
522+
equals(inp.attr('size'), 29, 'Auto size - DD, MM dd, yy');
523+
inp.removeAttr('size');
524+
// French
525+
inp.datepicker('option', $.extend({autoSize: false}, $.datepicker.regional['fr']));
526+
equals(inp.attr('size'), 0, 'Auto size - fr - default');
527+
inp.datepicker('option', 'autoSize', true);
528+
equals(inp.attr('size'), 10, 'Auto size - fr - dd/mm/yy');
529+
inp.datepicker('option', 'dateFormat', 'm/d/yy');
530+
equals(inp.attr('size'), 10, 'Auto size - fr - m/d/yy');
531+
inp.datepicker('option', 'dateFormat', 'D M d yy');
532+
equals(inp.attr('size'), 15, 'Auto size - fr - D M d yy');
533+
inp.datepicker('option', 'dateFormat', 'DD, MM dd, yy');
534+
equals(inp.attr('size'), 28, 'Auto size - fr - DD, MM dd, yy');
535+
inp.removeAttr('size');
536+
// Hebrew
537+
inp.datepicker('option', $.extend({autoSize: false}, $.datepicker.regional['he']));
538+
equals(inp.attr('size'), 0, 'Auto size - he - default');
539+
inp.datepicker('option', 'autoSize', true);
540+
equals(inp.attr('size'), 10, 'Auto size - he - dd/mm/yy');
541+
inp.datepicker('option', 'dateFormat', 'm/d/yy');
542+
equals(inp.attr('size'), 10, 'Auto size - he - m/d/yy');
543+
inp.datepicker('option', 'dateFormat', 'D M d yy');
544+
equals(inp.attr('size'), 14, 'Auto size - he - D M d yy');
545+
inp.datepicker('option', 'dateFormat', 'DD, MM dd, yy');
546+
equals(inp.attr('size'), 23, 'Auto size - he - DD, MM dd, yy');
547+
inp.removeAttr('size');
548+
});
549+
512550
test('daylightSaving', function() {
513551
var inp = init('#inp');
514552
var dp = $('#ui-datepicker-div');

ui/ui.datepicker.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ function Datepicker() {
100100
altField: '', // Selector for an alternate field to store selected dates into
101101
altFormat: '', // The date format to use for the alternate field
102102
constrainInput: true, // The input is constrained by the current date format
103-
showButtonPanel: false // True to show button panel, false to not show it
103+
showButtonPanel: false, // True to show button panel, false to not show it
104+
autoSize: false // True to size the input for the date format, false to leave as is
104105
};
105106
$.extend(this._defaults, this.regional['']);
106107
this.dpDiv = $('<div id="' + this._mainDivId + '" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible"></div>');
@@ -206,9 +207,36 @@ $.extend(Datepicker.prototype, {
206207
}).bind("getData.datepicker", function(event, key) {
207208
return this._get(inst, key);
208209
});
210+
this._autoSize(inst);
209211
$.data(target, PROP_NAME, inst);
210212
},
211213

214+
/* Apply the maximum length for the date format. */
215+
_autoSize: function(inst) {
216+
if (this._get(inst, 'autoSize') && !inst.inline) {
217+
var date = new Date(2009, 12 - 1, 20); // Ensure double digits
218+
var dateFormat = this._get(inst, 'dateFormat');
219+
if (dateFormat.match(/[DM]/)) {
220+
var findMax = function(names) {
221+
var max = 0;
222+
var maxI = 0;
223+
for (var i = 0; i < names.length; i++) {
224+
if (names[i].length > max) {
225+
max = names[i].length;
226+
maxI = i;
227+
}
228+
}
229+
return maxI;
230+
};
231+
date.setMonth(findMax(this._get(inst, (dateFormat.match(/MM/) ?
232+
'monthNames' : 'monthNamesShort'))));
233+
date.setDate(findMax(this._get(inst, (dateFormat.match(/DD/) ?
234+
'dayNames' : 'dayNamesShort'))) + 20 - date.getDay());
235+
}
236+
inst.input.attr('size', this._formatDate(inst, date).length);
237+
}
238+
},
239+
212240
/* Attach an inline date picker to a div. */
213241
_inlineDatepicker: function(target, inst) {
214242
var divSpan = $(target);
@@ -395,6 +423,7 @@ $.extend(Datepicker.prototype, {
395423
}
396424
var date = this._getDateDatepicker(target);
397425
extendRemove(inst.settings, settings);
426+
this._autoSize(inst);
398427
this._setDateDatepicker(target, date);
399428
this._updateDatepicker(inst);
400429
}

0 commit comments

Comments
 (0)