@@ -16,64 +16,30 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
1616 const { value, setSettingsStateValue, step, maxValue, minValue, classNames } =
1717 props ;
1818
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 ) ;
3421 } ;
3522
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 ;
3828 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 ) ;
4633
4734 setSettingsStateValue ( newValue ) ;
4835 } ;
4936
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 ) ;
7339 } ;
7440
75- const disableStepDown = ( ) : void => {
76- setStepDownDisabled ( true ) ;
41+ const onStepDown = ( ) : void => {
42+ onStep ( 'floor' , ( a : number , b : number ) => a - b ) ;
7743 } ;
7844
7945 const onUserInput = ( event : React . ChangeEvent < HTMLInputElement > ) : void => {
@@ -86,34 +52,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
8652 const number = Number ( eventValue ) ;
8753
8854 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 ) ;
10556
10657 setSettingsStateValue ( newValue ) ;
10758 }
10859 } ;
10960
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 ;
11763
11864 return (
11965 < div className = "settings-step-input-container" >
@@ -127,14 +73,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
12773 < div className = "settings-step-input-buttons-container" >
12874 < button
12975 className = "settings-step-input-button settings-step-input-up-button"
130- disabled = { stepUpDisabled }
76+ disabled = { upDisabled }
13177 onClick = { onStepUp }
13278 >
13379 ▾
13480 </ button >
13581 < button
13682 className = "settings-step-input-button"
137- disabled = { stepDownDisabled }
83+ disabled = { downDisabled }
13884 onClick = { onStepDown }
13985 >
14086 ▾
0 commit comments