Spinner: Allow strings for min, max, and step options, converting immediately to numbers based on numberFormat and culture.

This commit is contained in:
Scott González 2012-01-07 09:12:27 -05:00
parent f63820769d
commit 31ebe7e7da
2 changed files with 61 additions and 2 deletions

View File

@ -144,24 +144,56 @@ test( "max", function() {
equals( element.val(), 1000, "value not constrained on init" ); equals( element.val(), 1000, "value not constrained on init" );
element.spinner( "value", 1000 ); element.spinner( "value", 1000 );
equals( element.val(), 100, "max constrained if value method is greater" ); equals( element.val(), 100, "max constrained in value method" );
element.val( 1000 ).blur(); element.val( 1000 ).blur();
equals( element.val(), 1000, "max not constrained if manual entry" ); equals( element.val(), 1000, "max not constrained if manual entry" );
}); });
test( "max, string", function() {
expect( 3 );
var element = $( "#spin" )
.val( 1000 )
.spinner({
max: "$100.00",
numberFormat: "C",
culture: "en"
});
equals( element.val(), "$1,000.00", "value not constrained on init" );
equals( element.spinner( "option", "max" ), 100, "option converted to number" );
element.spinner( "value", 1000 );
equals( element.val(), "$100.00", "max constrained in value method" );
});
test( "min", function() { test( "min", function() {
expect( 3 ); expect( 3 );
var element = $( "#spin" ).val( -1000 ).spinner({ min: -100 }); var element = $( "#spin" ).val( -1000 ).spinner({ min: -100 });
equals( element.val(), -1000, "value not constrained on init" ); equals( element.val(), -1000, "value not constrained on init" );
element.spinner( "value", -1000 ); element.spinner( "value", -1000 );
equals( element.val(), -100, "min constrained if value method is greater" ); equals( element.val(), -100, "min constrained in value method" );
element.val( -1000 ).blur(); element.val( -1000 ).blur();
equals( element.val(), -1000, "min not constrained if manual entry" ); equals( element.val(), -1000, "min not constrained if manual entry" );
}); });
test( "min, string", function() {
expect( 3 );
var element = $( "#spin" )
.val( -1000 )
.spinner({
min: "-$100.00",
numberFormat: "C",
culture: "en"
});
equals( element.val(), "($1,000.00)", "value not constrained on init" );
equals( element.spinner( "option", "min" ), -100, "option converted to number" );
element.spinner( "value", -1000 );
equals( element.val(), "($100.00)", "min constrained in value method")
});
test( "step, 2", function() { test( "step, 2", function() {
expect( 3 ); expect( 3 );
var element = $( "#spin" ).val( 0 ).spinner({ step: 2 }); var element = $( "#spin" ).val( 0 ).spinner({ step: 2 });
@ -187,4 +219,18 @@ test( "step, 0.7", function() {
equals( element.val(), "0.7", "stepUp" ); equals( element.val(), "0.7", "stepUp" );
}); });
test( "step, string", function() {
expect( 2 );
var element = $("#spin").val( 0 ).spinner({
step: "$0.70",
numberFormat: "C",
culture: "en"
});
equals( element.spinner( "option", "step" ), 0.7, "option converted to number" );
element.spinner( "stepUp" );
equals( element.val(), "$0.70", "stepUp" );
});
})( jQuery ); })( jQuery );

View File

@ -45,7 +45,14 @@ $.widget( "ui.spinner", {
}, },
_create: function() { _create: function() {
// handle string values that need to be parsed
this._setOption( "max", this.options.max );
this._setOption( "min", this.options.min );
this._setOption( "step", this.options.step );
// format the value, but don't constrain
this._value( this.element.val(), true ); this._value( this.element.val(), true );
this._draw(); this._draw();
this._bind( this._events ); this._bind( this._events );
this._refresh(); this._refresh();
@ -318,6 +325,12 @@ $.widget( "ui.spinner", {
return; return;
} }
if ( key === "max" || key === "min" || key === "step" ) {
if ( typeof value === "string" ) {
value = this._parse( value );
}
}
this._super( key, value ); this._super( key, value );
if ( key === "disabled" ) { if ( key === "disabled" ) {