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 3 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
13 changes: 11 additions & 2 deletions frontend/javascripts/oxalis/view/settings/setting_input_views.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import { Row, Col, Slider, InputNumber, Switch, Tooltip, Input, Icon, Select } from "antd";
import * as React from "react";
import _ from "lodash";

import type { Vector3, Vector6 } from "oxalis/constants";
import * as Utils from "libs/utils";
Expand All @@ -23,13 +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, label, max, min, step, onChange, disabled } = this.props;
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.
// 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