@@ -352,6 +352,23 @@ export class NumberComponent implements ControlValueAccessor {
352352 * Adds `step` to the current `value`.
353353 */
354354 onIncrement ( ) : void {
355+ // if max is set and value + step is greater than max, set value to max
356+ // example: max = 100, step = 10, value = 95 , value + step = 105, value will be set to 100 (max) instead of 105
357+ if ( this . max !== null && this . value + this . step > this . max ) {
358+ this . value = this . max ;
359+ this . emitChangeEvent ( ) ;
360+ return ;
361+ }
362+
363+ // if min is set and value + step is less than min, set value to min
364+ // example: min = 5, step = 2, value = 0, value + step = 2, value will be set to 5 (min) instead of 2
365+ if ( this . min !== null && this . value + this . step < this . min ) {
366+ this . value = this . min ;
367+ this . emitChangeEvent ( ) ;
368+ return ;
369+ }
370+
371+ // if max is not set or value + step is less than max, increment value by step
355372 if ( this . max === null || this . value + this . step <= this . max ) {
356373 this . value += this . step ;
357374 this . value = parseFloat ( this . value . toPrecision ( this . precision ) ) ;
@@ -363,6 +380,23 @@ export class NumberComponent implements ControlValueAccessor {
363380 * Subtracts `step` to the current `value`.
364381 */
365382 onDecrement ( ) : void {
383+ // if max is set and value - step is greater than max, set value to max
384+ // example: max = 15, step = 2, value = 20, value - step = 18, value will be set to 15 (max) instead of 18
385+ if ( this . max !== null && this . value - this . step > this . max ) {
386+ this . value = this . max ;
387+ this . emitChangeEvent ( ) ;
388+ return ;
389+ }
390+
391+ // if min is set and value - step is less than min, set value to min
392+ // example: min = 5, step = 2, value = 6, value - step = 4, value will be set to 5 (min) instead of 4
393+ if ( this . min !== null && this . value - this . step < this . min ) {
394+ this . value = this . min ;
395+ this . emitChangeEvent ( ) ;
396+ return ;
397+ }
398+
399+ // if min is not set or value - step is greater than min, decrement value by step
366400 if ( this . min === null || this . value - this . step >= this . min ) {
367401 this . value -= this . step ;
368402 this . value = parseFloat ( this . value . toPrecision ( this . precision ) ) ;
0 commit comments