mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-07-25 20:26:38 +00:00
remove state from stepper input and simplify (#1264)
* remove state from stepper input and simplify * get rid of lodash
This commit is contained in:
parent
40c93bc19a
commit
55927ac3dd
@ -16,64 +16,30 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
|
|||||||
const { value, setSettingsStateValue, step, maxValue, minValue, classNames } =
|
const { value, setSettingsStateValue, step, maxValue, minValue, classNames } =
|
||||||
props;
|
props;
|
||||||
|
|
||||||
const [stepUpDisabled, setStepUpDisabled] = React.useState(false);
|
const clamp = (value: number, min: number, max: number): number => {
|
||||||
const [stepDownDisabled, setStepDownDisabled] = React.useState(false);
|
return Math.min(Math.max(value, min), max);
|
||||||
|
};
|
||||||
|
|
||||||
const onStepUp = (): void => {
|
const onStep = (
|
||||||
const valueRoundedToScale = Math.ceil(value / step) * step;
|
roundingOperation: 'ceil' | 'floor',
|
||||||
|
stepOperation: (a: number, b: number) => number
|
||||||
|
): void => {
|
||||||
|
const valueRoundedToScale = Math[roundingOperation](value / step) * step;
|
||||||
const calculatedValue =
|
const calculatedValue =
|
||||||
valueRoundedToScale === value ? value + step : valueRoundedToScale;
|
valueRoundedToScale === value
|
||||||
const newValue = limitValueByCondition(
|
? stepOperation(value, step)
|
||||||
calculatedValue,
|
: valueRoundedToScale;
|
||||||
maxValue,
|
const newValue = clamp(calculatedValue, minValue, maxValue);
|
||||||
calculatedValue >= maxValue,
|
|
||||||
disableStepUp
|
|
||||||
);
|
|
||||||
|
|
||||||
setSettingsStateValue(newValue);
|
setSettingsStateValue(newValue);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onStepUp = (): void => {
|
||||||
|
onStep('ceil', (a: number, b: number) => a + b);
|
||||||
|
};
|
||||||
|
|
||||||
const onStepDown = (): void => {
|
const onStepDown = (): void => {
|
||||||
const valueRoundedToScale = Math.floor(value / step) * step;
|
onStep('floor', (a: number, b: number) => a - b);
|
||||||
const calculatedValue =
|
|
||||||
valueRoundedToScale === value ? value - step : valueRoundedToScale;
|
|
||||||
const newValue = limitValueByCondition(
|
|
||||||
calculatedValue,
|
|
||||||
minValue,
|
|
||||||
calculatedValue <= minValue,
|
|
||||||
disableStepDown
|
|
||||||
);
|
|
||||||
|
|
||||||
setSettingsStateValue(newValue);
|
|
||||||
};
|
|
||||||
|
|
||||||
const limitValueByCondition = (
|
|
||||||
calculatedValue: number,
|
|
||||||
limitedValue: number,
|
|
||||||
condition: boolean,
|
|
||||||
onConditionTrue: () => void,
|
|
||||||
onConditionFalse = enableButtons
|
|
||||||
): number => {
|
|
||||||
if (condition) {
|
|
||||||
onConditionTrue();
|
|
||||||
return limitedValue;
|
|
||||||
} else {
|
|
||||||
onConditionFalse();
|
|
||||||
return calculatedValue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const enableButtons = (): void => {
|
|
||||||
setStepUpDisabled(false);
|
|
||||||
setStepDownDisabled(false);
|
|
||||||
};
|
|
||||||
|
|
||||||
const disableStepUp = (): void => {
|
|
||||||
setStepUpDisabled(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
const disableStepDown = (): void => {
|
|
||||||
setStepDownDisabled(true);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onUserInput = (event: React.ChangeEvent<HTMLInputElement>): void => {
|
const onUserInput = (event: React.ChangeEvent<HTMLInputElement>): void => {
|
||||||
@ -86,34 +52,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
|
|||||||
const number = Number(eventValue);
|
const number = Number(eventValue);
|
||||||
|
|
||||||
if (!isNaN(number) && number !== value) {
|
if (!isNaN(number) && number !== value) {
|
||||||
let newValue;
|
const newValue = clamp(number, minValue, maxValue);
|
||||||
if (number > value) {
|
|
||||||
newValue = limitValueByCondition(
|
|
||||||
number,
|
|
||||||
maxValue,
|
|
||||||
number >= maxValue,
|
|
||||||
disableStepUp
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
newValue = limitValueByCondition(
|
|
||||||
number,
|
|
||||||
minValue,
|
|
||||||
number <= minValue,
|
|
||||||
disableStepDown
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
setSettingsStateValue(newValue);
|
setSettingsStateValue(newValue);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// the component does not unmount when we close the settings dialog
|
const upDisabled = value >= maxValue;
|
||||||
// in theia which necessitates the below useEffect
|
const downDisabled = value <= minValue;
|
||||||
React.useEffect(() => {
|
|
||||||
if (value > minValue && value < maxValue) {
|
|
||||||
enableButtons();
|
|
||||||
}
|
|
||||||
}, [value, minValue, maxValue]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="settings-step-input-container">
|
<div className="settings-step-input-container">
|
||||||
@ -127,14 +73,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
|
|||||||
<div className="settings-step-input-buttons-container">
|
<div className="settings-step-input-buttons-container">
|
||||||
<button
|
<button
|
||||||
className="settings-step-input-button settings-step-input-up-button"
|
className="settings-step-input-button settings-step-input-up-button"
|
||||||
disabled={stepUpDisabled}
|
disabled={upDisabled}
|
||||||
onClick={onStepUp}
|
onClick={onStepUp}
|
||||||
>
|
>
|
||||||
▾
|
▾
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
className="settings-step-input-button"
|
className="settings-step-input-button"
|
||||||
disabled={stepDownDisabled}
|
disabled={downDisabled}
|
||||||
onClick={onStepDown}
|
onClick={onStepDown}
|
||||||
>
|
>
|
||||||
▾
|
▾
|
||||||
|
Loading…
x
Reference in New Issue
Block a user