Skip to content

Commit 31ebe7e

Browse files
committed
Spinner: Allow strings for min, max, and step options, converting immediately to numbers based on numberFormat and culture.
1 parent f638207 commit 31ebe7e

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

tests/unit/spinner/spinner_options.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,56 @@ test( "max", function() {
144144
equals( element.val(), 1000, "value not constrained on init" );
145145

146146
element.spinner( "value", 1000 );
147-
equals( element.val(), 100, "max constrained if value method is greater" );
147+
equals( element.val(), 100, "max constrained in value method" );
148148

149149
element.val( 1000 ).blur();
150150
equals( element.val(), 1000, "max not constrained if manual entry" );
151151
});
152152

153+
test( "max, string", function() {
154+
expect( 3 );
155+
var element = $( "#spin" )
156+
.val( 1000 )
157+
.spinner({
158+
max: "$100.00",
159+
numberFormat: "C",
160+
culture: "en"
161+
});
162+
equals( element.val(), "$1,000.00", "value not constrained on init" );
163+
equals( element.spinner( "option", "max" ), 100, "option converted to number" );
164+
165+
element.spinner( "value", 1000 );
166+
equals( element.val(), "$100.00", "max constrained in value method" );
167+
});
168+
153169
test( "min", function() {
154170
expect( 3 );
155171
var element = $( "#spin" ).val( -1000 ).spinner({ min: -100 });
156172
equals( element.val(), -1000, "value not constrained on init" );
157173

158174
element.spinner( "value", -1000 );
159-
equals( element.val(), -100, "min constrained if value method is greater" );
175+
equals( element.val(), -100, "min constrained in value method" );
160176

161177
element.val( -1000 ).blur();
162178
equals( element.val(), -1000, "min not constrained if manual entry" );
163179
});
164180

181+
test( "min, string", function() {
182+
expect( 3 );
183+
var element = $( "#spin" )
184+
.val( -1000 )
185+
.spinner({
186+
min: "-$100.00",
187+
numberFormat: "C",
188+
culture: "en"
189+
});
190+
equals( element.val(), "($1,000.00)", "value not constrained on init" );
191+
equals( element.spinner( "option", "min" ), -100, "option converted to number" );
192+
193+
element.spinner( "value", -1000 );
194+
equals( element.val(), "($100.00)", "min constrained in value method")
195+
});
196+
165197
test( "step, 2", function() {
166198
expect( 3 );
167199
var element = $( "#spin" ).val( 0 ).spinner({ step: 2 });
@@ -187,4 +219,18 @@ test( "step, 0.7", function() {
187219
equals( element.val(), "0.7", "stepUp" );
188220
});
189221

222+
test( "step, string", function() {
223+
expect( 2 );
224+
var element = $("#spin").val( 0 ).spinner({
225+
step: "$0.70",
226+
numberFormat: "C",
227+
culture: "en"
228+
});
229+
230+
equals( element.spinner( "option", "step" ), 0.7, "option converted to number" );
231+
232+
element.spinner( "stepUp" );
233+
equals( element.val(), "$0.70", "stepUp" );
234+
});
235+
190236
})( jQuery );

ui/jquery.ui.spinner.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ $.widget( "ui.spinner", {
4545
},
4646

4747
_create: function() {
48+
// handle string values that need to be parsed
49+
this._setOption( "max", this.options.max );
50+
this._setOption( "min", this.options.min );
51+
this._setOption( "step", this.options.step );
52+
53+
// format the value, but don't constrain
4854
this._value( this.element.val(), true );
55+
4956
this._draw();
5057
this._bind( this._events );
5158
this._refresh();
@@ -318,6 +325,12 @@ $.widget( "ui.spinner", {
318325
return;
319326
}
320327

328+
if ( key === "max" || key === "min" || key === "step" ) {
329+
if ( typeof value === "string" ) {
330+
value = this._parse( value );
331+
}
332+
}
333+
321334
this._super( key, value );
322335

323336
if ( key === "disabled" ) {

0 commit comments

Comments
 (0)