Skip to content

Commit eb78f36

Browse files
remove state from stepper input and simplify
1 parent f22be3c commit eb78f36

File tree

1 file changed

+19
-76
lines changed

1 file changed

+19
-76
lines changed

arduino-ide-extension/src/browser/dialogs/settings/settings-step-input.tsx

+19-76
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from '@theia/core/shared/react';
22
import classnames from 'classnames';
3+
import _ = require('lodash');
34

45
interface SettingsStepInputProps {
56
value: number;
@@ -16,64 +17,26 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
1617
const { value, setSettingsStateValue, step, maxValue, minValue, classNames } =
1718
props;
1819

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;
3825
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);
4630

4731
setSettingsStateValue(newValue);
4832
};
4933

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);
7336
};
7437

75-
const disableStepDown = (): void => {
76-
setStepDownDisabled(true);
38+
const onStepDown = (): void => {
39+
onStep('floor', _.subtract);
7740
};
7841

7942
const onUserInput = (event: React.ChangeEvent<HTMLInputElement>): void => {
@@ -86,34 +49,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
8649
const number = Number(eventValue);
8750

8851
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);
10553

10654
setSettingsStateValue(newValue);
10755
}
10856
};
10957

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;
11760

11861
return (
11962
<div className="settings-step-input-container">
@@ -127,14 +70,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
12770
<div className="settings-step-input-buttons-container">
12871
<button
12972
className="settings-step-input-button settings-step-input-up-button"
130-
disabled={stepUpDisabled}
73+
disabled={upDisabled}
13174
onClick={onStepUp}
13275
>
13376
&#9662;
13477
</button>
13578
<button
13679
className="settings-step-input-button"
137-
disabled={stepDownDisabled}
80+
disabled={downDisabled}
13881
onClick={onStepDown}
13982
>
14083
&#9662;

0 commit comments

Comments
 (0)