Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

remove state from stepper input and simplify #1264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
davegarthsimpson merged 2 commits into main from refactor-stepper-react-component
Jul 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,30 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
const { value, setSettingsStateValue, step, maxValue, minValue, classNames } =
props;

const [stepUpDisabled, setStepUpDisabled] = React.useState(false);
const [stepDownDisabled, setStepDownDisabled] = React.useState(false);

const onStepUp = (): void => {
const valueRoundedToScale = Math.ceil(value / step) * step;
const calculatedValue =
valueRoundedToScale === value ? value + step : valueRoundedToScale;
const newValue = limitValueByCondition(
calculatedValue,
maxValue,
calculatedValue >= maxValue,
disableStepUp
);

setSettingsStateValue(newValue);
const clamp = (value: number, min: number, max: number): number => {
return Math.min(Math.max(value, min), max);
};

const onStepDown = (): void => {
const valueRoundedToScale = Math.floor(value / step) * step;
const onStep = (
roundingOperation: 'ceil' | 'floor',
stepOperation: (a: number, b: number) => number
): void => {
const valueRoundedToScale = Math[roundingOperation](value / step) * step;
const calculatedValue =
valueRoundedToScale === value ? value - step : valueRoundedToScale;
const newValue = limitValueByCondition(
calculatedValue,
minValue,
calculatedValue <= minValue,
disableStepDown
);
valueRoundedToScale === value
? stepOperation(value, step)
: valueRoundedToScale;
const newValue = clamp(calculatedValue, minValue, maxValue);

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 onStepUp = (): void => {
onStep('ceil', (a: number, b: number) => a + b);
};

const disableStepDown = (): void => {
setStepDownDisabled(true);
const onStepDown = (): void => {
onStep('floor', (a: number, b: number) => a - b);
};

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

if (!isNaN(number) && number !== value) {
let newValue;
Copy link
Contributor

@kittaakos kittaakos Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the code could return with the function above 👆

if (eventValue === '') {
 setSettingsStateValue(0);
 return; // <--- this?
}

Copy link
Contributor Author

@davegarthsimpson davegarthsimpson Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! sorry I saw this after the fact, I'll add it in when we tackle the scale range question.

if (number > value) {
newValue = limitValueByCondition(
number,
maxValue,
number >= maxValue,
disableStepUp
);
} else {
newValue = limitValueByCondition(
number,
minValue,
number <= minValue,
disableStepDown
);
}
const newValue = clamp(number, minValue, maxValue);

setSettingsStateValue(newValue);
}
};

// the component does not unmount when we close the settings dialog
// in theia which necessitates the below useEffect
React.useEffect(() => {
if (value > minValue && value < maxValue) {
enableButtons();
}
}, [value, minValue, maxValue]);
const upDisabled = value >= maxValue;
const downDisabled = value <= minValue;

return (
<div className="settings-step-input-container">
Expand All @@ -127,14 +73,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = (
<div className="settings-step-input-buttons-container">
<button
className="settings-step-input-button settings-step-input-up-button"
disabled={stepUpDisabled}
disabled={upDisabled}
onClick={onStepUp}
>
&#9662;
</button>
<button
className="settings-step-input-button"
disabled={stepDownDisabled}
disabled={downDisabled}
onClick={onStepDown}
>
&#9662;
Expand Down

AltStyle によって変換されたページ (->オリジナル) /