]> source.dussan.org Git - jquery-ui.git/commitdiff
Datepicker: Add `value()` and `valueAsDate()` methods
authorTJ VanToll <tj.vantoll@gmail.com>
Thu, 21 Nov 2013 14:03:08 +0000 (09:03 -0500)
committerScott González <scott.gonzalez@gmail.com>
Thu, 29 Jan 2015 22:47:48 +0000 (17:47 -0500)
tests/unit/datepicker/datepicker_methods.js
ui/datepicker.js

index e204b804544526161e4bf203c50d66f0fb233d7b..dcd6dc2c7aa9654ee3b9a9465682b8fec689026a 100644 (file)
@@ -64,7 +64,51 @@ test( "open", function() {
 });
 
 test( "value", function() {
-       expect( 0 );
+       expect( 5 );
+       var input = $( "#datepicker" ).datepicker(),
+               picker = input.datepicker( "widget" ),
+               inline = $( "#inline" ).datepicker();
+
+       input.datepicker( "value", "1/1/2014" );
+       equal( input.val(), "1/1/2014", "input's value set" );
+       ok( picker.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
+               "first day marked as selected" );
+       equal( input.datepicker( "value" ), "1/1/2014", "getter" );
+
+       inline.datepicker( "value", "1/1/2014" );
+       ok( inline.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
+               "first day marked as selected" );
+       equal( inline.datepicker( "value" ), "1/1/2014", "getter" );
+
+       // TODO: Handle for invalid values.
+
+       input.datepicker( "destroy" );
+       inline.datepicker( "destroy" );
+});
+
+test( "valueAsDate", function() {
+       expect( 5 );
+       var input = $( "#datepicker" ).datepicker(),
+               picker = input.datepicker( "widget" ),
+               inline = $( "#inline" ).datepicker();
+
+       input.datepicker( "valueAsDate", new Date( 2014, 0, 1 ) );
+       equal( input.val(), "1/1/2014", "input's value set" );
+       ok( picker.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
+               "first day marked as selected" );
+       TestHelpers.datepicker.equalsDate( input.datepicker( "valueAsDate" ),
+               new Date( 2014, 0, 1 ), "getter" );
+
+       inline.datepicker( "valueAsDate", new Date( 2014, 0, 1 ) );
+       ok( inline.find( "a[data-timestamp]:first" ).hasClass( "ui-state-focus" ),
+               "first day marked as selected" );
+       TestHelpers.datepicker.equalsDate( inline.datepicker( "valueAsDate" ),
+               new Date( 2014, 0, 1 ), "getter" );
+
+       // TODO: Handle for invalid values.
+
+       input.datepicker( "destroy" );
+       inline.datepicker( "destroy" );
 });
 
 })( jQuery );
index 9d8813a29f5bbc8badc716507912319214b3fa17..425e81a727c37a6225bf92d2ce1b5a6b56505e86 100644 (file)
@@ -609,6 +609,27 @@ $.widget( "ui.datepicker", {
                        date: this.date.format()
                });
        },
+       _value: function( value ) {
+               this.date.setTime( value ).select();
+               if ( !this.inline ) {
+                       this.element.val( this.date.format() );
+               }
+               this.refresh();
+       },
+       value: function( value ) {
+               if ( arguments.length ) {
+                       this._value( value );
+               } else {
+                       return this.date.format();
+               }
+       },
+       valueAsDate: function( value ) {
+               if ( arguments.length ) {
+                       this._value( value );
+               } else {
+                       return this.date.date();
+               }
+       },
        _destroy: function() {
                if ( this.inline ) {
                        this.picker.empty();