Skip to content

Commit

Permalink
[Slider] Resolve slider value to respect max and min (#7863)
Browse files Browse the repository at this point in the history
  • Loading branch information
janmarsicek authored and oliviertassinari committed Aug 22, 2017
1 parent a2a2547 commit b19c146
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,36 +386,43 @@ class Slider extends Component {
value: valueProp,
defaultValue,
min,
max,
} = this.props;

let value = valueProp;
if (value === undefined) {
value = defaultValue !== undefined ? defaultValue : min;
}

if (value > max) {
value = max;
} else if (value < min) {
value = min;
}

this.setState({
value: value,
value: this.resolveValue(value),
});
}

componentWillReceiveProps(nextProps) {
if (nextProps.value !== undefined && !this.state.dragging) {
this.setState({
value: nextProps.value,
value: this.resolveValue(nextProps.value),
});
}
}

track = null;
handle = null;

resolveValue = (value) => {
const {max, min} = this.props;

if (value > max) {
return max;
}

if (value < min) {
return min;
}

return value;
}

handleKeyDown = (event) => {
const {
axis,
Expand Down Expand Up @@ -491,13 +498,7 @@ class Slider extends Component {

// We need to use toFixed() because of float point errors.
// For example, 0.01 + 0.06 = 0.06999999999999999
newValue = parseFloat(newValue.toFixed(5));

if (newValue > max) {
newValue = max;
} else if (newValue < min) {
newValue = min;
}
newValue = this.resolveValue(parseFloat(newValue.toFixed(5)));

if (this.state.value !== newValue) {
this.setState({
Expand Down Expand Up @@ -745,6 +746,8 @@ class Slider extends Component {
value = parseFloat(value.toFixed(5));
}

value = this.resolveValue(value);

if (this.state.value !== value) {
this.setState({
value: value,
Expand Down

0 comments on commit b19c146

Please sign in to comment.