@@ -16,64 +16,30 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
16
16
const { value, setSettingsStateValue, step, maxValue, minValue, classNames } =
17
17
props ;
18
18
19
- const [ stepUpDisabled , setStepUpDisabled ] = React . useState ( false ) ;
20
- const [ stepDownDisabled , setStepDownDisabled ] = React . useState ( false ) ;
21
-
22
- const onStepUp = ( ) : void => {
23
- const valueRoundedToScale = Math . ceil ( value / step ) * step ;
24
- const calculatedValue =
25
- valueRoundedToScale === value ? value + step : valueRoundedToScale ;
26
- const newValue = limitValueByCondition (
27
- calculatedValue ,
28
- maxValue ,
29
- calculatedValue >= maxValue ,
30
- disableStepUp
31
- ) ;
32
-
33
- setSettingsStateValue ( newValue ) ;
19
+ const clamp = ( value : number , min : number , max : number ) : number => {
20
+ return Math . min ( Math . max ( value , min ) , max ) ;
34
21
} ;
35
22
36
- const onStepDown = ( ) : void => {
37
- const valueRoundedToScale = Math . floor ( value / step ) * step ;
23
+ const onStep = (
24
+ roundingOperation : 'ceil' | 'floor' ,
25
+ stepOperation : ( a : number , b : number ) => number
26
+ ) : void => {
27
+ const valueRoundedToScale = Math [ roundingOperation ] ( value / step ) * step ;
38
28
const calculatedValue =
39
- valueRoundedToScale === value ? value - step : valueRoundedToScale ;
40
- const newValue = limitValueByCondition (
41
- calculatedValue ,
42
- minValue ,
43
- calculatedValue <= minValue ,
44
- disableStepDown
45
- ) ;
29
+ valueRoundedToScale === value
30
+ ? stepOperation ( value , step )
31
+ : valueRoundedToScale ;
32
+ const newValue = clamp ( calculatedValue , minValue , maxValue ) ;
46
33
47
34
setSettingsStateValue ( newValue ) ;
48
35
} ;
49
36
50
- const limitValueByCondition = (
51
- calculatedValue : number ,
52
- limitedValue : number ,
53
- condition : boolean ,
54
- onConditionTrue : ( ) => void ,
55
- onConditionFalse = enableButtons
56
- ) : number => {
57
- if ( condition ) {
58
- onConditionTrue ( ) ;
59
- return limitedValue ;
60
- } else {
61
- onConditionFalse ( ) ;
62
- return calculatedValue ;
63
- }
64
- } ;
65
-
66
- const enableButtons = ( ) : void => {
67
- setStepUpDisabled ( false ) ;
68
- setStepDownDisabled ( false ) ;
69
- } ;
70
-
71
- const disableStepUp = ( ) : void => {
72
- setStepUpDisabled ( true ) ;
37
+ const onStepUp = ( ) : void => {
38
+ onStep ( 'ceil' , ( a : number , b : number ) => a + b ) ;
73
39
} ;
74
40
75
- const disableStepDown = ( ) : void => {
76
- setStepDownDisabled ( true ) ;
41
+ const onStepDown = ( ) : void => {
42
+ onStep ( 'floor' , ( a : number , b : number ) => a - b ) ;
77
43
} ;
78
44
79
45
const onUserInput = ( event : React . ChangeEvent < HTMLInputElement > ) : void => {
@@ -86,34 +52,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
86
52
const number = Number ( eventValue ) ;
87
53
88
54
if ( ! isNaN ( number ) && number !== value ) {
89
- let newValue ;
90
- if ( number > value ) {
91
- newValue = limitValueByCondition (
92
- number ,
93
- maxValue ,
94
- number >= maxValue ,
95
- disableStepUp
96
- ) ;
97
- } else {
98
- newValue = limitValueByCondition (
99
- number ,
100
- minValue ,
101
- number <= minValue ,
102
- disableStepDown
103
- ) ;
104
- }
55
+ const newValue = clamp ( number , minValue , maxValue ) ;
105
56
106
57
setSettingsStateValue ( newValue ) ;
107
58
}
108
59
} ;
109
60
110
- // the component does not unmount when we close the settings dialog
111
- // in theia which necessitates the below useEffect
112
- React . useEffect ( ( ) => {
113
- if ( value > minValue && value < maxValue ) {
114
- enableButtons ( ) ;
115
- }
116
- } , [ value , minValue , maxValue ] ) ;
61
+ const upDisabled = value >= maxValue ;
62
+ const downDisabled = value <= minValue ;
117
63
118
64
return (
119
65
< div className = "settings-step-input-container" >
@@ -127,14 +73,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
127
73
< div className = "settings-step-input-buttons-container" >
128
74
< button
129
75
className = "settings-step-input-button settings-step-input-up-button"
130
- disabled = { stepUpDisabled }
76
+ disabled = { upDisabled }
131
77
onClick = { onStepUp }
132
78
>
133
79
▾
134
80
</ button >
135
81
< button
136
82
className = "settings-step-input-button"
137
- disabled = { stepDownDisabled }
83
+ disabled = { downDisabled }
138
84
onClick = { onStepDown }
139
85
>
140
86
▾
0 commit comments