From 31ebe7e7da7d7ce737a1feb3b19dec047e88a3a5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Scott=20Gonz=C3=A1lez?= Date: Sat, 7 Jan 2012 09:12:27 -0500 Subject: [PATCH] Spinner: Allow strings for min, max, and step options, converting immediately to numbers based on numberFormat and culture. --- tests/unit/spinner/spinner_options.js | 50 +++++++++++++++++++++++++-- ui/jquery.ui.spinner.js | 13 +++++++ 2 files changed, 61 insertions(+), 2 deletions(-) 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" ) { -- 2.39.5