Skip to content

Commit 2b22bbd

Browse files
committed
Spinner: Only trigger change when the field has blurred and the value has changed.
1 parent 08a7a11 commit 2b22bbd

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

tests/unit/spinner/spinner_events.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ test("change", function() {
5757
});
5858

5959
simulateKeyDownUp(el, $.ui.keyCode.UP);
60+
el.blur();
6061

6162
equals(change, 1, "Change triggered");
6263
});

ui/jquery.ui.spinner.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,10 @@ $.widget( "ui.spinner", {
7272
event.preventDefault();
7373
}
7474
},
75-
keyup: function( event ) {
76-
if ( this.spinning ) {
77-
this._stop( event );
78-
this._change( event );
79-
}
80-
},
75+
keyup: "_stop",
8176
focus: function() {
8277
uiSpinner.addClass( "ui-state-active" );
78+
this.previous = this.options.value;
8379
},
8480
blur: function( event ) {
8581
// don't clear invalid values on blur
@@ -90,6 +86,9 @@ $.widget( "ui.spinner", {
9086
this.element.val( value );
9187
}
9288
uiSpinner.removeClass( "ui-state-active" );
89+
if ( this.previous !== this.options.value ) {
90+
this._trigger( "change", event );
91+
}
9392
}
9493
});
9594

@@ -112,13 +111,7 @@ $.widget( "ui.spinner", {
112111

113112
this._repeat( null, $( event.currentTarget ).hasClass( "ui-spinner-up" ) ? 1 : -1, event );
114113
},
115-
mouseup: function( event ) {
116-
if ( this.spinning ) {
117-
this._stop( event );
118-
// TODO: don't trigger change until the field is blurred
119-
this._change( event );
120-
}
121-
},
114+
mouseup: "_stop",
122115
mouseenter: function( event ) {
123116
// button will add ui-state-active if mouse was down while mouseleave and kept down
124117
if ( !$( event.currentTarget ).hasClass( "ui-state-active" ) ) {
@@ -133,12 +126,7 @@ $.widget( "ui.spinner", {
133126
// TODO: do we really want to consider this a stop?
134127
// shouldn't we just stop the repeater and wait until mouseup before
135128
// we trigger the stop event?
136-
mouseleave: function( event ) {
137-
if ( this.spinning ) {
138-
this._stop( event );
139-
this._change( event );
140-
}
141-
}
129+
mouseleave: "_stop"
142130
});
143131

144132
// disable spinner if element was already disabled
@@ -186,12 +174,10 @@ $.widget( "ui.spinner", {
186174
}
187175

188176
this._spin( (delta > 0 ? 1 : -1) * this.options.step, event );
189-
clearTimeout( this.timeout );
190-
this.timeout = setTimeout(function() {
177+
clearTimeout( this.mousewheelTimer );
178+
this.mousewheelTimer = setTimeout(function() {
191179
if ( this.spinning ) {
192180
this._stop( event );
193-
// TODO: don't trigger change until the field is blurred
194-
this._change( event );
195181
}
196182
}, 100 );
197183
event.preventDefault();
@@ -276,16 +262,17 @@ $.widget( "ui.spinner", {
276262
},
277263

278264
_stop: function( event ) {
265+
if ( !this.spinning ) {
266+
return;
267+
}
268+
279269
clearTimeout( this.timer );
270+
clearTimeout( this.mousewheelTimer );
280271
this.counter = 0;
281272
this.spinning = false;
282273
this._trigger( "stop", event );
283274
},
284275

285-
_change: function( event ) {
286-
this._trigger( "change", event );
287-
},
288-
289276
_setOption: function( key, value ) {
290277
if ( key === "value") {
291278
value = this._trimValue( this._parse(value) );

0 commit comments

Comments
 (0)