Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Bug] Clamping slider values outside range (#1395)

Shan He6728b30f5ac0
diff --git a/src/components/common/range-slider.js b/src/components/common/range-slider.js
index d5839021b..3df916134 100644
--- a/src/components/common/range-slider.js
+++ b/src/components/common/range-slider.js
@@ -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;
@@ -117,18 +117,6 @@ 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;
@@ -136,24 +124,23 @@ export default function RangeSliderFactory(RangePlot) {
};
_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() {
diff --git a/src/components/common/slider/slider.js b/src/components/common/slider/slider.js
index 65feb0338..a84c9026b 100644
--- a/src/components/common/slider/slider.js
+++ b/src/components/common/slider/slider.js
@@ -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() {}
@@ -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) => {
diff --git a/src/utils/data-utils.d.ts b/src/utils/data-utils.d.ts
index da8b5a139..98d6430f5 100644
--- a/src/utils/data-utils.d.ts
+++ b/src/utils/data-utils.d.ts
@@ -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: {