From: Scott González Date: Sat, 7 Jan 2012 14:12:27 +0000 (-0500) Subject: Spinner: Allow strings for min, max, and step options, converting immediately to... X-Git-Tag: 1.9m7~109 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=31ebe7e7da7d7ce737a1feb3b19dec047e88a3a5;p=jquery-ui.git Spinner: Allow strings for min, max, and step options, converting immediately to numbers based on numberFormat and culture. --- diff --git a/tests/unit/spinner/spinner_options.js b/tests/unit/spinner/spinner_options.js index 81f434fc6..0be59bd07 100644 --- a/tests/unit/spinner/spinner_options.js +++ b/tests/unit/spinner/spinner_options.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 ); diff --git a/ui/jquery.ui.spinner.js b/ui/jquery.ui.spinner.js index b78b4dbea..c99520b7c 100644 --- a/ui/jquery.ui.spinner.js +++ b/ui/jquery.ui.spinner.js @@ -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" ) {