@@ -2,7 +2,7 @@ import * as React from '@theia/core/shared/react';
22import classnames from 'classnames' ;
33
44interface SettingsStepInputProps {
5- value : number ;
5+ initialValue : number ;
66 setSettingsStateValue : ( value : number ) => void ;
77 step : number ;
88 maxValue : number ;
@@ -15,7 +15,7 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
1515 props : SettingsStepInputProps
1616) => {
1717 const {
18- value ,
18+ initialValue ,
1919 setSettingsStateValue,
2020 step,
2121 maxValue,
@@ -24,7 +24,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
2424 classNames,
2525 } = props ;
2626
27- const [ tempValue , setTempValue ] = React . useState < string > ( String ( value ) ) ;
27+ const [ valueState , setValueState ] = React . useState < {
28+ currentValue : number ;
29+ isEmptyString : boolean ;
30+ } > ( {
31+ currentValue : initialValue ,
32+ isEmptyString : false ,
33+ } ) ;
34+ const { currentValue, isEmptyString } = valueState ;
2835
2936 const clamp = ( value : number , min : number , max : number ) : number => {
3037 return Math . min ( Math . max ( value , min ) , max ) ;
@@ -34,10 +41,11 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
3441 roundingOperation : 'ceil' | 'floor' ,
3542 stepOperation : ( a : number , b : number ) => number
3643 ) : void => {
37- const valueRoundedToScale = Math [ roundingOperation ] ( value / step ) * step ;
44+ const valueRoundedToScale =
45+ Math [ roundingOperation ] ( currentValue / step ) * step ;
3846 const calculatedValue =
39- valueRoundedToScale === value
40- ? stepOperation ( value , step )
47+ valueRoundedToScale === currentValue
48+ ? stepOperation ( currentValue , step )
4149 : valueRoundedToScale ;
4250 const newValue = clamp ( calculatedValue , minValue , maxValue ) ;
4351
@@ -54,41 +62,54 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
5462
5563 const onUserInput = ( event : React . ChangeEvent < HTMLInputElement > ) : void => {
5664 const { value : eventValue } = event . target ;
57- setTempValue ( eventValue ) ;
65+ setValueState ( {
66+ currentValue : Number ( eventValue ) ,
67+ isEmptyString : eventValue === '' ,
68+ } ) ;
5869 } ;
5970
6071 /* Prevent the user from entering invalid values */
6172 const onBlur = ( ) : void => {
62- const tempValueAsNumber = Number ( tempValue ) ;
63-
64- /* If the user input is not a number, reset the input to the previous value */
65- if ( isNaN ( tempValueAsNumber ) || tempValue === '' ) {
66- setTempValue ( String ( value ) ) ;
73+ if ( ( currentValue === 0 && minValue > 0 ) || isNaN ( currentValue ) ) {
74+ setValueState ( {
75+ currentValue : initialValue ,
76+ isEmptyString : false ,
77+ } ) ;
6778 return ;
6879 }
69- if ( tempValueAsNumber !== value ) {
80+
81+ if ( currentValue !== initialValue ) {
7082 /* If the user input is a number, clamp it to the min and max values */
71- const newValue = clamp ( tempValueAsNumber , minValue , maxValue ) ;
83+ const newValue = clamp ( currentValue , minValue , maxValue ) ;
7284
7385 setSettingsStateValue ( newValue ) ;
74- setTempValue ( String ( newValue ) ) ;
7586 }
7687 } ;
7788
78- const upDisabled = value >= maxValue ;
79- const downDisabled = value <= minValue ;
89+ const valueIsNotWithinRange =
90+ currentValue < minValue || currentValue > maxValue ;
91+ const isDisabledException =
92+ valueIsNotWithinRange || isEmptyString || isNaN ( currentValue ) ;
93+
94+ const upDisabled = isDisabledException || currentValue >= maxValue ;
95+ const downDisabled = isDisabledException || currentValue <= minValue ;
8096
8197 return (
8298 < div className = "settings-step-input-container" >
8399 < input
84100 className = { classnames ( 'settings-step-input-element' , classNames ?. input ) }
85- value = { tempValue . toString ( ) }
101+ value = { isEmptyString ? '' : String ( currentValue ) }
86102 onChange = { onUserInput }
87103 onBlur = { onBlur }
88104 type = "number"
89105 pattern = "[0-9]+"
90106 />
91- < div className = "settings-step-input-buttons-container" >
107+ < div
108+ className = { classnames (
109+ 'settings-step-input-buttons-container' ,
110+ classNames ?. buttonsContainer
111+ ) }
112+ >
92113 < button
93114 className = "settings-step-input-button settings-step-input-up-button"
94115 disabled = { upDisabled }
0 commit comments