-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathSlider.react.js
More file actions
165 lines (153 loc) · 5.33 KB
/
Slider.react.js
File metadata and controls
165 lines (153 loc) · 5.33 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, {Component} from 'react';
import ReactSlider, {createSliderWithTooltip} from 'rc-slider';
import {assoc, isNil, pick, pipe, omit} from 'ramda';
import computeSliderStyle from '../utils/computeSliderStyle';
import 'rc-slider/assets/index.css';
import {
sanitizeMarks,
calcStep,
setUndefined,
} from '../utils/computeSliderMarkers';
import {propTypes} from '../components/Slider.react';
import {
formatSliderTooltip,
transformSliderTooltip,
} from '../utils/formatSliderTooltip';
import LoadingElement from '../utils/LoadingElement';
const MAX_SLIDER_MARKS = 5000;
const sliderProps = [
'min',
'max',
'disabled',
'dots',
'included',
'tooltip',
'vertical',
'id',
];
/**
* A slider component with a single handle.
*/
export default class Slider extends Component {
constructor(props) {
super(props);
this.DashSlider = props.tooltip
? createSliderWithTooltip(ReactSlider)
: ReactSlider;
this._computeStyle = computeSliderStyle();
this.state = {value: props.value};
}
UNSAFE_componentWillReceiveProps(newProps) {
if (newProps.tooltip !== this.props.tooltip) {
this.DashSlider = newProps.tooltip
? createSliderWithTooltip(ReactSlider)
: ReactSlider;
}
if (newProps.value !== this.props.value) {
this.props.setProps({drag_value: newProps.value});
this.setState({value: newProps.value});
}
}
UNSAFE_componentWillMount() {
if (this.props.value !== null) {
this.props.setProps({drag_value: this.props.value});
this.setState({value: this.props.value});
}
}
render() {
const {
className,
id,
setProps,
tooltip,
updatemode,
min,
max,
marks,
step,
vertical,
verticalHeight,
} = this.props;
const value = this.state.value;
// Validate marks count
if (marks && Object.keys(marks).length > MAX_SLIDER_MARKS) {
throw new Error(
'Slider marks cannot exceed 5000 items. Received ' +
Object.keys(marks).length +
' marks.'
);
}
let tipProps, tipFormatter;
if (tooltip) {
/**
* clone `tooltip` but with renamed key `always_visible` -> `visible`
* the rc-tooltip API uses `visible`, but `always_visible` is more semantic
* assigns the new (renamed) key to the old key and deletes the old key
*/
tipProps = pipe(
assoc('visible', tooltip.always_visible),
omit(['always_visible', 'template', 'style', 'transform'])
)(tooltip);
if (tooltip.template || tooltip.style || tooltip.transform) {
tipFormatter = tipValue => {
let t = tipValue;
if (tooltip.transform) {
t = transformSliderTooltip(tooltip.transform, tipValue);
}
return (
<div style={tooltip.style}>
{formatSliderTooltip(
tooltip.template || '{value}',
t
)}
</div>
);
};
}
}
return (
<LoadingElement
id={id}
className={className}
style={this._computeStyle(vertical, verticalHeight, tooltip)}
>
<this.DashSlider
onChange={value => {
if (updatemode === 'drag') {
setProps({value: value, drag_value: value});
} else {
this.setState({value: value});
setProps({drag_value: value});
}
}}
onAfterChange={value => {
if (updatemode === 'mouseup') {
setProps({value});
}
}}
/*
if/when rc-slider or rc-tooltip are updated to latest versions,
we will need to revisit this code as the getTooltipContainer function will need to be a prop instead of a nested property
*/
tipProps={{
...tipProps,
getTooltipContainer: node => node,
}}
tipFormatter={tipFormatter}
style={{position: 'relative'}}
value={value}
marks={sanitizeMarks({min, max, marks, step})}
max={setUndefined(min, max, marks).max_mark}
min={setUndefined(min, max, marks).min_mark}
step={
step === null && !isNil(marks)
? null
: calcStep(min, max, step)
}
{...pick(sliderProps, this.props)}
/>
</LoadingElement>
);
}
}
Slider.propTypes = propTypes;