Skip to content

Commit 3e10a54

Browse files
committed
Added step prop
+ Added margin value in SB story + Refactoring
1 parent 88d4846 commit 3e10a54

6 files changed

+47
-15
lines changed

src/components/MultiRangeSlider.tsx

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Utils
2+
import { decimalCount } from '../utils/utils'
3+
4+
// Types
15
import { SliderProps } from '../types'
26

37
// Styles
@@ -25,6 +29,7 @@ const MultiRangeSlider = function (props: MultiRangeSliderProps) {
2529
const {
2630
defaultMinValue,
2731
defaultMaxValue,
32+
step = 1,
2833
minValue,
2934
maxValue,
3035
onChange,
@@ -39,9 +44,7 @@ const MultiRangeSlider = function (props: MultiRangeSliderProps) {
3944
* @param value - New value set on the slider.
4045
* @returns Percent of the slider that need to be colored, from 0 to 100.
4146
*/
42-
const getPercent = (value: number) => {
43-
return Math.round(((value - defaultMinValue) / (defaultMaxValue - defaultMinValue)) * 100)
44-
}
47+
const getPercent = (value: number) => Math.round(((value - defaultMinValue) / (defaultMaxValue - defaultMinValue)) * 100)
4548

4649
/**
4750
* Invokes callback function if slider value is updated.
@@ -62,11 +65,11 @@ const MultiRangeSlider = function (props: MultiRangeSliderProps) {
6265
* @returns Div element with labels.
6366
*/
6467
const renderLabels = () => {
65-
if (showLabels) {
68+
if (showLabels && minValue !== undefined && maxValue !== undefined) {
6669
return (
6770
<>
68-
<div className="slider__left-value">{minValue}</div>
69-
<div className="slider__right-value">{maxValue}</div>
71+
<div className="slider__left-value">{minValue.toFixed(decimalCount(step))}</div>
72+
<div className="slider__right-value">{maxValue.toFixed(decimalCount(step))}</div>
7073
</>
7174
)
7275
}
@@ -84,17 +87,18 @@ const MultiRangeSlider = function (props: MultiRangeSliderProps) {
8487
max={defaultMaxValue}
8588
value={minValue}
8689
onChange={(event) => {
87-
handleChange(parseInt(event.target.value), maxValue)
90+
handleChange(parseFloat(event.target.value), maxValue)
8891
}}
8992
className="thumb thumb--left"
9093
/>
9194
<input
9295
type="range"
96+
step={step}
9397
min={defaultMinValue}
9498
max={defaultMaxValue}
9599
value={maxValue}
96100
onChange={(event) => {
97-
handleChange(minValue, parseInt(event.target.value))
101+
handleChange(minValue, parseFloat(event.target.value))
98102
}}
99103
className="thumb thumb--right"
100104
/>

src/components/SingleRangeSlider.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Utils
2+
import { decimalCount } from '../utils/utils'
3+
4+
// Types
15
import { SliderProps } from '../types'
26

37
// Styles
@@ -18,7 +22,7 @@ interface SingleRangeSliderProps extends SliderProps {
1822
* @returns Component.
1923
*/
2024
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
2226

2327
/**
2428
* Invokes callback function if slider value is updated.
@@ -37,18 +41,16 @@ const SingleRangeSlider = function (props: SingleRangeSliderProps) {
3741
* @param newValue - New value set on the slider.
3842
* @returns Percent of the slider that need to be colored, from 0 to 100.
3943
*/
40-
const getSelectedSliderPercent = (newValue: number) => {
41-
return (newValue / defaultMaxValue) * 100
42-
}
44+
const getSelectedSliderPercent = (newValue: number) => newValue / defaultMaxValue * 100
4345

4446
/**
4547
* If `showLabels` is `true`, returns a `<div>` that contains a label with the current value of the slider.
4648
*
4749
* @returns Div with label.
4850
*/
4951
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>
5254
}
5355
}
5456

@@ -61,8 +63,9 @@ const SingleRangeSlider = function (props: SingleRangeSliderProps) {
6163
min={defaultMinValue}
6264
max={defaultMaxValue}
6365
value={value}
66+
step={step}
6467
onChange={(event) => {
65-
handleValueChange(parseInt(event.target.value))
68+
handleValueChange(parseFloat(event.target.value))
6669
}}
6770
className="thumb thumb--left"
6871
/>

src/stories/MultiRangeSlider.stories.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ const Template: ComponentStory<typeof MultiRangeSlider> = (args: MultiRangeSlide
4040
export const Basic = Template.bind({})
4141
Basic.args = {
4242
showLabels: true,
43+
step: 1,
4344
minValue: 30,
4445
maxValue: 70,
4546
defaultMinValue: 0,
4647
defaultMaxValue: 100,
48+
margin: 0,
4749
color: 'Orange'
4850
}

src/stories/SingleSlider.stories.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const Basic = Template.bind({})
3434
Basic.args = {
3535
showLabels: true,
3636
value: 40,
37+
step: 1,
3738
defaultMinValue: 0,
3839
defaultMaxValue: 100,
3940
color: 'Salmon'

src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ interface SliderProps {
150150
color?: RangeColor;
151151
/** Indicates if minimum/maximum values labels should be shown. Default to `false`. */
152152
showLabels?: boolean;
153+
/** Step to change slider value. Default to 1. */
154+
step?: number;
153155
}
154156

155157
export type { RangeColor, SliderProps }

src/utils/utils.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Gets the count of numbers after the decimal point from a number.
3+
*
4+
* @param num - Number to get its decimal count.
5+
* @returns Number of decimals.
6+
*/
7+
const decimalCount = (num: number): number => {
8+
// Convert to String
9+
const numStr = num.toString()
10+
11+
// String Contains Decimal
12+
if (numStr.includes('.')) {
13+
return numStr.split('.')[1].length
14+
}
15+
16+
// String Does Not Contain Decimal
17+
return 0
18+
}
19+
20+
export { decimalCount }

0 commit comments

Comments
 (0)