-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathSlider.react.js
More file actions
176 lines (164 loc) · 5.82 KB
/
Slider.react.js
File metadata and controls
176 lines (164 loc) · 5.82 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
166
167
168
169
170
171
172
173
174
175
176
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_MARKS = 500;
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;
// Check if marks exceed 500 limit for performance
let processedMarks = marks;
if (marks && typeof marks === 'object' && marks !== null) {
const marksCount = Object.keys(marks).length;
if (marksCount > MAX_MARKS) {
/* eslint-disable no-console */
console.error(
`dcc.Slider: Too many marks (${marksCount}) provided. ` +
`For performance reasons, marks are limited to 500. ` +
`Using auto-generated marks instead.`
);
processedMarks = undefined;
}
}
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: processedMarks,
step,
})}
max={setUndefined(min, max, processedMarks).max_mark}
min={setUndefined(min, max, processedMarks).min_mark}
step={
step === null && !isNil(processedMarks)
? null
: calcStep(min, max, step)
}
{...pick(sliderProps, this.props)}
/>
</LoadingElement>
);
}
}
Slider.propTypes = propTypes;