@@ -24,6 +24,8 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
24
24
classNames,
25
25
} = props ;
26
26
27
+ const [ tempValue , setTempValue ] = React . useState < string > ( String ( value ) ) ;
28
+
27
29
const clamp = ( value : number , min : number , max : number ) : number => {
28
30
return Math . min ( Math . max ( value , min ) , max ) ;
29
31
} ;
@@ -52,17 +54,24 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
52
54
53
55
const onUserInput = ( event : React . ChangeEvent < HTMLInputElement > ) : void => {
54
56
const { value : eventValue } = event . target ;
57
+ setTempValue ( eventValue ) ;
58
+ } ;
55
59
56
- if ( eventValue === '' ) {
57
- setSettingsStateValue ( 0 ) ;
58
- }
59
-
60
- const number = Number ( eventValue ) ;
60
+ /* Prevent the user from entering invalid values */
61
+ const onBlur = ( ) : void => {
62
+ const tempValueAsNumber = Number ( tempValue ) ;
61
63
62
- if ( ! isNaN ( number ) && number !== value ) {
63
- const newValue = clamp ( number , minValue , maxValue ) ;
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 ) ) ;
67
+ return ;
68
+ }
69
+ if ( tempValueAsNumber !== value ) {
70
+ /* If the user input is a number, clamp it to the min and max values */
71
+ const newValue = clamp ( tempValueAsNumber , minValue , maxValue ) ;
64
72
65
73
setSettingsStateValue ( newValue ) ;
74
+ setTempValue ( String ( newValue ) ) ;
66
75
}
67
76
} ;
68
77
@@ -73,8 +82,9 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
73
82
< div className = "settings-step-input-container" >
74
83
< input
75
84
className = { classnames ( 'settings-step-input-element' , classNames ?. input ) }
76
- value = { value . toString ( ) }
85
+ value = { tempValue . toString ( ) }
77
86
onChange = { onUserInput }
87
+ onBlur = { onBlur }
78
88
type = "number"
79
89
pattern = "[0-9]+"
80
90
/>
0 commit comments