Skip to content

Commit ce3b47b

Browse files
consider exceptions and fix styling
1 parent 2046f47 commit ce3b47b

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ export class SettingsComponent extends React.Component<
180180
<div className="column">
181181
<div className="flex-line">
182182
<SettingsStepInput
183-
value={this.state.editorFontSize}
183+
key={`font-size-stepper-${String(this.state.editorFontSize)}`}
184+
initialValue={this.state.editorFontSize}
184185
setSettingsStateValue={this.setFontSize}
185186
step={fontSizeStep}
186187
maxValue={maxFontSize}
@@ -199,13 +200,18 @@ export class SettingsComponent extends React.Component<
199200
</label>
200201
<div>
201202
<SettingsStepInput
202-
value={scalePercentage}
203+
key={`scale-stepper-${String(scalePercentage)}`}
204+
initialValue={scalePercentage}
203205
setSettingsStateValue={this.setInterfaceScale}
204206
step={scaleStep}
205207
maxValue={maxScale}
206208
minValue={minScale}
207209
unitOfMeasure="%"
208-
classNames={{ input: 'theia-input small with-margin' }}
210+
classNames={{
211+
input: 'theia-input small with-margin',
212+
buttonsContainer:
213+
'settings-step-input-buttons-container-perc',
214+
}}
209215
/>
210216
</div>
211217
</div>

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

+40-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from '@theia/core/shared/react';
22
import classnames from 'classnames';
33

44
interface SettingsStepInputProps {
5-
value: number;
5+
initialValue: number;
66
setSettingsStateValue: (value: number) => void;
77
step: number;
88
maxValue: number;
@@ -15,7 +15,7 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
1515
props: SettingsStepInputProps
1616
) => {
1717
const {
18-
value,
18+
initialValue,
1919
setSettingsStateValue,
2020
step,
2121
maxValue,
@@ -24,7 +24,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
2424
classNames,
2525
} = props;
2626

27-
const [tempValue, setTempValue] = React.useState<string>(String(value));
27+
const [valueState, setValueState] = React.useState<{
28+
currentValue: number;
29+
isEmptyString: boolean;
30+
}>({
31+
currentValue: initialValue,
32+
isEmptyString: false,
33+
});
34+
const { currentValue, isEmptyString } = valueState;
2835

2936
const clamp = (value: number, min: number, max: number): number => {
3037
return Math.min(Math.max(value, min), max);
@@ -34,10 +41,11 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
3441
roundingOperation: 'ceil' | 'floor',
3542
stepOperation: (a: number, b: number) => number
3643
): void => {
37-
const valueRoundedToScale = Math[roundingOperation](value / step) * step;
44+
const valueRoundedToScale =
45+
Math[roundingOperation](currentValue / step) * step;
3846
const calculatedValue =
39-
valueRoundedToScale === value
40-
? stepOperation(value, step)
47+
valueRoundedToScale === currentValue
48+
? stepOperation(currentValue, step)
4149
: valueRoundedToScale;
4250
const newValue = clamp(calculatedValue, minValue, maxValue);
4351

@@ -54,41 +62,54 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
5462

5563
const onUserInput = (event: React.ChangeEvent<HTMLInputElement>): void => {
5664
const { value: eventValue } = event.target;
57-
setTempValue(eventValue);
65+
setValueState({
66+
currentValue: Number(eventValue),
67+
isEmptyString: eventValue === '',
68+
});
5869
};
5970

6071
/* Prevent the user from entering invalid values */
6172
const onBlur = (): void => {
62-
const tempValueAsNumber = Number(tempValue);
63-
64-
/* If the user input is not a number, reset the input to the previous value */
65-
if (isNaN(tempValueAsNumber) || tempValue === '') {
66-
setTempValue(String(value));
73+
if ((currentValue === 0 && minValue > 0) || isNaN(currentValue)) {
74+
setValueState({
75+
currentValue: initialValue,
76+
isEmptyString: false,
77+
});
6778
return;
6879
}
69-
if (tempValueAsNumber !== value) {
80+
81+
if (currentValue !== initialValue) {
7082
/* If the user input is a number, clamp it to the min and max values */
71-
const newValue = clamp(tempValueAsNumber, minValue, maxValue);
83+
const newValue = clamp(currentValue, minValue, maxValue);
7284

7385
setSettingsStateValue(newValue);
74-
setTempValue(String(newValue));
7586
}
7687
};
7788

78-
const upDisabled = value >= maxValue;
79-
const downDisabled = value <= minValue;
89+
const valueIsNotWithinRange =
90+
currentValue < minValue || currentValue > maxValue;
91+
const isDisabledException =
92+
valueIsNotWithinRange || isEmptyString || isNaN(currentValue);
93+
94+
const upDisabled = isDisabledException || currentValue >= maxValue;
95+
const downDisabled = isDisabledException || currentValue <= minValue;
8096

8197
return (
8298
<div className="settings-step-input-container">
8399
<input
84100
className={classnames('settings-step-input-element', classNames?.input)}
85-
value={tempValue.toString()}
101+
value={isEmptyString ? '' : String(currentValue)}
86102
onChange={onUserInput}
87103
onBlur={onBlur}
88104
type="number"
89105
pattern="[0-9]+"
90106
/>
91-
<div className="settings-step-input-buttons-container">
107+
<div
108+
className={classnames(
109+
'settings-step-input-buttons-container',
110+
classNames?.buttonsContainer
111+
)}
112+
>
92113
<button
93114
className="settings-step-input-button settings-step-input-up-button"
94115
disabled={upDisabled}

Diff for: arduino-ide-extension/src/browser/style/settings-step-input.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
display: none;
1313
flex-direction: column;
1414
position: absolute;
15-
right: 14px;
15+
right: 0px;
1616
top: 50%;
1717
transform: translate(0px, -50%);
1818
height: calc(100% - 4px);
@@ -21,6 +21,10 @@
2121
background: var(--theia-input-background);
2222
}
2323

24+
.settings-step-input-buttons-container-perc {
25+
right: 14px;
26+
}
27+
2428
.settings-step-input-container:hover > .settings-step-input-buttons-container {
2529
display: flex;
2630
}

0 commit comments

Comments
 (0)