Skip to content
Permalink
Browse files

Spinner: Allow strings for min, max, and step options, converting imm…

…ediately to numbers based on numberFormat and culture.
  • Loading branch information...
scottgonzalez committed Jan 7, 2012
1 parent f638207 commit 31ebe7e7da7d7ce737a1feb3b19dec047e88a3a5
Showing with 61 additions and 2 deletions.
  1. +48 −2 tests/unit/spinner/spinner_options.js
  2. +13 −0 ui/jquery.ui.spinner.js
@@ -144,24 +144,56 @@ test( "max", function() {
equals( element.val(), 1000, "value not constrained on init" );

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();
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() {
expect( 3 );
var element = $( "#spin" ).val( -1000 ).spinner({ min: -100 });
equals( element.val(), -1000, "value not constrained on init" );

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();
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() {
expect( 3 );
var element = $( "#spin" ).val( 0 ).spinner({ step: 2 });
@@ -187,4 +219,18 @@ test( "step, 0.7", function() {
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 );
@@ -45,7 +45,14 @@ $.widget( "ui.spinner", {
},

_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._draw();
this._bind( this._events );
this._refresh();
@@ -318,6 +325,12 @@ $.widget( "ui.spinner", {
return;
}

if ( key === "max" || key === "min" || key === "step" ) {
if ( typeof value === "string" ) {
value = this._parse( value );
}
}

this._super( key, value );

if ( key === "disabled" ) {

0 comments on commit 31ebe7e

Please sign in to comment.
You can’t perform that action at this time.