-
-
Notifications
You must be signed in to change notification settings - Fork 453
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
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import * as React from '@theia/core/shared/react'; | ||
import classnames from 'classnames'; | ||
import _ = require('lodash'); | ||
|
||
interface SettingsStepInputProps { | ||
value: number; | ||
|
@@ -16,64 +17,26 @@ 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 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', _.add); | ||
}; | ||
|
||
const disableStepDown = (): void => { | ||
setStepDownDisabled(true); | ||
const onStepDown = (): void => { | ||
onStep('floor', _.subtract); | ||
}; | ||
|
||
const onUserInput = (event: React.ChangeEvent<HTMLInputElement>): void => { | ||
|
@@ -86,34 +49,14 @@ const SettingsStepInput: React.FC<SettingsStepInputProps> = ( | |
const number = Number(eventValue); | ||
|
||
if (!isNaN(number) && number !== value) { | ||
let newValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"> | ||
|
@@ -127,14 +70,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} | ||
> | ||
▾ | ||
</button> | ||
<button | ||
className="settings-step-input-button" | ||
disabled={stepDownDisabled} | ||
disabled={downDisabled} | ||
onClick={onStepDown} | ||
> | ||
▾ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.