Skip to content

Commit 073775c

Browse files
committed
Update changelog.
1 parent 45e8aa8 commit 073775c

File tree

7 files changed

+98
-29
lines changed

7 files changed

+98
-29
lines changed

components/dash-core-components/src/components/RangeSlider.react.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ RangeSlider.propTypes = {
134134
* Custom style for the tooltip.
135135
*/
136136
style: PropTypes.object,
137+
/**
138+
* Reference to a function in the `window.dccFunctions` namespace.
139+
* This can be added in a scrip in the asset folder.
140+
*
141+
* For example, in `assets/tooltip.js`:
142+
* ```
143+
* window.dccFunctions = window.dccFunctions || {};
144+
* window.dccFunctions.multByTen = function(value) {
145+
* return value * 10;
146+
* }
147+
* ```
148+
* Then in the component `tooltip={'transform': 'multByTen'
149+
*/
150+
transform: PropTypes.string,
137151
}),
138152

139153
/**

components/dash-core-components/src/components/Slider.react.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ Slider.propTypes = {
116116
* Custom style for the tooltip.
117117
*/
118118
style: PropTypes.object,
119+
/**
120+
* Reference to a function in the `window.dccFunctions` namespace.
121+
* This can be added in a scrip in the asset folder.
122+
*
123+
* For example, in `assets/tooltip.js`:
124+
* ```
125+
* window.dccFunctions = window.dccFunctions || {};
126+
* window.dccFunctions.multByTen = function(value) {
127+
* return value * 10;
128+
* }
129+
* ```
130+
* Then in the component `tooltip={'transform': 'multByTen'
131+
*/
132+
transform: PropTypes.string,
119133
}),
120134

121135
/**

components/dash-core-components/src/fragments/RangeSlider.react.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {Component} from 'react';
2-
import {assoc, pick, isNil, pipe, dissoc} from 'ramda';
2+
import {assoc, pick, isNil, pipe, omit} from 'ramda';
33
import {Range, createSliderWithTooltip} from 'rc-slider';
44
import computeSliderStyle from '../utils/computeSliderStyle';
55

@@ -11,7 +11,10 @@ import {
1111
setUndefined,
1212
} from '../utils/computeSliderMarkers';
1313
import {propTypes, defaultProps} from '../components/RangeSlider.react';
14-
import {formatSliderTooltip} from '../utils/formatSliderTooltip';
14+
import {
15+
formatSliderTooltip,
16+
transformSliderTooltip,
17+
} from '../utils/formatSliderTooltip';
1518

1619
const sliderProps = [
1720
'min',
@@ -82,19 +85,23 @@ export default class RangeSlider extends Component {
8285
*/
8386
tipProps = pipe(
8487
assoc('visible', tooltip.always_visible),
85-
dissoc('always_visible'),
86-
dissoc('format'),
87-
dissoc('style')
88+
omit(['always_visible', 'format', 'style', 'transform'])
8889
)(tooltip);
89-
if (tooltip.format || tooltip.style) {
90-
tipFormatter = tipValue => (
91-
<div style={tooltip.style}>
92-
{formatSliderTooltip(
93-
tooltip.format || '{value}',
94-
tipValue
95-
)}
96-
</div>
97-
);
90+
if (tooltip.format || tooltip.style || tooltip.transform) {
91+
tipFormatter = tipValue => {
92+
let t = tipValue;
93+
if (tooltip.transform) {
94+
t = transformSliderTooltip(tooltip.transform, tipValue);
95+
}
96+
return (
97+
<div style={tooltip.style}>
98+
{formatSliderTooltip(
99+
tooltip.format || '{value}',
100+
t
101+
)}
102+
</div>
103+
);
104+
};
98105
}
99106
}
100107

components/dash-core-components/src/fragments/Slider.react.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {Component} from 'react';
22
import ReactSlider, {createSliderWithTooltip} from 'rc-slider';
3-
import {assoc, isNil, pick, pipe, dissoc} from 'ramda';
3+
import {assoc, isNil, pick, pipe, omit} from 'ramda';
44
import computeSliderStyle from '../utils/computeSliderStyle';
55

66
import 'rc-slider/assets/index.css';
@@ -11,7 +11,10 @@ import {
1111
setUndefined,
1212
} from '../utils/computeSliderMarkers';
1313
import {propTypes, defaultProps} from '../components/Slider.react';
14-
import {formatSliderTooltip} from '../utils/formatSliderTooltip';
14+
import {
15+
formatSliderTooltip,
16+
transformSliderTooltip,
17+
} from '../utils/formatSliderTooltip';
1518

1619
const sliderProps = [
1720
'min',
@@ -82,19 +85,23 @@ export default class Slider extends Component {
8285
*/
8386
tipProps = pipe(
8487
assoc('visible', tooltip.always_visible),
85-
dissoc('always_visible'),
86-
dissoc('format'),
87-
dissoc('style')
88+
omit(['always_visible', 'format', 'style', 'transform'])
8889
)(tooltip);
89-
if (tooltip.format || tooltip.style) {
90-
tipFormatter = tipValue => (
91-
<div style={tooltip.style}>
92-
{formatSliderTooltip(
93-
tooltip.format || '{value}',
94-
tipValue
95-
)}
96-
</div>
97-
);
90+
if (tooltip.format || tooltip.style || tooltip.transform) {
91+
tipFormatter = tipValue => {
92+
let t = tipValue;
93+
if (tooltip.transform) {
94+
t = transformSliderTooltip(tooltip.transform, tipValue);
95+
}
96+
return (
97+
<div style={tooltip.style}>
98+
{formatSliderTooltip(
99+
tooltip.format || '{value}',
100+
t
101+
)}
102+
</div>
103+
);
104+
};
98105
}
99106
}
100107

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
import {replace} from 'ramda';
1+
import {replace, path, split, concat, pipe} from 'ramda';
22

33
export const formatSliderTooltip = (template, value) => {
44
return replace('{value}', value, template);
55
};
6+
7+
export const transformSliderTooltip = (funcName, value) => {
8+
const func = pipe(
9+
split('.'),
10+
s => concat(['dccFunctions'], s),
11+
s => path(s, window)
12+
)(funcName);
13+
if (!func) {
14+
throw new Error(
15+
`Invalid func for slider tooltip transform: ${funcName}`
16+
);
17+
}
18+
return func(value);
19+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
window.dccFunctions = window.dccFunctions || {};
2+
window.dccFunctions.transformTooltip = function(value) {
3+
return "Transformed " + value
4+
}

components/dash-core-components/tests/integration/sliders/test_sliders.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,12 @@ def test_sls016_sliders_format_tooltips(dash_dcc):
583583
id="range-slider",
584584
tooltip={"format": "Custom tooltip: {value}", "always_visible": True},
585585
),
586+
dcc.Slider(
587+
min=20,
588+
max=100,
589+
id="slider-transform",
590+
tooltip={"always_visible": True, "transform": "transformTooltip"},
591+
),
586592
],
587593
style={"padding": "12px", "marginTop": "48px"},
588594
)
@@ -603,6 +609,9 @@ def test_sls016_sliders_format_tooltips(dash_dcc):
603609
dash_dcc.wait_for_style_to_equal(
604610
"#slider .rc-slider-tooltip-inner > div", "padding", "8px"
605611
)
612+
dash_dcc.wait_for_text_to_equal(
613+
"#slider-transform .rc-slider-tooltip-content", "Transformed 20"
614+
)
606615

607616
dash_dcc.percy_snapshot("sliders-format-tooltips")
608617

0 commit comments

Comments
 (0)