Skip to content

Commit 8abb9c3

Browse files
Merge pull request #29 from SimformSolutionsPvtLtd/develop
Release v0.0.4
2 parents e07d8e1 + 4c796c7 commit 8abb9c3

File tree

12 files changed

+124
-6911
lines changed

12 files changed

+124
-6911
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
# react-native-radial-slider
44

5-
[![npm version](https://img.shields.io/badge/npm%20package-0.0.3-orange)](https://www.npmjs.org/package/react-native-radial-slider) [![Android](https://img.shields.io/badge/Platform-Android-green?logo=android)](https://www.android.com) [![iOS](https://img.shields.io/badge/Platform-iOS-green?logo=apple)](https://developer.apple.com/ios) [![MIT](https://img.shields.io/badge/License-MIT-green)](https://opensource.org/licenses/MIT)
5+
[![npm version](https://img.shields.io/badge/npm%20package-0.0.4-orange)](https://www.npmjs.org/package/react-native-radial-slider) [![Android](https://img.shields.io/badge/Platform-Android-green?logo=android)](https://www.android.com) [![iOS](https://img.shields.io/badge/Platform-iOS-green?logo=apple)](https://developer.apple.com/ios) [![MIT](https://img.shields.io/badge/License-MIT-green)](https://opensource.org/licenses/MIT)
66

77
---
88

9-
This is a pure javascript and react-native-svg based library that provides many variants of `Radial Slider` and `Speedo Meter` including `default`, `radial-circle-slider`, `speedometer` and `speedometer-marker`
10-
9+
This is a pure javascript and react-native-svg based library that provides many variants of `Radial Slider` and `Speedo Meter` including `default`, `radial-circle-slider`, `speedometer` and `speedometer-marker`
1110

1211
Radial Slider allows you to select any specific value from a range of values. It comes with two variants, one is default and which allows selection on a 180-degree arc and the second one is 360-degree which allows selection of values on a complete circle. It can be used to select/set goals, vision, range, etc.
1312

@@ -133,6 +132,7 @@ export default RadialVariant;
133132
```
134133

135134
# SpeedoMeter
135+
136136
> The speedometer will not take user inputs, when we need to update dynamic values at that time we can use it
137137
138138
- SpeedoMeter has two different variants, speedometer and speedometer-marker

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ local.properties
3535
node_modules/
3636
npm-debug.log
3737
yarn-error.log
38+
yarn.lock
3839

3940
# BUCK
4041
buck-out/

example/yarn.lock

Lines changed: 0 additions & 6879 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-radial-slider",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "React Native component to select or highlight a specific value from a range of values",
55
"homepage": "https://github.com/SimformSolutionsPvtLtd/react-native-radial-slider#readme",
66
"main": "lib/index.js",

src/components/RadialSlider/ButtonContent.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const ButtonContent = (props: ButtonProps) => {
1111
activeOpacity={0.7}
1212
disabled={disabled}
1313
onPress={onPress}
14+
onLongPress={props?.onLongPress}
15+
onPressOut={props?.onPressOut}
1416
style={style}>
1517
<Svg height="30" width="45">
1618
<G>

src/components/RadialSlider/LineContent.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const LineContent = (props: LineContentProps) => {
3131
isMarkerVariant,
3232
marks,
3333
isRadialCircleVariant,
34+
isRadialSliderVariant,
35+
isSpeedoMeterVariant,
3436
} = useRadialSlider(props);
3537

3638
const markerInnerValue = Math.round((max / markerValueInterval) as number);
@@ -73,9 +75,10 @@ const LineContent = (props: LineContentProps) => {
7375

7476
const isSpeedoMarker = !isMarkerVariant ? 0 : isMarkerLine ? -10 : 0;
7577

76-
const isSpeedoMeterMarkerLine = isRadialCircleVariant
77-
? false
78-
: isMarkerLine;
78+
const isSpeedoMeterMarkerLine =
79+
isRadialCircleVariant || isRadialSliderVariant || isSpeedoMeterVariant
80+
? false
81+
: isMarkerLine;
7982

8083
const radialCircleLineRotation = isRadialCircleVariant ? 86 : 90;
8184

src/components/RadialSlider/MarkerValueContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const MarkerValueContent = (props: MarkerValueContentProps) => {
2121

2222
return (
2323
<>
24-
{marks.map((mark, index) => {
24+
{marks.map((mark: { value: number }, index: number) => {
2525
const markIndex = Math.floor(
2626
(((((!fixedMarker ? (markerValue as number) : (value as number)) -
2727
min) *

src/components/RadialSlider/RadialSlider.tsx

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import Svg, {
33
Path,
44
Defs,
@@ -20,6 +20,9 @@ import TailText from './TailText';
2020
import LineContent from './LineContent';
2121

2222
const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
23+
const [isStart, setIsStart] = useState<boolean>(false);
24+
const [iconPosition, setIconPosition] = useState<string>('');
25+
2326
const {
2427
step,
2528
radius,
@@ -45,9 +48,10 @@ const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
4548
leftIconStyle,
4649
rightIconStyle,
4750
stroke,
51+
onChange = () => {},
4852
} = props;
4953

50-
const { panResponder, value, setValue, curPoint, currentRadian } =
54+
const { panResponder, value, setValue, curPoint, currentRadian, prevValue } =
5155
useSilderAnimation(props);
5256

5357
const {
@@ -61,6 +65,22 @@ const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
6165
centerValue,
6266
} = useRadialSlider(props);
6367

68+
useEffect(() => {
69+
//check max value length
70+
const maxLength = max?.toString()?.length;
71+
72+
const timerId = setTimeout(handleValue, maxLength > 2 ? 10 : 100);
73+
return () => clearTimeout(timerId);
74+
});
75+
76+
const handleValue = () => {
77+
if (iconPosition === 'up' && max > value) {
78+
isStart && onPressButtons('up');
79+
} else if (iconPosition === 'down' && min < value) {
80+
isStart && onPressButtons('down');
81+
}
82+
};
83+
6484
const leftButtonStyle = StyleSheet.flatten([
6585
leftIconStyle,
6686
(disabled || min === value) && {
@@ -84,9 +104,19 @@ const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
84104

85105
const onPressButtons = (type: string) => {
86106
if (type === 'up' && max > value) {
87-
setValue((prevState: number) => prevState + step);
107+
setValue((prevState: number) => {
108+
prevValue.current = prevState + step;
109+
110+
return prevState + step;
111+
});
112+
onChange(value);
88113
} else if (type === 'down' && min < value) {
89-
setValue((prevState: number) => prevState - step);
114+
setValue((prevState: number) => {
115+
prevValue.current = prevState - step;
116+
117+
return prevState - step;
118+
});
119+
onChange(value);
90120
}
91121
};
92122

@@ -169,6 +199,11 @@ const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
169199
<View style={styles.center}>
170200
<ButtonContent
171201
onPress={() => onPressButtons('down')}
202+
onLongPress={() => {
203+
setIsStart(true);
204+
setIconPosition('down');
205+
}}
206+
onPressOut={() => setIsStart(false)}
172207
buttonType="left-btn"
173208
style={leftButtonStyle}
174209
disabled={disabled || min === value}
@@ -177,6 +212,11 @@ const RadialSlider = (props: RadialSliderProps & typeof defaultProps) => {
177212
<ButtonContent
178213
disabled={disabled || max === value}
179214
onPress={() => onPressButtons('up')}
215+
onLongPress={() => {
216+
setIsStart(true);
217+
setIconPosition('up');
218+
}}
219+
onPressOut={() => setIsStart(false)}
180220
style={rightButtonStyle}
181221
buttonType="right-btn"
182222
stroke={stroke ?? Colors.blue}

src/components/RadialSlider/hooks/useRadialSlider.ts

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useEffect, useMemo } from 'react';
22
import {
33
createRange,
44
getExtraSize,
@@ -22,11 +22,31 @@ const useRadialSlider = (props: RadialSliderHookProps) => {
2222

2323
const centerValue = Math.round((max - min) / 2) as number;
2424

25+
//For default variant in radial slider
26+
const isRadialSliderVariant = variant === Constants.radialSlider;
27+
28+
//For radial-circle-slider variant
2529
const isRadialCircleVariant = variant === Constants.radialCircleSlider;
2630

31+
//For speedometer-marker variant
32+
const isMarkerVariant = variant === Constants.speedoMeterMarker;
33+
34+
//For speedometer variant
35+
const isSpeedoMeterVariant = variant === Constants.speedometer;
36+
2737
const radianValue = isRadialCircleVariant ? 0.057 : openingRadian;
2838

29-
const isMarkerVariant = variant === Constants.speedoMeterMarker;
39+
useEffect(() => {
40+
if (isMarkerVariant)
41+
if (min < 0) {
42+
throw 'Negative number is not allowed';
43+
} else if (max < 0) {
44+
throw 'Negative number is not allowed';
45+
}
46+
if (max < min) {
47+
throw 'max value should be greater than min';
48+
}
49+
}, [isMarkerVariant, max, min]);
3050

3151
const angle = (radianValue * 180.0) / Math.PI;
3252

@@ -64,17 +84,33 @@ const useRadialSlider = (props: RadialSliderHookProps) => {
6484
);
6585

6686
const marks = useMemo(() => {
67-
const stepsLength = Math.round((max - min) / step);
68-
69-
return [...Array(stepsLength + 1)].map((_val, index) => {
70-
const isEven = index % 2 === 0;
71-
72-
return {
73-
isEven,
74-
value: Math.round(index * step),
75-
};
76-
});
77-
}, [max, min, step]);
87+
if (isMarkerVariant) {
88+
const stepsLength = Math.round((max - min) / step);
89+
90+
return [...Array(stepsLength + 1)].map((_val, index) => {
91+
const isEven = index % 2 === 0;
92+
93+
return {
94+
isEven,
95+
value: Math.round(index * step),
96+
};
97+
});
98+
} else {
99+
const array: any = [];
100+
for (let i = 0; i <= max; i++) {
101+
array.push(i);
102+
}
103+
104+
return array.map((index: number) => {
105+
const isEven = index % 2 === 0;
106+
107+
return {
108+
isEven,
109+
value: Math.round(index * step),
110+
};
111+
});
112+
}
113+
}, [isMarkerVariant, max, min, step]);
78114

79115
return {
80116
angle,
@@ -91,6 +127,8 @@ const useRadialSlider = (props: RadialSliderHookProps) => {
91127
marks,
92128
isRadialCircleVariant,
93129
centerValue,
130+
isRadialSliderVariant,
131+
isSpeedoMeterVariant,
94132
};
95133
};
96134

src/components/RadialSlider/hooks/useSilderAnimation.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,17 @@ const useSilderAnimation = (props: RadialSliderAnimationHookProps) => {
3939
const prevValue = useRef(props.value > min ? props.value : min);
4040

4141
const [value, setValue] = useState(
42-
props.value >= min ? props.value || min : min
42+
props?.value < min ? min : props?.value > max ? max : props?.value
4343
);
4444

4545
useEffect(() => {
46-
if (props?.value < min) {
47-
setValue(min);
48-
} else if (props?.value > max) {
46+
if (max < props?.value) {
4947
setValue(max);
50-
} else {
51-
setValue(props.value);
48+
} else if (min > props?.value) {
49+
setValue(min);
5250
}
53-
}, [max, min, props.value]);
51+
// eslint-disable-next-line react-hooks/exhaustive-deps
52+
}, [max, min]);
5453

5554
const handlePanResponderGrant = () => {
5655
moveStartValue = prevValue.current;

0 commit comments

Comments
 (0)