1
1
import * as React from '@theia/core/shared/react' ;
2
2
import classnames from 'classnames' ;
3
+ import _ = require( 'lodash' ) ;
3
4
4
5
interface SettingsStepInputProps {
5
6
value : number ;
@@ -16,64 +17,26 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
16
17
const { value, setSettingsStateValue, step, maxValue, minValue, classNames } =
17
18
props ;
18
19
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 ) ;
34
- } ;
35
-
36
- const onStepDown = ( ) : void => {
37
- const valueRoundedToScale = Math . floor ( value / step ) * step ;
20
+ const onStep = (
21
+ roundingOperation : 'ceil' | 'floor' ,
22
+ stepOperation : ( a : number , b : number ) => number
23
+ ) : void => {
24
+ const valueRoundedToScale = Math [ roundingOperation ] ( value / step ) * step ;
38
25
const calculatedValue =
39
- valueRoundedToScale === value ? value - step : valueRoundedToScale ;
40
- const newValue = limitValueByCondition (
41
- calculatedValue ,
42
- minValue ,
43
- calculatedValue <= minValue ,
44
- disableStepDown
45
- ) ;
26
+ valueRoundedToScale === value
27
+ ? stepOperation ( value , step )
28
+ : valueRoundedToScale ;
29
+ const newValue = _ . clamp ( calculatedValue , minValue , maxValue ) ;
46
30
47
31
setSettingsStateValue ( newValue ) ;
48
32
} ;
49
33
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 ) ;
34
+ const onStepUp = ( ) : void => {
35
+ onStep ( 'ceil' , _ . add ) ;
73
36
} ;
74
37
75
- const disableStepDown = ( ) : void => {
76
- setStepDownDisabled ( true ) ;
38
+ const onStepDown = ( ) : void => {
39
+ onStep ( 'floor' , _ . subtract ) ;
77
40
} ;
78
41
79
42
const onUserInput = ( event : React . ChangeEvent < HTMLInputElement > ) : void => {
@@ -86,34 +49,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
86
49
const number = Number ( eventValue ) ;
87
50
88
51
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
- }
52
+ const newValue = _ . clamp ( number , minValue , maxValue ) ;
105
53
106
54
setSettingsStateValue ( newValue ) ;
107
55
}
108
56
} ;
109
57
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 ] ) ;
58
+ const upDisabled = value >= maxValue ;
59
+ const downDisabled = value <= minValue ;
117
60
118
61
return (
119
62
< div className = "settings-step-input-container" >
@@ -127,14 +70,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
127
70
< div className = "settings-step-input-buttons-container" >
128
71
< button
129
72
className = "settings-step-input-button settings-step-input-up-button"
130
- disabled = { stepUpDisabled }
73
+ disabled = { upDisabled }
131
74
onClick = { onStepUp }
132
75
>
133
76
▾
134
77
</ button >
135
78
< button
136
79
className = "settings-step-input-button"
137
- disabled = { stepDownDisabled }
80
+ disabled = { downDisabled }
138
81
onClick = { onStepDown }
139
82
>
140
83
▾
0 commit comments