Skip to content

Commit 8792a39

Browse files
davegarthsimpsonAlberto Iannaccone
authored and
Alberto Iannaccone
committed
misc fixes
1 parent a980e7e commit 8792a39

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

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

+16-18
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
3737
return Math.min(Math.max(value, min), max);
3838
};
3939

40-
const setValidValue = (value: number): void => {
41-
const clampedValue = clamp(value, minValue, maxValue);
42-
setValueState({ currentValue: clampedValue, isEmptyString: false });
43-
setSettingsStateValue(clampedValue);
40+
const resetToInitialState = (): void => {
41+
setValueState({
42+
currentValue: initialValue,
43+
isEmptyString: false,
44+
});
4445
};
4546

4647
const onStep = (
@@ -53,7 +54,9 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
5354
valueRoundedToScale === currentValue
5455
? stepOperation(currentValue, step)
5556
: valueRoundedToScale;
56-
setValidValue(calculatedValue);
57+
const newValue = clamp(calculatedValue, minValue, maxValue);
58+
59+
setSettingsStateValue(newValue);
5760
};
5861

5962
const onStepUp = (): void => {
@@ -74,25 +77,20 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
7477

7578
/* Prevent the user from entering invalid values */
7679
const onBlur = (event: React.FocusEvent): void => {
77-
if (event.currentTarget.contains(event.relatedTarget as Node)) {
78-
return;
79-
}
80-
8180
if (
82-
(currentValue === 0 && minValue > 0) ||
83-
isNaN(currentValue) ||
84-
isEmptyString
81+
(currentValue === initialValue && !isEmptyString) ||
82+
event.currentTarget.contains(event.relatedTarget as Node)
8583
) {
86-
setValueState({
87-
currentValue: initialValue,
88-
isEmptyString: false,
89-
});
9084
return;
9185
}
9286

93-
if (currentValue !== initialValue) {
94-
setValidValue(currentValue);
87+
const clampedValue = clamp(currentValue, minValue, maxValue);
88+
if (clampedValue === initialValue || isNaN(currentValue) || isEmptyString) {
89+
resetToInitialState();
90+
return;
9591
}
92+
93+
setSettingsStateValue(clampedValue);
9694
};
9795

9896
const valueIsNotWithinRange =

0 commit comments

Comments
 (0)