Skip to content

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

[Chore] Typescript 'components/common/slider' (#1740)

Victor Belomestnovb06dfb1cfae2
diff --git a/src/components/common/animation-control/animation-control.js b/src/components/common/animation-control/animation-control.js
index 3c2a93564..d8ebc4ce9 100644
--- a/src/components/common/animation-control/animation-control.js
+++ b/src/components/common/animation-control/animation-control.js
@@ -118,7 +118,6 @@ function AnimationControlFactory(PlaybackControls, FloatingTimeDisplay) {
</StyledDomain>
<SliderWrapper className="animation-control__slider">
<Slider
- showValues={false}
isRanged={false}
step={step}
minValue={domain ? domain[0] : 0}
diff --git a/src/components/common/range-slider.tsx b/src/components/common/range-slider.tsx
index ce603c776..aa03ee924 100644
--- a/src/components/common/range-slider.tsx
+++ b/src/components/common/range-slider.tsx
@@ -279,14 +279,13 @@ export default function RangeSliderFactory(RangePlot: ReturnType<typeof RangePlo
) : null}
<Slider
marks={this.props.marks}
- showValues={false}
isRanged={isRanged}
minValue={range[0]}
maxValue={range[1]}
value0={this.props.value0}
value1={this.props.value1}
step={step}
- handleWidth={sliderHandleWidth}
+ sliderHandleWidth={sliderHandleWidth}
onSlider0Change={this._setRangeVal0}
onSlider1Change={this._setRangeVal1}
onSliderBarChange={(val0, val1) => {
diff --git a/src/components/common/slider/mouse-event.js b/src/components/common/slider/mouse-event.js
deleted file mode 100644
index e2a0cde50..000000000
--- a/src/components/common/slider/mouse-event.js
+++ /dev/null
@@ -1,97 +0,0 @@
-// Copyright (c) 2022 Uber Technologies, Inc.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-
-import document from 'global/document';
-
-function nope(...args) {}
-
-export default class MouseEventHandler {
- constructor({
- vertical = false,
- valueListener = nope,
- toggleMouseOver = nope,
- track,
- setAnchor = null
- }) {
- this._vertical = vertical;
- this._valueListener = valueListener;
- this._toggleMouseOver = toggleMouseOver;
- this._track = track;
- this._setAnchor = setAnchor;
- }
-
- handleMouseDown = e => {
- document.addEventListener('mouseup', this._mouseup);
- document.addEventListener('mousemove', this._mousemove);
- if (this._setAnchor) {
- const pos = this._getMousePos(e);
- this._setAnchor(this._getDistanceToTrack(pos));
- }
- this._toggleMouseOver();
- };
-
- _getMousePos(e) {
- return this._vertical ? e.clientY : e.clientX;
- }
-
- _getTouchPosition(e) {
- return this._vertical ? e.touches[0].clientY : e.touches[0].clientX;
- }
-
- _mouseup = () => {
- document.removeEventListener('mouseup', this._mouseup);
- document.removeEventListener('mousemove', this._mousemove);
- this._toggleMouseOver();
- };
-
- _getDistanceToTrack(pos) {
- const trackRect = this._track.current.getBoundingClientRect();
- return pos - (this._vertical ? trackRect.bottom : trackRect.left);
- }
-
- _mousemove = e => {
- e.preventDefault();
- const pos = this._getMousePos(e);
- this._valueListener(this._getDistanceToTrack(pos));
- };
-
- handleTouchStart = e => {
- // TODO: fix touch event
- document.addEventListener('touchend', this._touchend);
- document.addEventListener('touchmove', this._touchmove);
- if (this._setAnchor) {
- const pos = this._getTouchPosition(e);
- this._setAnchor(this._getDistanceToTrack(pos));
- }
- this._toggleMouseOver();
- };
-
- _touchmove = e => {
- // TODO: touch not tested
- const pos = this._getTouchPosition(e);
- this._valueListener(this._getDistanceToTrack(pos));
- };
-
- _touchend = () => {
- document.removeEventListener('touchend', this._touchend);
- document.removeEventListener('touchmove', this._touchmove);
- this._toggleMouseOver();
- };
-}
diff --git a/src/components/common/slider/mouse-event.ts b/src/components/common/slider/mouse-event.ts
new file mode 100644
index 000000000..b0488a81c
--- /dev/null
+++ b/src/components/common/slider/mouse-event.ts
@@ -0,0 +1,113 @@
+// Copyright (c) 2022 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import document from 'global/document';
+import {RefObject} from 'react';
+import {StyleRangeSliderType} from './slider';
+
+function nope(...args) {}
+
+type MouseEventHandlerProps = {
+ vertical: boolean;
+ valueListener: (distance: number) => void;
+ toggleMouseOver: () => void;
+ track: RefObject<StyleRangeSliderType>;
+ setAnchor?: null | ((distance: number) => void);
+};
+
+export default class MouseEventHandler {
+ private vertical: boolean;
+ private valueListener: (distance: number) => void;
+ private toggleMouseOver: () => void;
+ private track: RefObject<StyleRangeSliderType>; // Set correct type
+ private setAnchor: null | ((distance: number) => void); // Set correct type
+
+ constructor({
+ vertical = false,
+ valueListener = nope,
+ toggleMouseOver = nope,
+ track,
+ setAnchor = null
+ }: MouseEventHandlerProps) {
+ this.vertical = vertical;
+ this.valueListener = valueListener;
+ this.toggleMouseOver = toggleMouseOver;
+ this.track = track;
+ this.setAnchor = setAnchor;
+ }
+
+ handleMouseDown = (e: MouseEvent) => {
+ document.addEventListener('mouseup', this.mouseup);
+ document.addEventListener('mousemove', this.mousemove);
+ if (this.setAnchor) {
+ const pos = this.getMousePos(e);
+ this.setAnchor(this.getDistanceToTrack(pos));
+ }
+ this.toggleMouseOver();
+ };
+
+ private getMousePos(e: MouseEvent) {
+ return this.vertical ? e.clientY : e.clientX;
+ }
+
+ private getTouchPosition(e: TouchEvent) {
+ return this.vertical ? e.touches[0].clientY : e.touches[0].clientX;
+ }
+
+ private mouseup = () => {
+ document.removeEventListener('mouseup', this.mouseup);
+ document.removeEventListener('mousemove', this.mousemove);
+ this.toggleMouseOver();
+ };
+
+ private getDistanceToTrack(pos: number) {
+ const trackRect = this.track.current.getBoundingClientRect();
+ return pos - (this.vertical ? trackRect.bottom : trackRect.left);
+ }
+
+ private mousemove = (e: MouseEvent) => {
+ e.preventDefault();
+ const pos = this.getMousePos(e);
+ this.valueListener(this.getDistanceToTrack(pos));
+ };
+
+ handleTouchStart = (e: TouchEvent) => {
+ // TODO: fix touch event
+ document.addEventListener('touchend', this.touchend);
+ document.addEventListener('touchmove', this.touchmove);
+ if (this.setAnchor) {
+ const pos = this.getTouchPosition(e);
+ this.setAnchor(this.getDistanceToTrack(pos));
+ }
+ this.toggleMouseOver();
+ };
+
+ private touchmove = (e: TouchEvent) => {
+ // TODO: touch not tested
+ const pos = this.getTouchPosition(e);
+ this.valueListener(this.getDistanceToTrack(pos));
+ };
+
+ private touchend = () => {
+ document.removeEventListener('touchend', this.touchend);
+ document.removeEventListener('touchmove', this.touchmove);
+ this.toggleMouseOver();
+ };
+}
diff --git a/src/components/common/slider/slider-bar-handle.js b/src/components/common/slider/slider-bar-handle.tsx
similarity index 86%
rename from src/components/common/slider/slider-bar-handle.js
rename to src/components/common/slider/slider-bar-handle.tsx
index 3a38ce0c3..ef9d5d26f 100644
--- a/src/components/common/slider/slider-bar-handle.js
+++ b/src/components/common/slider/slider-bar-handle.tsx
@@ -18,11 +18,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import React, {Component} from 'react';
-import PropTypes from 'prop-types';
+import React, {Component, RefObject} from 'react';
import classnames from 'classnames';
import styled from 'styled-components';
import MouseEventHandler from './mouse-event';
+import {StyleRangeSliderType} from './slider';
const StyledSlider = styled.div`
position: relative;
@@ -37,22 +37,26 @@ const StyledSlider = styled.div`
function nope() {}
-export default class SliderBarHandle extends Component {
- static propTypes = {
- width: PropTypes.number,
- left: PropTypes.string,
- sliderBarListener: PropTypes.func,
- enableBarDrag: PropTypes.bool,
- vertical: PropTypes.bool
- };
+type SliderBarHandleProps = {
+ width: number;
+ v0Left: number;
+ sliderBarListener: (distance: number) => void;
+ enableBarDrag: boolean;
+ vertical: boolean;
+ track: RefObject<StyleRangeSliderType>;
+ setAnchor: (distance: number) => void;
+};
+export default class SliderBarHandle extends Component {
static defaultProps = {
sliderBarListener: nope,
enableBarDrag: false,
vertical: false
};
- constructor(props) {
+ public mouseEvent: MouseEventHandler;
+
+ constructor(public props: SliderBarHandleProps) {
super(props);
this.mouseEvent = new MouseEventHandler({
vertical: props.vertical,
diff --git a/src/components/common/slider/slider-handle.js b/src/components/common/slider/slider-handle.tsx
similarity index 87%
rename from src/components/common/slider/slider-handle.js
rename to src/components/common/slider/slider-handle.tsx
index 18153f138..b4f2183af 100644
--- a/src/components/common/slider/slider-handle.js
+++ b/src/components/common/slider/slider-handle.tsx
@@ -18,11 +18,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import React, {Component, createRef} from 'react';
-import PropTypes from 'prop-types';
+import React, {Component, createRef, CSSProperties, RefObject} from 'react';
import classnames from 'classnames';
import styled from 'styled-components';
import MouseEventHandler from './mouse-event';
+import {StyleRangeSliderType} from './slider';
const StyledSliderHandle = styled.span.attrs({
className: 'kg-range-slider__handle'
@@ -107,7 +107,19 @@ const StyledSliderTooltip = styled.div`
}
`;
-const SliderTooltip = ({value, format = val => val, style, sliderHandleWidth}) => {
+type SliderTooltipProps = {
+ value?: number | null;
+ format?: (value: number | null | undefined) => number | null | undefined;
+ style: CSSProperties;
+ sliderHandleWidth: number;
+};
+
+const SliderTooltip = ({
+ value,
+ format = val => val,
+ style,
+ sliderHandleWidth
+}: SliderTooltipProps) => {
return (
<StyledSliderTooltip sliderHandleWidth={sliderHandleWidth} style={style}>
{format(value)}
@@ -115,15 +127,18 @@ const SliderTooltip = ({value, format = val => val, style, sliderHandleWidth}) =
);
};
-export default class SliderHandle extends Component {
- static propTypes = {
- sliderHandleWidth: PropTypes.number,
- left: PropTypes.string,
- display: PropTypes.bool,
- valueListener: PropTypes.func,
- vertical: PropTypes.bool
- };
+type SliderHandleProps = {
+ sliderHandleWidth: number;
+ left: string;
+ display: boolean;
+ valueListener: (distance: number) => void;
+ vertical: boolean;
+ track: RefObject<StyleRangeSliderType>;
+ showTooltip: boolean;
+ value?: number;
+};
+export default class SliderHandle extends Component {
static defaultProps = {
sliderHandleWidth: 12,
left: '50%',
@@ -133,7 +148,9 @@ export default class SliderHandle extends Component {
showTooltip: false
};
- constructor(props) {
+ public mouseEvent: MouseEventHandler;
+
+ constructor(public props: SliderHandleProps) {
super(props);
this.mouseEvent = new MouseEventHandler({
diff --git a/src/components/common/slider/slider.js b/src/components/common/slider/slider.tsx
similarity index 72%
rename from src/components/common/slider/slider.js
rename to src/components/common/slider/slider.tsx
index 9610d2647..992498359 100644
--- a/src/components/common/slider/slider.js
+++ b/src/components/common/slider/slider.tsx
@@ -18,8 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import React, {Component, createRef} from 'react';
-import PropTypes from 'prop-types';
+import React, {Component, createRef, RefObject} from 'react';
import classnames from 'classnames';
import styled from 'styled-components';
@@ -36,29 +35,33 @@ const StyledRangeSlider = styled.div`
${props => `${props.vertical ? 'height' : 'width'}: 100%`};
`;
+export type StyleRangeSliderType = typeof StyledRangeSlider;
+
const SliderWrapper = styled.div`
flex-grow: 1;
`;
-export default class Slider extends Component {
- static propTypes = {
- title: PropTypes.string,
- isRanged: PropTypes.bool,
- value0: PropTypes.number,
- value1: PropTypes.number,
- minValue: PropTypes.number,
- maxValue: PropTypes.number,
- sliderHandleWidth: PropTypes.number,
- onSlider0Change: PropTypes.func,
- onInput0Change: PropTypes.func,
- onSlider1Change: PropTypes.func,
- onInput1Change: PropTypes.func,
- onSliderBarChange: PropTypes.func,
- step: PropTypes.number,
- enableBarDrag: PropTypes.bool,
- showTooltip: PropTypes.bool
- };
+type SliderProps = {
+ title: string;
+ isRanged: boolean;
+ value0: number;
+ value1: number;
+ minValue: number;
+ maxValue: number;
+ sliderHandleWidth: number;
+ onSlider0Change: (val: number) => void;
+ onSlider1Change: (val: number) => void;
+ onSliderBarChange: (val0: number, val1: number) => void;
+ step: number;
+ enableBarDrag: boolean;
+ showTooltip: boolean;
+ vertical: boolean;
+ marks?: number[];
+ classSet?: {[key: string]: boolean};
+ disabled: boolean;
+};
+export default class Slider extends Component {
static defaultProps = {
title: '',
isRanged: true,
@@ -70,83 +73,87 @@ export default class Slider extends Component {
sliderHandleWidth: 12,
enableBarDrag: false,
onSlider0Change: noop,
- onInput0Change: noop,
onSlider1Change: noop,
- onInput1Change: noop,
onSliderBarChange: noop,
disabled: false,
vertical: false,
showTooltip: false
};
- ref = createRef();
- track = createRef();
+ private anchor: number = 0;
+
+ public ref: RefObject<typeof SliderWrapper> = createRef<typeof SliderWrapper>();
+ public track: RefObject<StyleRangeSliderType> = createRef<StyleRangeSliderType>();
+
+ constructor(public props: SliderProps) {
+ super(props);
+ }
- _setAnchor = x => {
+ private setAnchor = (x: number) => {
// used to calculate delta
- this._anchor = x;
+ this.anchor = x;
};
- _getBaseDistance() {
+ private getBaseDistance() {
return this.props.vertical ? this.ref.current.offsetHeight : this.ref.current.offsetWidth;
}
- _getDeltaVal(x) {
- const percent = x / this._getBaseDistance();
+ private getDeltaVal(x: number) {
+ const percent = x / this.getBaseDistance();
const maxDelta = this.props.maxValue - this.props.minValue;
return percent * maxDelta;
}
- _getDeltaX(v) {
+ private getDeltaX(v: number) {
const percent = v / (this.props.maxValue - this.props.minValue);
- const maxDelta = this._getBaseDistance();
+ const maxDelta = this.getBaseDistance();
return percent * maxDelta;
}
- _getValue(baseV, offset) {
+ private getValue(baseV: number, offset: number) {
// offset is the distance between slider handle and track left
- const rawValue = baseV + this._getDeltaVal(offset);
+ const rawValue = baseV + this.getDeltaVal(offset);
- return this._normalizeValue(rawValue);
+ return this.normalizeValue(rawValue);
}
- _normalizeValue(val) {
+ private normalizeValue(val: number) {
const {minValue, step, marks} = this.props;
return normalizeSliderValue(val, minValue, step, marks);
}
- slide0Listener = x => {
+ slide0Listener = (x: number) => {
const {value1, minValue} = this.props;
- const val = this._getValue(minValue, x);
+ const val = this.getValue(minValue, x);
this.props.onSlider0Change(clamp([minValue, value1], val));
};
- slide1Listener = x => {
+ slide1Listener = (x: number) => {
const {minValue, maxValue, value0} = this.props;
- const val = this._getValue(minValue, x);
+ const val = this.getValue(minValue, x);
this.props.onSlider1Change(clamp([value0, maxValue], val));
};
- sliderBarListener = x => {
+ sliderBarListener = (x: number) => {
const {value0, value1, minValue, maxValue} = this.props;
// for slider bar, we use distance delta
- const anchor = this._anchor;
+ const anchor = this.anchor;
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 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);
+ const deltaX = this.getDeltaX(val0 - this.props.value0);
this.props.onSliderBarChange(val0, val1);
// update anchor
- this._anchor = this._anchor + deltaX;
+ this.anchor = this.anchor + deltaX;
};
- calcHandleLeft0 = (w, l, num) => {
+ calcHandleLeft0 = (w: number, l: number) => {
return w === 0
? `calc(${l}% - ${this.props.sliderHandleWidth / 2}px)`
: `calc(${l}% - ${this.props.sliderHandleWidth / 2}px)`;
};
- calcHandleLeft1 = (w, l) => {
+ calcHandleLeft1 = (w: number, l: number) => {
return this.props.isRanged && w === 0
? `${l}%`
: `calc(${l + w}% - ${this.props.sliderHandleWidth / 2}px)`;
@@ -204,7 +211,7 @@ export default class Slider extends Component {
sliderBarListener={this.sliderBarListener}
vertical={vertical}
track={this.track}
- setAnchor={this._setAnchor}
+ setAnchor={this.setAnchor}
/>
</StyledRangeSlider>
</SliderWrapper>