Skip to content
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

Make number slider more robust against weird input values #4758

Merged
merged 4 commits into from
Aug 10, 2020
Merged
Changes from 2 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
10 changes: 7 additions & 3 deletions frontend/javascripts/oxalis/view/settings/setting_input_views.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ export class NumberSliderSetting extends React.PureComponent<NumberSliderSetting
};

_onChange = (_value: number) => {
if (this.props.min <= _value && _value <= this.props.max) {
if (this.isValueValid(_value)) {
this.props.onChange(_value);
}
};

isValueValid = (_value: number) =>
_.isNumber(_value) && _value >= this.props.min && _value <= this.props.max;

philippotto marked this conversation as resolved.
Show resolved Hide resolved
render() {
const { value: originalValue, label, max, min, step, onChange, disabled } = this.props;

// Validate the provided value. If it's not valid, fallback to the midpoint between min and max.
const isValueValid = _.isNumber(originalValue) && originalValue >= min && originalValue <= max;
const value = isValueValid ? originalValue : Math.floor((min + max) / 2);
// This check guards against broken settings which could be introduced before this component
// checked more thoroughly against invalid values.
const value = this.isValueValid(originalValue) ? originalValue : Math.floor((min + max) / 2);

return (
<Row type="flex" align="middle">
Expand Down