-
Notifications
You must be signed in to change notification settings - Fork 295
/
Slider.tsx
113 lines (109 loc) · 2.93 KB
/
Slider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// @flow
import React, { useState } from 'react';
import { observer } from 'mobx-react';
import BigNumber from 'bignumber.js';
import RcSlider from 'rc-slider';
import { PopOver } from 'react-polymorph/lib/components/PopOver';
import { shortNumber } from '../../utils/formatters';
import styles from './Slider.scss';
type Props = {
className?: string,
min: number,
minDisplayValue?: number,
max: number,
maxDisplayValue?: number,
marks?: any,
step?: number,
vertical?: boolean,
handle?: Function,
included?: boolean,
reverse?: boolean,
disabled?: boolean,
dots?: boolean,
onBeforeChange?: Function,
onChange: Function,
onAfterChange?: Function,
minimumTrackStyle?: any,
maximumTrackStyle?: any,
handleStyle?: any,
trackStyle?: any,
railStyle?: any,
dotStyle?: any,
activeDotStyle?: any,
defaultValue?: number,
value: number,
displayValue?: any,
showRawValue?: boolean,
showTooltip?: boolean,
minTooltip?: string,
maxTooltip?: string,
startPoint?: number,
tabIndex?: number,
ariaLabelForHandle?: string,
ariaLabelledByForHandle?: string,
ariaValueTextFormatterForHandle?: Function,
};
export const Slider = observer((props: Props) => {
const [initialValue, setInitialValue] = useState<number | null>(null);
const {
showTooltip,
minTooltip,
maxTooltip,
minDisplayValue,
maxDisplayValue,
displayValue,
showRawValue,
...rest
} = props;
const { min, max, value } = rest;
const valueMarkLeftPosition =
max === min ? `0` : `${((value - min) / (max - min)) * 100}%`;
const valueMarkStyle = { left: valueMarkLeftPosition };
const formattedValue = showRawValue
? displayValue || value
: new BigNumber(value).toFormat(0);
return (
<div className={styles.component}>
<div className={styles.upperMarks}>
<div className={styles.minMark}>
{showTooltip ? (
<PopOver content={minTooltip}>
{shortNumber(minDisplayValue || min)}
</PopOver>
) : (
shortNumber(minDisplayValue || min)
)}
</div>
<div className={styles.maxMark}>
{showTooltip ? (
<PopOver content={maxTooltip}>
{shortNumber(maxDisplayValue || max)}
</PopOver>
) : (
shortNumber(maxDisplayValue || max)
)}
</div>
</div>
<RcSlider
{...rest}
onBeforeChange={(e: number) => {
if (!initialValue) setInitialValue(e);
}}
onChange={(e: number) => {
rest.onChange(e);
}}
onAfterChange={(e: number) => {
if (e !== initialValue && !!rest.onAfterChange) {
rest.onAfterChange(e);
}
setInitialValue(null);
}}
/>
<div className={styles.lowerMarks}>
<div className={styles.valueMark} style={valueMarkStyle}>
{formattedValue}
</div>
</div>
</div>
);
});