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

[Bug] Clamping slider values outside range #1395

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 13 additions & 26 deletions src/components/common/range-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import RangePlotFactory from './range-plot';
import Slider from 'components/common/slider/slider';
import {Input} from 'components/common/styled-components';

import {roundValToStep} from 'utils/data-utils';
import {roundValToStep, clamp} from 'utils/data-utils';

const SliderInput = styled(Input)`
width: ${props => props.theme.sliderInputWidth}px;
Expand Down Expand Up @@ -117,43 +117,30 @@ export default function RangeSliderFactory(RangePlot) {
(value0, value1) => [value0, value1]
);

_isVal0InRange = val => {
const {value1, range} = this.props;

return Boolean(val >= range[0] && val <= value1);
};

_isVal1InRange = val => {
const {range, value0} = this.props;

return Boolean(val <= range[1] && val >= value0);
};

_roundValToStep = val => {
const {range, step} = this.props;

return roundValToStep(range[0], step, val);
};

_setRangeVal1 = val => {
const {value0, onChange} = this.props;
const {value0, range, onChange} = this.props;
const val1 = Number(val);
if (this._isVal1InRange(val1)) {
onChange([value0, this._roundValToStep(val1)]);
return true;
}
return false;
onChange([
value0,
clamp([value0, range[1]], this._roundValToStep(val1))
]);
return true;
};

_setRangeVal0 = val => {
const {value1, onChange} = this.props;
const {value1, range, onChange} = this.props;
const val0 = Number(val);

if (this._isVal0InRange(val0)) {
onChange([this._roundValToStep(val0), value1]);
return true;
}
return false;
onChange([
clamp([range[0], value1], this._roundValToStep(val0)),
value1
]);
return true;
};

_resize() {
Expand Down
45 changes: 16 additions & 29 deletions src/components/common/slider/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import styled from 'styled-components';
import SliderHandle from './slider-handle';
import SliderBarHandle from './slider-bar-handle';
import {normalizeSliderValue} from 'utils/data-utils';
import { clamp } from 'utils/data-utils';

function noop() {}

Expand Down Expand Up @@ -109,49 +110,35 @@ export default class Slider extends Component {
return this._normalizeValue(rawValue);
}

_isVal0InRange = val => {
const {value1, minValue} = this.props;
return Boolean(val >= minValue && val <= value1);
};

_isVal1InRange = val => {
const {maxValue, value0} = this.props;
return Boolean(val <= maxValue && val >= value0);
};

_normalizeValue(val) {
const {minValue, step, marks} = this.props;
return normalizeSliderValue(val, minValue, step, marks);
}

slide0Listener = x => {
const val = this._getValue(this.props.minValue, x);

if (this._isVal0InRange(val)) {
this.props.onSlider0Change(val);
}
const {value1, minValue} = this.props;
const val = this._getValue(minValue, x);
this.props.onSlider0Change(clamp([minValue, value1], val));
};

slide1Listener = x => {
const val = this._getValue(this.props.minValue, x);
if (this._isVal1InRange(val)) {
this.props.onSlider1Change(val);
}
const {minValue, maxValue, value0} = this.props;
const val = this._getValue(minValue, x);
this.props.onSlider1Change(clamp([value0, maxValue], val));
};

sliderBarListener = x => {
const {minValue, maxValue} = this.props;
const {value0, value1, minValue, maxValue} = this.props;
// for slider bar, we use distance delta
const anchor = this._anchor;
const val0 = this._getValue(this.props.value0, x - anchor);
const val1 = this._getValue(this.props.value1, x - anchor);

if (val0 >= minValue && val1 <= maxValue && val1 >= val0) {
const deltaX = this._getDeltaX(val0 - this.props.value0);
this.props.onSliderBarChange(val0, val1);
// update anchor
this._anchor = this._anchor + deltaX;
}
const length = value1 - value0; // the length of the selected range shouldn't change when clamping
const val0 = clamp([minValue, maxValue - length], this._getValue(value0, x - anchor));
const val1 = clamp([val0 + length, maxValue], this._getValue(value1, x - anchor));

const deltaX = this._getDeltaX(val0 - this.props.value0);
this.props.onSliderBarChange(val0, val1);
// update anchor
this._anchor = this._anchor + deltaX;
};

calcHandleLeft0 = (w, l, num) => {
Expand Down
1 change: 1 addition & 0 deletions src/utils/data-utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function normalizeSliderValue(
marks?: number[]
): number;
export function roundValToStep(minValue: number, step: number, val: number): number;
export function clamp([min, max], val);

export type FieldFormatter = (value: any) => string;
export declare const FIELD_DISPLAY_FORMAT: {
Expand Down