1
+ // Utils
2
+ import { decimalCount } from '../utils/utils'
3
+
4
+ // Types
1
5
import { SliderProps } from '../types'
2
6
3
7
// Styles
@@ -18,7 +22,7 @@ interface SingleRangeSliderProps extends SliderProps {
18
22
* @returns Component.
19
23
*/
20
24
const SingleRangeSlider = function ( props : SingleRangeSliderProps ) {
21
- const { defaultMinValue, defaultMaxValue, value, color = 'Green' , showLabels = false , onChange } = props
25
+ const { defaultMinValue, defaultMaxValue, value, color = 'Green' , showLabels = false , step = 1 , onChange } = props
22
26
23
27
/**
24
28
* Invokes callback function if slider value is updated.
@@ -37,18 +41,16 @@ const SingleRangeSlider = function (props: SingleRangeSliderProps) {
37
41
* @param newValue - New value set on the slider.
38
42
* @returns Percent of the slider that need to be colored, from 0 to 100.
39
43
*/
40
- const getSelectedSliderPercent = ( newValue : number ) => {
41
- return ( newValue / defaultMaxValue ) * 100
42
- }
44
+ const getSelectedSliderPercent = ( newValue : number ) => newValue / defaultMaxValue * 100
43
45
44
46
/**
45
47
* If `showLabels` is `true`, returns a `<div>` that contains a label with the current value of the slider.
46
48
*
47
49
* @returns Div with label.
48
50
*/
49
51
const renderLabels = ( ) => {
50
- if ( showLabels ) {
51
- return < div className = "slider__left-value" > { value } </ div >
52
+ if ( showLabels && value !== undefined ) {
53
+ return < div className = "slider__left-value" > { value . toFixed ( decimalCount ( step ) ) } </ div >
52
54
}
53
55
}
54
56
@@ -61,8 +63,9 @@ const SingleRangeSlider = function (props: SingleRangeSliderProps) {
61
63
min = { defaultMinValue }
62
64
max = { defaultMaxValue }
63
65
value = { value }
66
+ step = { step }
64
67
onChange = { ( event ) => {
65
- handleValueChange ( parseInt ( event . target . value ) )
68
+ handleValueChange ( parseFloat ( event . target . value ) )
66
69
} }
67
70
className = "thumb thumb--left"
68
71
/>
0 commit comments